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 Wait Message 2

Status
Not open for further replies.

CDavis

Programmer
May 5, 2000
155
US
(Using VFP 9)

Problem: When automating MS Word, I'm using a document template that is the client's letter head. The template loads more slowly than I'd like and so I set ThisForm.Message.visible = .T. ("Creating Word Document...") I'd like to set .Message.Visible = .F. when the user closes the Word document. How can I test for that condition?

Code:
#DEFINE CR CHR(13)
#DEFINE CR2 CHR(13) + CHR(13)

*** Other Define Statements ***

THISFORM.MESSAGE.VISIBLE = .T.

RELEASE ALL LIKE o*
PUBLIC oWord

LOCAL oWord, oDocument, oRange

oWord = CREATEOBJECT("Word.Application")  && Create Object Reference to MS Word
oDocument = oWord.Documents.ADD("EHMC LETTERHEAD.DOT")  && Create Document Based On Template
oRange = oDocument.RANGE()  

oRange.InsertAfter(CR2 + MDY(DATE()) + CR2)
oRange.InsertAfter(EVAL.Firm + CR)

*** insert Other Data etc.****


oWord.VISIBLE = .T.  && Make the Word Application Visible

*** Test for Word Document Closing prior to next line ****

THISFORM.MESSAGE.VISIBLE = .F. && without wait message is never visible.


I'm hoping that there is a better way than a WAIT command.

Thanks in advance for your help.

Chuck
 
Code:
oWord.VISIBLE = .T.  && Make the Word Application Visible
DO WHILE TYPE("oWord.Name") == "C"
   Sleep(1000)
   DOEVENTS
ENDDO

THISFORM.MESSAGE.VISIBLE = .F. && without wait message is never visible.

NOT TESTED!!!!!!

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Or, do what Borislav suggested, but do it in a timer rather than a DO WHILE loop. Set the timer interval to say, 1000 ms. When the timer fires, test to see if oWord is NULL. If it is, assume the user has closed the instance of Word.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Create an eventhandler and bind to word's ApplicationEvents2_Quit event. Here is a sample (originally posted to universalthread):

Code:
lcGetFile = getfile('DOC')

local oword as 'word.application'
oWord = newobject('word.application')
oWordEvents = newobject("WordEvents",'','',oWord)
eventhandler(oWord,oWordEvents)
with oword
  .Documents.open(m.lcGetFile)
  .visible = .t.
  .activate
endwith


define class WordEvents as session olepublic
  implements ApplicationEvents2 in "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\MSWORD.OLB"

  oHook = null
  procedure init(loHook)
    this.oHook  = loHook
  endproc

  procedure ApplicationEvents2_Quit() as VOID
    ? 'User is quitting word',datetime()
    eventhandler(this.oHook,this,.t.)
  endproc

  procedure ApplicationEvents2_DocumentChange() as VOID
  endproc

  procedure ApplicationEvents2_DocumentOpen(doc as VARIANT) as VOID
  endproc
  procedure ApplicationEvents2_DocumentBeforeClose(doc as VARIANT, cancel as LOGICAL) as VOID
  endproc
  procedure ApplicationEvents2_DocumentBeforePrint(doc as VARIANT, cancel as LOGICAL) as VOID
  endproc
  procedure ApplicationEvents2_DocumentBeforeSave(doc as VARIANT, SaveAsUI as LOGICAL, cancel as LOGICAL) as VOID
  endproc
  procedure ApplicationEvents2_NewDocument(doc as VARIANT) as VOID
  endproc
  procedure ApplicationEvents2_WindowActivate(doc as VARIANT, Wn as VARIANT) as VOID
  endproc
  procedure ApplicationEvents2_WindowDeactivate(doc as VARIANT, Wn as VARIANT) as VOID
  endproc
  procedure ApplicationEvents2_WindowSelectionChange(Sel as VARIANT) as VOID
  endproc
  procedure ApplicationEvents2_WindowBeforeRightClick(Sel as VARIANT, cancel as LOGICAL) as VOID
  endproc
  procedure ApplicationEvents2_WindowBeforeDoubleClick(Sel as VARIANT, cancel as LOGICAL) as VOID
  endproc
enddefine



