如何通过VBA实现AIP插件的邮件分类?原使用Document Tagging插件
Hey there, let’s walk through how to get Azure Information Protection (AIP) plugin email classification working with VBA, since you’re coming from the Document Tagging Outlook plugin and its Tag.applicationForMacro.MacroOutlookClassify(something) call.
首先,确认AIP插件的基础准备
Before diving into code, make sure:
- The Azure Information Protection Outlook plugin is installed, enabled, and running in your Outlook (check COM Add-ins under File > Options > Add-ins).
- Your AIP tenant has labels configured that you want to apply to emails.
获取AIP插件的宏交互对象
Unlike the Document Tagging plugin’s Tag.applicationForMacro object, AIP uses a specific macro interface. You can access it in two ways:
方法1:直接创建对象
Use CreateObject to grab the AIP macro interface (works if the plugin is registered correctly):
Dim aipMacro As Object Set aipMacro = CreateObject("Microsoft.InformationProtection.MacroInterface")
方法2:通过Outlook的COM Add-ins集合
If the first method fails, you can fetch the add-in directly from Outlook’s COM add-ins list:
Dim aipAddin As COMAddIn Set aipAddin = Application.COMAddIns("Microsoft.InformationProtection.OutlookAddin") Dim aipMacro As Object Set aipMacro = aipAddin.Object
调用AIP的邮件分类方法
Once you have the aipMacro object, you can use its ClassifyEmail method to apply labels. Here’s a practical example that classifies the currently selected email:
Sub ClassifySelectedEmailWithAIP() ' 检查是否有选中的邮件 If Application.ActiveExplorer.Selection.Count = 0 Then MsgBox "Please select an email first!", vbExclamation Exit Sub End If Dim targetMail As MailItem Set targetMail = Application.ActiveExplorer.Selection.Item(1) ' 获取AIP宏接口 Dim aipMacro As Object On Error Resume Next Set aipMacro = CreateObject("Microsoft.InformationProtection.MacroInterface") On Error GoTo 0 If Not aipMacro Is Nothing Then ' 替换成你的AIP标签名称或GUID(GUID更可靠,避免名称变更) Dim targetLabel As String targetLabel = "Confidential - Internal Only" ' 或者用标签GUID:"xxxx-xxxx-xxxx-xxxx" ' 调用分类方法 aipMacro.ClassifyEmail targetMail, targetLabel MsgBox "Email labeled successfully with AIP!", vbInformation Else MsgBox "Could not connect to AIP plugin. Make sure it's installed, enabled, and up to date.", vbCritical End If End Sub
关键注意事项
- 标签标识:使用标签的GUID代替名称会更可靠,因为标签名称可能在AIP门户中被修改。你可以从AIP管理中心获取标签的GUID。
- 版本兼容性:确保你运行的是最新版本的AIP Outlook插件——旧版本可能不提供宏接口。
- 权限:运行宏的用户需要拥有应用目标AIP标签的权限(检查你的AIP标签策略)。
- 错误处理:务必添加错误检查(比如上面的
On Error代码块),以处理AIP插件不可用的情况。
内容的提问来源于stack exchange,提问作者Miroslav Kolouch

