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?