Automation of Office programs

W

William LaMartin

I have created a program that allows for the automation of things in Word
documents, like changing the values of DocVariables and the links to Excel
Sheets. I did it using interoperoperatability, where I declare objects as
follows:

Dim oWordApp As New Microsoft.Office.Interop.Word.Application, after adding
a reference to Microsoft.Office.Core and a reference to Microsoft Word. I
could do this since I have the Primary Interop Assemblies installed on my
computer. Everything works fine.

But I then realized that if I install this on a client's computer, in
addition to needing the .Net framework installed, they will also have to
have the Primary Interop Assemblies installed. I would like to avoid
having to install the PIAs , so I went back and tried to access Word the old
fashion way. After adding a Reference to Word, I put the following code in
a subroutine on the form.

Dim WordApp as object
WordApp = CreateObject("Word.Application")
Dim docPath As String
Me.OpenFileDialog1.Filter = "Word files (*.doc)|*.doc"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
docPath = Me.OpenFileDialog1.FileName
End If
WordApp.Visible = True
'MsgBox("App Name = " & oWordApp.Name)
WordApp.Documents.Open(docPath)

This code works, and the Word document is opened. But in writing the code
above, I had no autocompletion, and if I make the WordApp a shared object at
the form level, other subs on the form say that the document is not open
even though it is.

How do you get the above type of code to work as it did in Visual Basic?
 
M

Matthew Dill

I think your problem with autocompletion is due to the way in which you are
declaring the word objects.

Dim WordApp as object
WordApp = CreateObject("Word.Application")

Since you are declaring WordApp as an object VB has no reference to what
type of object it actually is and cannot provide the properties/methods etc
that you are looking for. I've done similar Word automation and I think if
you change your declaration to this:

Dim mobjWordApp As Word.Application

you should gain access to the autocompletion functionality. Hope this
helps.

Matt
 
W

William LaMartin

Even though I have added a reference to the Microsoft Word 11 Object
Library, when I type dim oWordApp as Word. there is no list of available
properties, etc. that pops up as there is when I type Dim oWordApp As New
Microsoft.Office.Interop.Word. In fact if I type dim oWordApp as
Word.Application, I get an error message to the effect that Word.Application
is not defined.

It is as if Visual Studio is forcing me to use the second method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top