Microsoft Interop Assembly for Word fails with newest version of Word from Microsoft 365 version 2211

K Burn 1 Reputation point
2022-12-28T22:25:41.617+00:00

I have a program that uses the Word Interop Assembly to open documents. This code has worked for years, but when a user has updated to the newest version of Word from Microsoft 365 build v2211, it doesn't work. When a user reverts to a previous version (such as 2209), it works just fine.

The code failing is fairly basic:

oWord = New Word.Application  
  
 oDOC = oWord.Documents.Open(WordFilePath, objMissing, True, False, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing)  

The code fails at oWords.Documents.Open, saying that it is a null object. This only happens with Word version 2211 when attempting to access template files (.dotx). (the "objMissing" in the Documents.Open are optional parameters, so we don't think this is contributing).

It seems like this new version of Word is incompatible with with the Word Assembly in some way, or there is a breaking change we are not aware of.

Can anyone provide any insights into this behavior change? Has anyone else encountered an issue like this recently?

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Word | For business | Windows
Developer technologies | Visual Basic for Applications
{count} votes

1 answer

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points
    2022-12-30T16:59:48.013+00:00

    Try to use this code:

    Imports Microsoft.Office.Interop.Word  
    Imports System.Runtime.InteropServices  
      
    'public declaration'  
    Public oWord As Word.Application = Nothing  
      
    'in your class '  
    Sub your_procedure()  
    Try  
       oWord = WDExist()  
    Catch  
      msgbox("You do not have MS Word installed")  
      Exit sub  
    End Try  
      
    Dim WordFilePath$ = "c:\your.docx"  
    Dim WordDok As Word.Document = oWord.Documents.Open(WordFilePath, ,True)  
    '... do somenthing with this document'  
    end sub  
      
        Public Function WDExist() As Object  
            Try  
                oWord = CType(Marshal.GetActiveObject("Word.Application"), Word.Application)  
            Catch ex As Exception  
                If oWord Is Nothing Then oWord = GetObject(, "Word.Application")  
            Finally  
                If oWord Is Nothing Then oWord = CType(CreateObject("Word.Application"), Word.Application) 'not visible yet'  
            End Try  
            Return oWord   
        End Function  
    

    And Happy New Year.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.