Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instance of Word Automate

Status
Not open for further replies.

dotnik

Programmer
Nov 14, 2003
24
IT
Hello to all,
I've starting a program that execute a Word instance and open an (o more) document's model.
How do I can verify if there is already open an instance of MsWord?
For example:
if <instance_open> Then
myWord.Documents.NewDocuments()
else
myWord = CreateObject(&quot;Word.Application&quot;)
Endif
...
Bests regards :)
Nicola
P.S.: I hope the You understand my English :-(
 
dotnik,

Use GetObject() function.

-----------
lError = .F.
On error lError = .T.
oWord = GetObject(, 'Word.Application')
On error

If (vartype(oWord) == 'O')
oWord.Documents.NewDocuments()
else
oWord = CreateObject(&quot;Word.Application&quot;)
Endif
-----------

Hope it helps

-- AirCon --
 
Hi

Major part of the code lifted from Mike's site.
**************************************************
DO doDecl
LOCAL lcAppl
lcAppl = &quot;WORD&quot; && or &quot;EXCEL&quot; or &quot;Calculator&quot;
lnHand = IsRunning(lcAppl)

IF lnHand = 0
* Application not running, so launch it now
** do whatever
ELSE
* Application running, so activate it
BringWindowToTop(lnHand)
ENDIF
**************************************************
FUNCTION IsRunning
* Generic routine to check if a given
* application is running on the user's system.
* Parameter is all or part of the window's title.
LPARAMETER tcTitle
hNext = GetActiveWindow() && current app's window
* iterate through the open windows
DO WHILE hNext<>0
cText = REPLICATE(CHR(0),80)
GetWindowText(hNext,@cText,80) && get window title
IF UPPER(ALLTRIM(tcTitle)) $ UPPER(cText)
* parameter text is present in window title
RETURN hNext
ENDIF
hNext = GetWindow(hNext,2) && next window
ENDDO
* required window not found
RETURN 0
ENDFUNC
**************************************************
PROCEDURE doDecl
DECLARE INTEGER GetActiveWindow IN Win32API
DECLARE INTEGER GetWindow IN Win32API ;
INTEGER hWnd, INTEGER nType
DECLARE INTEGER GetWindowText IN Win32API ;
INTEGER hWnd, STRING @cText, INTEGER nType
DECLARE INTEGER BringWindowToTop IN Win32API ;
INTEGER hWnd
ENDFUNC
**************************************************
:)

____________________________________________
ramani - (Subramanian.G) :)
When you ask VFP questions, please add VFP version.
 
It won't make any difference to your VFP app if the user already has an instance of Word running that was initiated by the user. You won't be able to use automation commands on the instance of Word opened by the user anyway (I don't believe without some serious low level programming; which is well beyond the capabilities of VFP)

Just create a copy of MSWord and bring it to the top of the visible screen stack. You can find an already instance of Word using the methods outlined by others in this thread. If your app finds one, maybe you should inform the user to close the other instance(s) first and then open a new instance. This will avoid any confusion for the user vis-a-vis having multiple instances open on the desktop.


Darrell
 
darrel,

You wrong about it. GetObject() is meant to be used for this.
Try the code I posted. Notice that I didn't use the first parameter in the GetObject.


-- AirCon --
 
Darrell,

Forgot to say this. That is the reason why I trapped it with ON ERROR. Because if there is no WORD instances opened, you will get the error

BTW, I mistyped your name, please accept my apology.

Regards


-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top