1
votes

I am unable to open a Microsoft Word document in vb.net. I have the following imports:

Imports Microsoft.Office
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Word

When I try to call Word in my code I get the following error:

Type 'Word.Application' is not defined

Dim appWord As New Word.Application
Dim docWord As New Word.Document

Do I need to download a library or something else?

2
As long as you have Microsoft Word Object Library (COM reference) added to your project references, this should work fine. I don't have any issues doing the same thing.RianBattle
I have added the (COM reference) and it works fine now. Thank you!Brad Moulder
Usually, you need to have Microsoft Office or at least Word installed on the PC that you're developing on and the machine that you will install the software on.Paul

2 Answers

2
votes

If you have

Imports Microsoft.Office.Interop.Word

but not

Imports Microsoft.Office.Interop

it will report Word.Application as not defined.

So either change the import to

Imports Microsoft.Office.Interop

Or change the instantiator to

Dim appWord as New Application
1
votes

You need to add a reference to the Microsoft Word Object Library (found under Project Settings -> References -> Add New Reference, then selecting the COM reference tab).

Doing this, in addition to your current Imports statements, should take care of it.

Note that after adding the reference, while still in your references page of your project properties, you can scroll through the imported namespaces list and check the checkbox next to Microsoft.Office.Interop.Word and not have to add the Imports statements (although it's still a good idea to use them, for readability).

Also, as Paul mentioned in the comments above, you will need Word installed on the machine you are developing on and on any machine running your program (can't open Word if it isn't there!).

How to automate Word from Visual Basic .NET to create a new document