Outlook VSTO Add-in VB – 如何读取在收件箱中打开的电子邮件的主题行?

huangapple go评论63阅读模式
英文:

Outlook VSTO Add-in VB - How to read the subject line of an email opened in the inbox?

问题

我已创建了一个插件,允许用户将有关电子邮件的信息添加到 SQL 表中。

我目前正在尝试实现一个提高用户体验的功能,它将从收件箱中打开的电子邮件的主题行读取到用户表单的相关字段中。我尝试了几种方法,但都没有成功。

这是我当前的代码:

Dim newMail As Outlook.MailItem
Dim oInspector As Outlook.Inspector

oInspector = Microsoft.Office.Interop.Outlook.Application.ActiveInspector()
newMail = oInspector.CurrentItem

[初始化表单的其他部分]

If oInspector Is Nothing Then
   MsgBox("没有活动检查员")
Else
   newMail = oInspector.CurrentItem
   With AddEmailSubjectTextBox
        .Text = newMail.Subject
   End With
End If

除了抛出的"reference to a non-shared member requires an object reference"错误之外,我认为我的问题的一部分是 ActiveInspector 方法和 CurrentItem 应该表示一个在弹出窗口中编写但尚未发送的电子邮件。

英文:

I have created an add-in that allows a user to add information concerning an email into a SQL Table.

I am currently trying to implement a quality-of-life feature that will read the subject line of an opened email from the inbox into the relevant field of the user form. I have tried several methods and have had no success.

This is what I have currently:

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

Besides the "reference to a non-shared member requires an object reference" error that is thrown, I believe part of my issue is that the ActiveInspector method and CurrentItem are supposed to represent an email that is being written in a pop-up window and has yet to be sent.

答案1

得分: 0

以下是翻译好的部分:

应处理有效的 Outlook `Application` 实例的以下代码行:

oInspector = Microsoft.Office.Interop.Outlook.Application.ActiveInspector()


但你试图在类型声明上调用一个方法。在 VBA 环境中,你可以使用全局的 `Application` 属性。例如:

oInspector = Application.ActiveInspector()


在基于 VSTO 的加载项中,你可以使用 `ThisAddin` 类的 `Application` 属性。你还可以使用 `Globals` 命名空间,它允许在加载项的任何代码中访问应用程序实例,详细信息请参见 [Office 项目中对象的全局访问][1]。对于 VSTO 加载项,代码应如下所示:

Dim newMail As Outlook.MailItem
Dim oInspector As Outlook.Inspector

oInspector = Globals.ThisAddIn.Application.ActiveInspector()
newMail = oInspector.CurrentItem

[初始化表单的其他部分]

If oInspector Is Nothing Then
MsgBox("没有活动检查员")
Else
newMail = oInspector.CurrentItem
With AddEmailSubjectTextBox
.Text = newMail.Subject
End With
End If


请注意,检查员窗口中打开的项可能是不同的 - 预约、任务、便笺、文档等。因此,还有意义添加检查员窗口中打开的项类型的检查。

最后,如果你需要获取 `Explorer` 窗口中当前选定的项,你需要使用 [Selection][2] 属性

  [1]: https://learn.microsoft.com/en-us/visualstudio/vsto/global-access-to-objects-in-office-projects?view=vs-2022&tabs=csharp
  [2]: https://learn.microsoft.com/en-us/office/vba/api/outlook.explorer.selection
英文:

The following line of code should deal with a valid Outlook Application instance:

oInspector = Microsoft.Office.Interop.Outlook.Application.ActiveInspector()

But you are trying to call a method on a type declaration. In the VBA environment you can use the global Application property. For example:

oInspector = Application.ActiveInspector()

In VSTO based add-ins you can use the Application property of the ThisAddin class. You may also use the Globals namespace which gives access to the Application instance anywhere in the code of your add-in, see Global access to objects in Office projects for more information. For VSTO add-ins the code should look like that:

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

Be aware, the item opened in the inspector window can be different - appointment, task, note, document. So, it also makes sense to add a check for the item type opened in the inspector window.

Finally, if you need to get the currently selected item in the Explorer window, you need to use the Selection property

huangapple
  • 本文由 发表于 2023年2月23日 23:19:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定