user2827024
user2827024

Reputation:

VS 2013 Desktop C# Automating Word 2007

I've gone through the hits in the search and can find no relief.

I'm trying to populate an Avery template with data. I've found several good examples, but they all have a hitch.

Each one begins with code like:

Dim objApp As Word.Application
Dim objDoc As Word.Document
objApp = New Word.Application()
objDoc = objApp.Documents.Open("//Path of a file to Open")

I add the word 2007 reference (Microsoft Word 12.0 Object Library) to the project and the Word object does not exist, so the first two lines won't compile.

Am I getting the right object library for Word 2007? It's the only version I have installed.

Am I missing dll's that might show up by re-installing Word?

Any other ideas?

Upvotes: 1

Views: 462

Answers (2)

user3165438
user3165438

Reputation: 2661

You use C# as the developing language (you mentioned it in the tags you've attached to the post)

But, in the code you post you use the Visual Basic syntax. That's why you cannot compile the first 2 lines.

Instead of

Dim objApp As Word.Application
Dim objDoc As Word.Document

Use

 Word.Application objApp ;
 Word.Document objDoc ;

Put attention to locate a ; character at the end of each line.

Upvotes: 0

Markus
Markus

Reputation: 761

The best way to do this, in my opinion, is to use the primary interop assemblies provided by Microsoft. When you set a reference to the COM libraries, Visual Studio creates an interop assembly for you. Presumably, automatically generated interop assemblies won't be as clean as those generated by a developer specifically for an application.

There is information about the primary interop assemblies here:

 http://www.microsoft.com/en-us/download/details.aspx?id=18346
 http://msdn.microsoft.com/en-us/library/15s06t57.aspx

EDIT: Once you have set a reference to the Microsoft.Office.Interop.Word interop assembly, you can reference it like this at the top of your class files:

 using Word = Microsoft.Office.Interop.Word;

Then, you can refer to it in your code like this:

Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open("filename to open");

Upvotes: 2

Related Questions