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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with getobject()

Status
Not open for further replies.

Nifrabar

Programmer
Mar 16, 2003
1,343
NL
Hi!
According a white paper of Tamar this should be a way to open a known worddoc.
oDocument=GETOBJECT('e:\VFP\Projects\Win_assist\data\Manual.doc','Word.Application')
oDocument.visible = .T.

in fact oDocument=GETOBJECT('e:\VFP\Projects\Win_assist\data\Manual.doc') should do the job already.

For me it fails. Only word has been instanciated.

What is wrong?

KR
-Bart
 
Bart,

There are a couple of issues here.

First, if Word is running, you use this syntax to get an object reference to it:

oWord = GETOBJECT(, "word.application")

Note that lonely comma.

To open a specific document and to get a reference to it, if Word is already running:

oDoc = ;
GETOBJECT('e:\VFP\Projects\Win_assist\data\Manual.doc')

However, you can't then do oDoc.Visible = .T. It's the application, not the document, that has the Visible property. But you can do this:

oDoc.Parent.Visible = .T.

Hope this makes sense.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,
Thanks for explanation.
I think I did understood the sample wrong.
Anyway also still I don't get this to work.
Just a matter of sequence of instrunctions I think.
Should there be a way to open a doc, independent of word already running, useing getobject() or should I use creatobject() for this?
-Bart
 
Bart,

One way of doing that would be use GETOBJECT() with the initial comma, but to error-trap it. If it fails, it means that Word isn't running, so do a CREATOBJECT(), then go ahead and open the document in the usual way.

In other words:

Code:
llFails = .F.
TRY
  oWord = GETOBJECT(, "word.application")
CATCH
   llFails = .T.
ENDTRY
IF llFails
  oWord = CREATEOBJECT("word.application")
ENDIF

This is off the top of my head, and not tested. There might be a better way.

If you are using VFP 8.0 or below, you won't have TRY / ENDTRY, but you can achieve the same thing with ON ERROR.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,
Thansk,
yes this works.
Full code now:
Code:
Local oWord, oDocument

try
	oWord = GETOBJECT(,'Word.Application')
CATCH
	oWord = CreateObject("Word.Application")
ENDTRY 

oWord.Visible = .T.

oDocument = oWord.Documents.Open('e:\VFP\Projects\Win_assist\data\myDoc')

This works OK.
-Bart
 
Alternatively you can use ShellExecute:
Code:
DECLARE INTEGER ShellExecute IN Shell32.DLL INTEGER hndwin,STRING cAction,STRING cFileName,STRING cParams,STRING cDir,INTEGER xShowWin
=ShellExecute(0, 'open', 'e:\VFP\Projects\Win_assist\data\myDoc')

Stewart
 
Bart,

Glad to see it works.

Stewart,

Agreed. ShellExecute() is simpler. Of course, it only opens the document. If Bart also wants to do other things with it from within his program, then he'll have to stick with Automation.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Stewart, thanks for your approach but as Mike points to is true.
This is just the begin of a larger VFP-application which will end up in a fully mailmerge.
KR
-Bart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top