Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Allow user to modify a Word document?
Message
From
17/01/2009 07:00:28
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01372571
Message ID:
01374630
Views:
77
I added code to the ApplicationEvents2_DocumentBeforePrint and ApplicationEvents2_DocumentBeforeSave methods in WordWrapper.prg to display a message and set Cancel = .t. I expected it to prevent the default actions of Save and Print but the code doesn't execute. What am I doing wrong?

I don't know. I just tested this and it worked fine for me in Word 2003 - I do not have 2007 installed anywhere so I cannot test with it. When I "saved" the document in Word, it initially appeared that Word had saved the document, but it really didn't because when I closed Word, it asked me if I wanted to save my changes. And when I tried to print, Word, did not print the document. Here is my test Handler class. To run it using Word 2007, just change the version number (8.3) to the one for 2007. It is probably 8.4, but if you want to verify, just go into the IntelliSense manager and add the Word 2007 Type Library under the types tab. Then, double click on it and see what is in the Data memo field. This should work as long as Microsoft hasn't changes the interface.
DEFINE CLASS WordHandler AS session OLEPUBLIC

  IMPLEMENTS ApplicationEvents2 IN {00020905-0000-0000-C000-000000000046}#8.3

  PROCEDURE ApplicationEvents2_Quit() AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_DocumentChange() AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_DocumentOpen(Doc AS VARIANT) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_DocumentBeforeClose(Doc AS VARIANT, Cancel AS LOGICAL) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_DocumentBeforePrint(Doc AS VARIANT, Cancel AS LOGICAL) AS VOID
    MESSAGEBOX( "In DocumentBeforePrint()...")
    CANCEL = .T.
  ENDPROC

  PROCEDURE ApplicationEvents2_DocumentBeforeSave(Doc AS VARIANT, SaveAsUI AS LOGICAL, Cancel AS LOGICAL) AS VOID
    MESSAGEBOX( "In DocumentBeforeSave()...")
    CANCEL = .T.
  ENDPROC

  PROCEDURE ApplicationEvents2_NewDocument(Doc AS VARIANT) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_WindowActivate(Doc AS VARIANT, Wn AS VARIANT) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_WindowDeactivate(Doc AS VARIANT, Wn AS VARIANT) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_WindowSelectionChange(Sel AS VARIANT) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_WindowBeforeRightClick(Sel AS VARIANT, Cancel AS LOGICAL) AS VOID
  * add user code here
  ENDPROC

  PROCEDURE ApplicationEvents2_WindowBeforeDoubleClick(Sel AS VARIANT, Cancel AS LOGICAL) AS VOID
  * add user code here
  ENDPROC

ENDDEFINE
And here is the code to use the handler class:
oWord = CREATEOBJECT( "Word.Application" )
oHandler = NEWOBJECT( "WordHandler", "WordHandler.prg")
?EVENTHANDLER( oWord, oHandler )
oWord.Documents.Open( FULLPATH(CURDIR()) + "TestWord.DOC" )
oWord.Visible = .T.
HTH
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform