Welcome! Log In Create A New Profile

Advanced

[WD12] Word automation

Posted by Louis Verbraak 
Louis Verbraak
[WD12] Word automation
April 09, 2009 06:36PM
Hello to you all,

I'm trying to start with word automation. Now I've the some questions. This is the code I'm using within a class:

// Is Word actif or should it be started.
:m_oWordObject=GetActiveObject("Word.Application")
IF :m_oWordObject=Null THEN
// Word needs to be started.
:m_oWordObject=new object OLE "Word.Application"
END

:m_sDocumentName="C:\Temp\MyDocument.doc"
:m_oWordObject>>Documents>>Open(:m_sDocumentName,OLEFalse,OLEFalse)

Info("Now my document is loaded")

When I start Word at this moment and open some other documents, the active document is changed.

When I return to my program and I want to close my original document, how can I make that document the active one. I know I can do something like:

:m_oWordObject>>Windows(1)>>Activate

but I don't know what number it is. So I've tried with ':m_oWordObject>>Windows(:m_sDocumentName)>>Activate' but that didn't work as well.

Can anybody tell me how I can make my own document the active one, or how I should do this another way.

The second question I want to ask is: how can I retrieve the number of opened documents in Word.
I found this one: :m_oWordObject>>Documents>>Count



Edited 1 time(s). Last edit at 04/09/2009 07:01PM by Louis Verbraak.
Merijn van der Wijk
Re: [WD12] Word automation
April 09, 2009 11:10PM
Louis,

Can you give the code below a try:

iDocNr is int
FOR iDocNr=1 TO :m_oWordObject>>Documents>>Count
    IF :m_oWordObject>>Windows(iDocNr)>>Name=:m_sDocumentName THEN
        :m_oWordObject>>Windows(iDocNr)>>Activate
    END
END
HTH Merijn
Louis Verbraak
Re: [WD12] Word automation - THANKS
April 10, 2009 09:01AM
Hello Merijn,

Thanks for your suggestion. It works, I only needed to change the following.

IF :m_oWordObject>>Documents(iDocNr)>>FullName=:m_sDocumentName THEN
:m_oWordObject>>Documents(iDocNr)>>Activate
END

Thanks again.
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: