question

JeffStiegler-8414 avatar image
0 Votes"
JeffStiegler-8414 asked JeffStiegler-8414 commented

Word.Document not defined


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


dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered JeffStiegler-8414 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx,

I added the reference but I am still missing something because I still get the error.

198585-reference.png


0 Votes 0 ·
reference.png (11.9 KiB)
cooldadtx avatar image cooldadtx JeffStiegler-8414 ·

Quick action the identifier (or right click on it) and you should see an option to add a using Microsoft.Office or similar. Add the namespace using and the error should go away.

0 Votes 0 ·