我有一個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)
我已經看過了錯誤,有一個微軟網頁...
.. 。這表明這可能是一個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))
任何想法?
我會在早上試試這個,謝謝... –
對不起它採取這麼久纔回到你身邊......我改變了代碼如下所示... myMSWord =新建Word.Application Dim inval As Object '在將對象傳遞給方法之前對其進行規劃。 inval = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter) myMSDoc = myMSWord.Documents。打開(inval) 'myMSDoc = myMSWord.Documents.Open(myNewsLetter) –
哦,這是不可讀的,我將不得不編輯原始問題... –