You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VBA检查Office 365离线许可剩余有效期并设置预警?

解决方案:用VBA监控Office 365离线许可剩余时间

核心思路

Office 365的许可离线期限及最后验证日期可通过Windows Management Instrumentation (WMI)查询系统许可信息获取。以下VBA代码可自动计算剩余离线天数,当不足7天时触发警报。

VBA实现代码

Sub CheckOffice365LicenseExpiry()
    Dim objWMIService As Object
    Dim colProducts As Object
    Dim objProduct As Object
    Dim lastActivationDate As Date
    Dim expiryDate As Date
    Dim remainingDays As Integer
    
    ' 连接到本地WMI服务
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    
    ' 查询所有激活状态的Office 365许可
    Set colProducts = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_SoftwareLicensingProduct WHERE Name LIKE '%Office 365%' AND LicenseStatus = 1")
    
    If colProducts.Count = 0 Then
        MsgBox "未检测到激活状态的Office 365许可", vbExclamation
        Exit Sub
    End If
    
    ' 遍历获取目标许可的最后验证日期
    For Each objProduct In colProducts
        If Not IsNull(objProduct.LastActivationDate) Then
            lastActivationDate = CDate(objProduct.LastActivationDate)
            ' 计算许可到期日(默认离线有效期31天)
            expiryDate = DateAdd("d", 31, lastActivationDate)
            remainingDays = DateDiff("d", Date, expiryDate)
            
            ' 剩余时间不足7天触发警告
            If remainingDays <= 7 Then
                MsgBox "警告:Office 365离线许可剩余" & remainingDays & "天到期,即将切换为只读模式,请尽快联网验证!", vbCritical
            Else
                MsgBox "Office 365离线许可剩余" & remainingDays & "天到期", vbInformation
            End If
            Exit For
        End If
    Next objProduct
    
    ' 释放对象资源
    Set objProduct = Nothing
    Set colProducts = Nothing
    Set objWMIService = Nothing
End Sub

代码说明

  1. WMI查询逻辑:通过Win32_SoftwareLicensingProduct类筛选激活状态(LicenseStatus = 1)的Office 365许可条目。
  2. 日期计算规则:以LastActivationDate(最后一次联网验证日期)为起点,加31天得到许可到期日,计算当前日期到到期日的剩余天数。
  3. 警报触发条件:剩余天数≤7时弹出警告框,明确提示用户需联网验证许可。

自动触发设置

若需每次打开工作簿时自动执行检查,可将代码添加到工作簿的ThisWorkbook模块中:

Private Sub Workbook_Open()
    CheckOffice365LicenseExpiry
End Sub

注意事项

  • 运行代码需管理员权限(WMI查询系统许可信息需要对应权限)。
  • 若设备安装多个Office版本,可根据Name属性调整WMI查询的筛选条件,确保定位到正确的Office 365产品。
  • 若LastActivationDate为空,说明许可从未完成联网验证,需立即联网激活。

内容的提问来源于stack exchange,提问作者Mike M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 15:42:38