reference to use with VB.Net to automate Word

K

Kevin

If I want to use VB.Net, and to use Word automation with various Word
versions later than Word 97, what Reference do I need to set in my
VB.Net project? Since I want to access various versions of Word, I
assume I have to use COM and not just a PIA specific to a particular
version of Word.

Within VB.Net, what should I set my Reference at to allow me to access
Word.Application's methods and properties (early binding, which I'd
like to use at design time) and to the Object (late binding, for
compiling for production)?
Thanks.
Kevin
 
C

Cindy M -WordMVP-

Hi Kevin,
If I want to use VB.Net, and to use Word automation with various Word
versions later than Word 97, what Reference do I need to set in my
VB.Net project? Since I want to access various versions of Word, I
assume I have to use COM and not just a PIA specific to a particular
version of Word.

Within VB.Net, what should I set my Reference at to allow me to access
Word.Application's methods and properties (early binding, which I'd
like to use at design time) and to the Object (late binding, for
compiling for production)?
Rule of thumb: if developing for multiple versions, always program
against the oldest version. Then test in the newer versions in case MS
"broke" something. (Doesn't happen often, but it does happen.)

That means you should install Word 2000 on the development system and,
if you want to use early binding and Intellisense, set references to the
Word 9.0 library. Visual Studio will then create a set of IAs and save
them in your solution's folder. You need to distribute them with your
solution. (So even if you install it on a machine with Word 2003, for
example, your solution will continue to reference the IAs for Word 9.0.)

If you use late-binding, then you don't set any references; no IAs will
be generated. You should still develop and test on a machine with Word
2000, however. In order to keep life simple, in this case, use Option
Strict OFF (not ON).

For the scenario you describe, I'd set up a project with references.
Then create a second project with NO references and copy the module(s)
from the first one into it. Trying to get rid of the references and IAs
could be tricky.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
K

Kevin

Cindy said:
Hi Kevin,

Rule of thumb: if developing for multiple versions, always program
against the oldest version. Then test in the newer versions in case
MS "broke" something. (Doesn't happen often, but it does happen.)

That means you should install Word 2000 on the development system
and, if you want to use early binding and Intellisense, set
references to the Word 9.0 library. Visual Studio will then create a
set of IAs and save them in your solution's folder. You need to
distribute them with your solution. (So even if you install it on a
machine with Word 2003, for example, your solution will continue to
reference the IAs for Word 9.0.)

If you use late-binding, then you don't set any references; no IAs
will be generated. You should still develop and test on a machine
with Word 2000, however. In order to keep life simple, in this case,
use Option Strict OFF (not ON).

For the scenario you describe, I'd set up a project with references.
Then create a second project with NO references and copy the
module(s) from the first one into it. Trying to get rid of the
references and IAs could be tricky.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)

Thank you, Cindy. What is the purpose of what you describe in your last
paragraph ("For the scenario you describe...")? What does this
accomplish?

If I use late binding, and don't set any references, how do I refer to
the properties and methods of the Word object--could you give a brief
example?

I'm still unclear as to the part COM plays in this. Could you talk a
little about it?

Thanks. Kevin
 
C

Cindy M -WordMVP-

Hi Kevin,
Thank you, Cindy. What is the purpose of what you describe in your last
paragraph ("For the scenario you describe...")? What does this
accomplish?

If I use late binding, and don't set any references, how do I refer to
the properties and methods of the Word object--could you give a brief
example?

I'm still unclear as to the part COM plays in this. Could you talk a
little about it?
I quote from your original message:

" (early binding, which I'd
like to use at design time) and to the Object (late binding, for
compiling for production)?"

COM is always part of the package, because you're automating a COM-based
application (in a COM environment). The Interop Assemblies (IAs) are the
interface between .NET and COM type libraries. A "string" in VBA-world is
not exactly the same as a "string" in .NET-world, for example. In .NET
terms, the IAs take on the task of "marshalling" the commands between
..NET and COM (you can search "marshalling" to get a lot more tech
detail).

When you set a reference to COM in a .NET project, this requires the
presence of IAs. If none can be found, either in the local folder or the
GAC, Visual Studio generates an IA for you for each COM library, and
these references are in your solution assembly.

So, unlike a VBA project, it's not quite so simple a matter to "remove
the reference". Thus, my suggestion to create a second solution with NO
reference, copy the development code from the referenced project into it,
and there change everything from typed variables (early binding) to
"objects" (late binding).

You'd start with this
Dim wdApp as Word.Application = New Word.Application
Dim wdDoc as Word.Document = wdApp.Documents.Add()
wdApp.Visible = True

Copy it into the "production solution" and change it to this
Dim wdApp as Object = CreateObject("Word.Application")
Dim wdDoc as Object = wdApp.Documents.Add()
wdApp.Visible = True

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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