Word.Document not defined

Jeff Stiegler 466 Reputation points
2022-05-03T14:37:23.163+00:00

If found the code below on the Internet but cannot use it because some of the code such as Word.Document and Word.Application produces an error "not defined". Is there an Import that I need to use?

Private Sub GetWordStatus()

        Dim wordDoc As Word.Document
        Dim wordAppl As Word.Application
        Dim mydoc As String
        Dim myAppl As String

        'now check if Word is running
        If Not IsRunning(wordAppl) Then
            MessageBox.Show("Word is not running -> will create a new instance of Word. ")
            Set wordAppl = CreateObject("Word.Application")
            Set wordDoc = wordAppl.Documents.Open(mydoc)
        Else
            MessageBox.Show("Word is running -> will get the specified document. ")
            ' Bind the wordDoc variable to a specific Word document
            Set wordDoc = GetObject(mydoc)
        End If

    End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,663 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 51,341 Reputation points
    2022-05-03T14:52:47.393+00:00

    That is trying to use the Office PIA. In your project's references add a COM reference (under your dependencies) to Microsoft Word xxx Object Library. This will add the appropriate namespace (Word) and objects to the code so the above should compile.