vb.net Outlook 2007创建新实例时出错(对象引用未设置为对象实例)

时间:2018-02-12 06:45:16

标签: vb.net outlook office-interop

我正在构建一个windows-form-application,它将使用Outlook Interop(它不是vsto插件)进行一些自动化。并且这适用于除outlook 2007之外的所有版本,它在启动Dim oApp As New Outlook.Application时生成错误消息(错误:Object reference not set to an instance of an object)但是在抛出错误后它会自动启动进程outlook.exe,但是不执行任何任务。

注意:应用程序仅在没有运行Outlook实例时才有效,因此这与创建新实例New Outlook.Application不会产生任何冲突。我正在使用2017年的视觉工作室。

Imports Microsoft.Office.Interop '(Version 14.0)

Public Sub Form_Startup() Handles Me.Load
    If Not IsProcessRunning("OUTLOOK") Then
        DoSomething()
    End If
End Sub

Public Sub DoSomething()
  Dim oApp As New Outlook.Application
  Dim oNspace As Outlook.NameSpace = oApp.GetNamespace("MAPI")
  Dim oStores As Outlook.Stores = oApp.Session.Stores
  Dim dFolder As Outlook.Folder 
  'Some code here
  'Some other code here
  Runtime.InteropServices.Marshal.ReleaseComObject(oApp)
  Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
  oApp = Nothing
End Sub

另一方面,我也在vsto插件中测试了这个DoSomething()部分,在包括2007在内的所有版本中都没有错误。

更新

使用以下来自其他专家的建议仍未解决我在outlook 2007中的问题。我遵循了后期绑定以及@aduguid建议的直接版本引用到Outlook 2007并没有解决错误消息。也许我很想理解来自这里评论的反馈。

Dim outlook As Object = Nothing 
outlook = CreateObject("Outlook.Application.12")

enter image description here

这个目标的主要目标是构建一个可用于所有Outlook版本的outlook的自动化应用程序(例如2007,10,13,16),我的上述代码与2007年以外的所有高级版本一起工作正常。我不得不在我的开发环境使用Visual Studion 2017进行设置时,单独使用单独的VM测试所有办公室版本。

1 个答案:

答案 0 :(得分:0)

以下是与特定版本的Outlook进行后期绑定的示例。

 Dim outlook As Object = Nothing
 outlook = CreateObject("Outlook.Application.12")
相关问题