VB.net program falling foul of InteropServices.COMException Bug

0

I have a VB.net program that I wrote and have used hundreds of times. Whilst using Windows 7 I "upgraded" to Office 2010 and IIRC had to make a few small changes to get it to work. I have now (and again I put it in quotes as I fail to see the benefits of calling it an upgrade !) "upgraded" to Windows 10 but went back to Office 2007 as I much prefer it. I am also using Visual Studio Community 2015. All of that may or may not be of help !!!

So, I run the program and it fails with the following error :

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

Additional information: Word cannot open this document template.

(L:...\Customize Ribbon Example 2.dotm)

I have looked up the error and there is a Microsoft page ...

MS Support Bug

... that suggests this may be a Bug, it explains why it may be happening and gives a resolution but I program for fun, I'm not an expert in VB at all and it may as well be written in Russian for all it helps me !!!

I also have no idea why Word should be trying to open that Example Template either, I copy a Template of my own to create a new Word document, this is pretty basic stuff !!! This is the relevant code, any help would be very much appreciated ...

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 :

Olaf, I updated the code as follows ...

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)

... but I am getting a similar error on the Open statement ...

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))

Any ideas ?

vb.net
asked on Stack Overflow Sep 22, 2016 by Gary Heath • edited Sep 23, 2016 by Gary Heath

1 Answer

1

The MS page says you should try something like

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)
answered on Stack Overflow Sep 22, 2016 by Olaf

User contributions licensed under CC BY-SA 3.0