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

Word Automation from VFP 3

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I would like to include a command button within a VFP application, so that the user can refer to a .doc file (just one per customer) which he can maintain in whatever way he likes, and can bring it up when he clicks on a button which I will add to the form.

I have Tamar’s helpful book ‘Microsoft Office Automation with Visual Foxpro’.

The filename will be ‘XtraCust’ + <Cust account> + '.doc'. So perhaps xtracustA003.doc It will be saved in a folder xtra within the user folder for the system.
When he clicks on the button I will check whether the file exists; otherwise I will fire up Word and open the document.

But this is some way in the future! At present I am trying to get Word to fire up at all (and be visible) from VFP. So as a first test I am trying to see if I can open an existing Word file. This is my code :

Code:
SET STEP ON
oWord = CREATEOBJECT("Word.application")
oword.documents.open("C:\Stage2\progress.doc")

I had hoped that the first line would create a Word session and display it. It certainly does something. The hourglass icon is displayed for maybe 2-3 seconds. And when I look at the taskbar, there is a Microsoft Word session. But no new window appears on the screen.

When I execute the second, ‘Microsoft Word’ appears in the task window (with two subtasks, ‘Artificial Intelligence (AI) host’ and ‘File in use’).
And if I try to switch back to my VFP session, this window pops up.
Word_Error_u8guyo.png


Grateful for any suggestions - Andrew
 
All objects instantiated with CreateObject are "invisible", you have to add code to make it visible. IOW
[pre]oWord.visible = .T.[/pre]

 

IMHO, C:\Stage2\progress.doc not is MS Word document and MS Word show messagebox with error meesage or conversion dialog in hidden main word window.

mJindrova
 
To summarize

since you have word open already with the document loaded, your second try leads to another word with the error message file is in use, and as Tore said that's all hidden and the second word therefore also shows a modal error messge dialog that leads to the problem you made a screenshot of.

So first use task manager to close all Word Processes, if you havent even restarted by now. And then do as Tore suggested
Code:
oWord = CREATEOBJECT("Word.application")
oWord.documents.open("C:\Stage2\progress.doc") 
oWord.visible=.t.

Chriss
 
Thanks Tore - Quicker than a speeding bullet !

That certainly makes a difference. The Word window comes up and is displayed - I will continue testing.

Andrew
 
For what it's worth, the following code is my generic routine for instantiating Word. It takes care of the various possibilities, such as Word already being instantiated in the application, or it having been opened interactively by the user. It is intended to be used on a form that has a property, named oWord, which is used to hold the object ref. to Word:

Code:
* Try to instantiate Word. If successful, return .T.

LOCAL loWord, llError, llJunk

IF NOT ISNULL(thisform.oWord)
    * Word has been instantiated ...
    TRY
        llJunk = thisform.oWord.Visible
    CATCH
        * ... but an attempt to access its PEMs has failed;
        * this indicates that the user closed Word interactively
        * since it was last instantiated; the solution is to
        * clear out the object reference so that we can instantiate
        * it again.
        thisform.oWord = NULL
    ENDTRY
ENDIF
    
IF ISNULL(thisform.oWord)
    * Word not yet instantiated (in this form). So first try to get
    * a reference to an existing Word session
    
    TRY
        llError = .f.
        loWord = GETOBJECT(,"word.application")
    CATCH
        llError = .t.
    ENDTRY
    
    IF llError
        * That didn't work, so try for a reference to an existing Word session
        TRY
            llError = .f.
            loWord = CREATEOBJECT("word.application")
        CATCH
            llError = .t.
        ENDTRY
       
        IF llError
            * That didn't work either, so jump out
            RETURN .F.
        ENDIF
       
    ENDIF

    * Successfully opened Word. Store the object ref.
    thisform.oWord = loWord
    
    RETURN .t.
    
ENDIF

Andrew, I'm not saying that this will solve your particular problem, but I thought I would post it in the hope that others will find it useful.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top