Reference to Word

P

pk

Hello. I hope someone can help me.

I am actually working on a project to automate Word from
Excel. I can start a new instance of Word, open files,
etc., but:

1. After the Word App is instantiated, my code does some
processing. Then it switches to Excel. Later, in a new
procedure, how do I re-establish a reference to Word again
(i.e. if the App is already running)?

2. If there is more than one instance running, how can I
determine in VBA which instance is the one my program
started and which is an instance the user started/already
had open?

Please note that my program will always start a new
instance of Word and work with that instance.

Your example code is what I need the most. Thank you very
much for your assistance.
 
J

Jonathan West

Hi pk,


pk said:
Hello. I hope someone can help me.

I am actually working on a project to automate Word from
Excel. I can start a new instance of Word, open files,
etc., but:

1. After the Word App is instantiated, my code does some
processing. Then it switches to Excel. Later, in a new
procedure, how do I re-establish a reference to Word again
(i.e. if the App is already running)?

You need to maintain the object variable that contains the reference to
Word. This article will help you understand what is going on

Control Word from Excel
http://www.mvps.org/word/FAQs/InterDev/ControlWordFromXL.htm
2. If there is more than one instance running, how can I
determine in VBA which instance is the one my program
started and which is an instance the user started/already
had open?

By keeping the object variable within scope.
Please note that my program will always start a new
instance of Word and work with that instance.

The example code in the article uses GetObject to grab an existing instance
if Word is already running, and then uses CreateObject if Word isn't
running. You should modify the code to remove the use if GetObject, and
instead immediately create a new instance of Word using the CreateObject
command.
 
M

Malcolm Smith

Basically when you do:

Set oWord = New Word.Application

you have a pointer to your instance of Word. Then all you do is work with
that pointer, examples (this isn't runnable code, but examples of
statements which points to the oWord reference):

set oDoc = oWord.Documents.Add
oWord.Selection.Range.Text = "Hello World"
oWord.Bookmarks.Add "bmkExample", oWord.Selection.Range
nDocuments = oWord.Documents.Count

and so on

- Malc
www.dragondrop.com
 

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