2016-09-22 106 views
0

我有一个VB.net程序,我写了并使用了数百次。在使用Windows 7时,我“升级”到Office 2010,IIRC必须做一些小改动才能使其运行。我现在(并且再次将它放在引号中,因为我没有看到将其称为升级的好处!)“升级”到Windows 10,但我更喜欢它,因此又回到了Office 2007。我也在使用Visual Studio Community 2015.所有这些可能或可能不会有帮助!VB.net程序犯InteropServices.COMException错误

所以,我运行程序,它失败,出现以下错误:

的类型 “System.Runtime.InteropServices.COMException”未处理的异常出现在 KA_Newsletter.exe

附加信息:Word无法打开此文档模板。

(L:... \自定义功能区示例2.dotm)

我已经看过了错误,有一个微软网页...

MS Support Bug

.. 。这表明这可能是一个Bug,它解释了为什么它可能会发生,并给出了解决方案,但我编程的乐趣,我不是VB的专家,它可能是用俄语写的所有它可以帮助我!

我也不知道为什么Word应该试图打开示例模板,我复制我自己的模板来创建一个新的Word文档,这是非常基本的东西!这是相关的代码,任何帮助将非常感激...

Dim myNewsLetter As String 

。 。 。

If File.Exists(myNewsLetter) Then 
    'do nothing 
Else 
    myTemplate = myTempFolder & "KA_Newsletter.doc" 
    File.Copy(myTemplate, myNewsLetter) 
    Create_Blank_Newsletter() 
End If 

。 。 。

Private Sub Create_Blank_Newsletter() 

    myMSWord = New Word.Application 
    myMSDoc = myMSWord.Documents.Open(myNewsLetter) << <Error occurs on this line 
    myMSWord.WindowState= Word.WdWindowState.wdWindowStateNormal 
    myMSWord.Visible= False 

UPDATE:

奥拉夫,我更新的代码如下...

myMSWord = New Word.Application 

Dim inval As Object 
'Marshal the object before passing it to the method. 
inval = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter) 
myMSDoc = myMSWord.Documents.Open(inval) 

'myMSDoc = myMSWord.Documents.Open(myNewsLetter) 

...但我得到一个类似的错误在打开的语句...

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in KA_Newsletter.exe 

Additional information: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) 

任何想法?

回答

1

的MS页说,你应该尝试像

Dim inval As Object 
'Marshal the object before passing it to the method. 
inVal = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter) 
myMSDoc = myMSWord.Documents.Open(inval) 
+0

我会在早上试试这个,谢谢... –

+0

对不起它采取这么久才回到你身边......我改变了代码如下所示... myMSWord =新建Word.Application Dim inval As Object '在将对象传递给方法之前对其进行规划。 inval = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter) myMSDoc = myMSWord.Documents。打开(inval) 'myMSDoc = myMSWord.Documents.Open(myNewsLetter) –

+0

哦,这是不可读的,我将不得不编辑原始问题... –