【问题标题】:Outlook VSTO Add-in VB - How to read the subject line of an email opened in the inbox?Outlook VSTO 加载项 VB - 如何阅读在收件箱中打开的电子邮件的主题行?
【发布时间】:2023-02-23 23:40:27
【问题描述】:
我创建了一个插件,允许用户将有关电子邮件的信息添加到 SQL 表中。
我目前正在尝试实现一种生活质量功能,该功能会将收件箱中打开的电子邮件的主题行读取到用户表单的相关字段中。我尝试了几种方法,但都没有成功。
这是我目前拥有的:
Dim newMail As Outlook.MailItem
Dim oInspector As Outlook.Inspector
oInspector = Microsoft.Office.Interop.Outlook.Application.ActiveInspector()
newMail = oInspector.CurrentItem
[Initializing other parts of form]
If oInspector Is Nothing Then
MsgBox("No Active Inspector")
Else
newMail = oInspector.CurrentItem
With AddEmailSubjectTextBox
.Text = newMail.Subject
End With
End If
除了抛出的“对非共享成员的引用需要对象引用”错误外,我认为我的部分问题是 ActiveInspector 方法和 CurrentItem 应该代表正在弹出窗口中写入的电子邮件,并且尚未发送。
【问题讨论】:
标签:
vb.net
outlook
vsto
outlook-addin
office-addins
【解决方案1】:
以下代码行应处理有效的 Outlook Application 实例:
oInspector = Microsoft.Office.Interop.Outlook.Application.ActiveInspector()
但是您正试图在类型声明上调用方法。在 VBA 环境中,您可以使用全局 Application 属性。例如:
oInspector = Application.ActiveInspector()
在基于 VSTO 的加载项中,您可以使用 ThisAddin 类的 Application 属性。您还可以使用 Globals 命名空间,它可以在您的加载项代码中的任何位置访问 Application 实例,有关详细信息,请参阅 Global access to objects in Office projects。对于 VSTO 加载项,代码应如下所示:
Dim newMail As Outlook.MailItem
Dim oInspector As Outlook.Inspector
oInspector = Globals.ThisAddIn.Application.ActiveInspector()
newMail = oInspector.CurrentItem
[Initializing other parts of form]
If oInspector Is Nothing Then
MsgBox("No Active Inspector")
Else
newMail = oInspector.CurrentItem
With AddEmailSubjectTextBox
.Text = newMail.Subject
End With
End If
请注意,在检查器窗口中打开的项目可以不同 - 约会、任务、笔记、文档。因此,为检查器窗口中打开的项目类型添加检查也是有意义的。
最后,如果需要获取Explorer窗口中当前选中的项,需要使用Selection属性