Macro : switch between excel and word

T

Tom

Hi,

I'm using this macro :

Range("A1").Select
Selection.Copy

Const wdAlignParagraphCenter = 1

Set WordApp = CreateObject("Word.Application")
With WordApp
.Visible = True
.Documents.Add

With .Selection
.Paste
.ParagraphFormat.Alignment =
wdAlignParagraphCenter
.PageSetup.TopMargin = 36

End With


End With
Sheets("Quotation").Select

Now I want to make a new macro that does the same, but
not opens Word but switches to word when it is already
open. (it has to create a new doc in word that is already
open)

Is this possible ?
How can I do this ?

Thx in advance!
Tom
 
N

NickHK

Tom,
If it is your instance of Word created before, pass your reference e.g.
WordApp.
If you want any instance of Word that happens to be running, use GetObject.

NickHK
 
T

Tom

NickHK

If I change : Set WordApp = CreateObject
("Word.Application") into
Set WordApp = GetObject("Word.Application")
and run the marco when word is already open, it doens't
work.

what do you mean with : pass your reference e.g.

Thx !
Tom
 
N

NickHK

Tom,
Read the help on GetObject
Hint: Set WordApp = GetObject(, "Word.Application")

If you creating your own instance of Word you will have
Set WordApp = CreateObject ("Word.Application")

You can then pass/use WordApp in other code outside of your initial routine,
if the object variable is defined with sufficient scope e.g. in a .bas.
When you finished all your processing in various routines, close/quit Word
and finally set your WordApp to nothing.

NickHK
 

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