Outlook 加载项错误
Outlook Add-In error
我正在创建一个加载项,让用户单击一个按钮,它会打开一封新电子邮件并自动填充字段,并让他们在发送电子邮件之前编辑正文。
我收到一个错误,不允许我使用 Outlook.Application
Error BC30111 'Application' is an interface type and cannot be used as
an expression.
我做错了什么?
我的代码:
Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Tools.Ribbon
Public Class Ribbon1
Private olMailItem As Object
Private olImportanceHigh As OlImportance
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim obApp As Object
Dim NewMail As MailItem
obApp = Outlook.Application
NewMail = obApp.CreateItem(olMailItem)
'You can change the concrete info as per your needs
With NewMail
.Subject = " Test Email"
.To = "example@mail.com"
.Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
.Importance = olImportanceHigh
.Display
End With
obApp = Nothing
NewMail = Nothing
End Sub
End Class
根据您的代码进行的更正 - 已声明 obApp as Outlook.Application
在创建对象实例和处置对象时添加了Set
。
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim obApp As Outlook.Application
Dim NewMail As MailItem
Set obApp = New Outlook.Application
Set NewMail = obApp.CreateItem(olMailItem)
'You can change the concrete info as per your needs
With NewMail
.Subject = " Test Email"
.To = "example@mail.com"
.Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
.Importance = olImportanceHigh
.Display
End With
Set obApp = Nothing
Set NewMail = Nothing
End Sub
您的 VSTO 加载项有一个入口点。那将是一个 Partial Class
可能称为 ThisAddIn
,具有 ThisAddIn_Startup
和 ThisAddIn_Shutdown
等方法(根据 this article)。
class 继承了一个 class,该 class 公开了一个类型为 Outlook.Application
的 Application
属性 - 这就是您要使用的对象。
因此,当您在启动时创建它时,将该对象传递给您的 Ribbon1
实例:
Private Sub ThisAddIn_Startup(object sender, System.EventArgs e) Handles Something(?).Startup
Dim ribbon As Ribbon1 = New Ribbon1(Me.Application)
' ...
End Sub
为您的 Ribbon1
class 公开构造函数,以便您可以传递 Application
实例:
Private olApp As Outlook.Application
Public Sub New(ByVal app As Outlook.Application)
olApp = app
End Sub
现在删除您的本地 obApp As Outlook.Application
声明,并改用从构造函数传递的实例。
编辑:从头开始。阅读我链接到的文章,那里 是 一个全局 Application
实例随时可用 - 只需使用它:
obApp = Globals.ThisAddIn.Application
因此,您的 Ribbon1
class 不需要通过其构造函数接收 Application
实例。虽然,使用全局变量是否是一个更好的设计是值得商榷的。
我正在创建一个加载项,让用户单击一个按钮,它会打开一封新电子邮件并自动填充字段,并让他们在发送电子邮件之前编辑正文。
我收到一个错误,不允许我使用 Outlook.Application
Error BC30111 'Application' is an interface type and cannot be used as an expression.
我做错了什么?
我的代码:
Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Tools.Ribbon
Public Class Ribbon1
Private olMailItem As Object
Private olImportanceHigh As OlImportance
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim obApp As Object
Dim NewMail As MailItem
obApp = Outlook.Application
NewMail = obApp.CreateItem(olMailItem)
'You can change the concrete info as per your needs
With NewMail
.Subject = " Test Email"
.To = "example@mail.com"
.Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
.Importance = olImportanceHigh
.Display
End With
obApp = Nothing
NewMail = Nothing
End Sub
End Class
根据您的代码进行的更正 - 已声明 obApp as Outlook.Application
在创建对象实例和处置对象时添加了Set
。
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim obApp As Outlook.Application
Dim NewMail As MailItem
Set obApp = New Outlook.Application
Set NewMail = obApp.CreateItem(olMailItem)
'You can change the concrete info as per your needs
With NewMail
.Subject = " Test Email"
.To = "example@mail.com"
.Body = "This is just a test email template with Outlook VBA" & vbCrLf & vbCrLf & vbCrLf & "Yours Truly," & vbCrLf & vbCrLf & "John Smith"
.Importance = olImportanceHigh
.Display
End With
Set obApp = Nothing
Set NewMail = Nothing
End Sub
您的 VSTO 加载项有一个入口点。那将是一个 Partial Class
可能称为 ThisAddIn
,具有 ThisAddIn_Startup
和 ThisAddIn_Shutdown
等方法(根据 this article)。
class 继承了一个 class,该 class 公开了一个类型为 Outlook.Application
的 Application
属性 - 这就是您要使用的对象。
因此,当您在启动时创建它时,将该对象传递给您的 Ribbon1
实例:
Private Sub ThisAddIn_Startup(object sender, System.EventArgs e) Handles Something(?).Startup
Dim ribbon As Ribbon1 = New Ribbon1(Me.Application)
' ...
End Sub
为您的 Ribbon1
class 公开构造函数,以便您可以传递 Application
实例:
Private olApp As Outlook.Application
Public Sub New(ByVal app As Outlook.Application)
olApp = app
End Sub
现在删除您的本地 obApp As Outlook.Application
声明,并改用从构造函数传递的实例。
编辑:从头开始。阅读我链接到的文章,那里 是 一个全局 Application
实例随时可用 - 只需使用它:
obApp = Globals.ThisAddIn.Application
因此,您的 Ribbon1
class 不需要通过其构造函数接收 Application
实例。虽然,使用全局变量是否是一个更好的设计是值得商榷的。