Cetin Basoz
MS Foxpro MVP, MCP
 
Borislav, Mike, and Cetin,

Thanks for taking a look at this with me. I've not taken the time to try Cetin's suggestion as he posted just before me.

Mike's timer solution works well. I've posted code snippets below. I don't know why it works better but calling a form method to set the message visible and to enable the timer event works better than setting those directly with my other code.

Code:
ThisForm.SetMessage()
*
*Code as in my original post
**



***SetMessage Method
Code:
THISFORM.MESSAGE.VISIBLE = .T.
ThisForm.cdtimer1.Enabled = .T.

****Timer Event***
Code:
IF THISFORM.MESSAGE.VISIBLE = .T.
	THISFORM.MESSAGE.VISIBLE = .F.
ELSE
	THISFORM.MESSAGE.VISIBLE = .T.
ENDIF

IF TYPE("oWord.Name") == "U"
	THISFORM.MESSAGE.VISIBLE = .F.
	THIS.ENABLED = .F.
ENDIF

I tested a number of variants for oWord being null but none of them worked and some gave me error messages.

It seems to be a good indication that Word has closed when oWord.Name goes undefined.

Another interesting item to note: I attempted to flash the Message by setting .visible to its opposite state each time through the timer event. I never happens -- I guess once the call is made to Word that the timer event doesn't fire.

I'm satisfied with the end result-- so thanks again for the help.
 
Glad to hear you've got it working, Chuck.

You said:

I attempted to flash the Message by setting .visible to its opposite state each time through the timer event. I never happens

You might need to execute DOEVENTS just after toggling the label's visibility, or alternatively, pause for few millisecs (using Sleep() or Inkey()). That will give VFP time to eat the events and make the label visible.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
What I provided doesn't rely on a timer nor a property of variable you are keeping 'alive' (which I don't suggest). It merely binds to word's events.

Cetin Basoz
MS Foxpro MVP, MCP
 
Cetin,

I would like to implent your solution, however facing a problem: how to determine for the wordevents for office11 / office12 or office13 or in other words how to determine where msword.olb is installed? I would like to make your routine generic.

F.y.i. for instantance on my win7 configuration I have mwWord.OLB installed on C:\Program files(x86)\Microsoft Office\Office12.

Regards,

Jockey(2)
 
Jockey,
You only need the olb on your development machine.

Cetin Basoz
MS Foxpro MVP, MCP
 
Cetin,

All clear. Thanks.

Would you also be able to tell me also which one is favorite to use (a/b/c/d) ?


*a)
Code:
DEFINE CLASS WordWrapper AS custom 
  IMPLEMENTS ApplicationEvents2 in {00020905-0000-0000-C000-000000000046}#8.1
*b)
Code:
DEFINE CLASS WordWrapper AS SESSION Olepublic
  IMPLEMENTS ApplicationEvents2 in "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\MSWORD.OLB"
*c)
Code:
DEFINE CLASS WordWrapper AS custom
  IMPLEMENTS ApplicationEvents2 in "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\MSWORD.OLB"
*d)
Code:
DEFINE CLASS WordWrapper AS SESSION Olepublic
   IMPLEMENTS ApplicationEvents2 in {00020905-0000-0000-C000-000000000046}#8.1

Regards,

Jockey(2)
 
Last one has the advantage of using Word's unique ID. I am not sure about #8.1 part. I actually write the initial version of class using the object explorer (IOW VFP writes it for me, I then edit it) - drag & drop ApplicationEvents2 from object explorer window on to a code window.

Cetin Basoz
MS Foxpro MVP, MCP
 
Cetin,

Thanks,

for time being I will use d) also noticed on my system (XP/Word2007) there is no difference wether using 8.1, 8.2, 8.3 or 8.4 after the # sign. :)

Now puzzling to combine your eventhandler procedure and an mailmerge facility with option to change the template before the actual mailmerge. Your eventhanldler coding seems to be a good starting point.

I suppose, If I face unbeatable problems it is allright to continue this topic?

Regards,

Jockey(2)


 
Probably. This forum is a little hard to track. I may miss messages (if you meant that I would participate too:)

I think I also posted a mail merge code here.

Cetin Basoz
MS Foxpro MVP, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top