Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Prevent Save in Word?
Message
From
01/03/2007 10:39:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
01198988
Message ID:
01199997
Views:
32
>I have developed a Word automation process that has been running for several years. It (1) opens a template, (2) copies the text, (3)creates a new blank document, (4) pastes the copied text, (5) closes the template, (6) merges data, (7) calculates the number of copies needed, (8) prints them and (9) closes Word without saving the document. This is all invisible to the user.
>
>Now the client needs the ability to occasionally edit one of the documents so the automation process needs to be interrupted prior to step 7--and then ideally resumed and completed. I can use oWord.visible= .t. and oWord.Activate() to allow them to edit, but is there a way to get control back after the editing so I can continue the automated process?
>
>The natural tendency is for the user to print the document manually and then close Word. If they do that, I've lost the refrence and the printing can't take place. I'd also prefer that the document not be saved, or at least that I be able to control the name so that they don't overwrite the template. Any suggestions?

Lynda,
Probably didn't understand why you need to intercept. If it's based on a template then the template is intact whether or not they save it with changes to the document opened (or are you directly opening the template?! If so simply open a new document based on template instead). When they're editing themselves they could decide number of copies, print etc and close.

Assuming you still badly need to intercept, good news is that you can do that since VFP7 (or 6 with VFPCOM?). You could make the word visible and let them edit while silently controlling any action if you want to from VFP at background. You can say if they selected something, check what they selected, if selection contains "fox" replace it with "visual foxpro" etc. You can prevent save and saveas, or whatever they do to save and whatever name under you could instead save with a known path and filename. You can add toolbars and buttons, respond to their click event etc. Possibilities are virtually endless. Here are some samples:
clear
lcGetFile = Getfile('DOC')
oWordEvents=Newobject("WordEvents")

Local owrd As 'word.application'
owrd = Newobject('word.application')
Eventhandler(owrd,oWordEvents)
owrd.Documents.Open(m.lcGetFile)
owrd.Visible = .T.
owrd.Activate

Define Class WordEvents As Session OlePublic
  IMPLEMENTS ApplicationEvents2 IN "C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE11\MSWORD.OLB"

  MyDocName = "c:\temp\VFPWasHere.doc"
  Host = null

  PROCEDURE init(oHost)
    this.host = oHost
  ENDPROC

  Procedure ApplicationEvents2_Quit() As VOID
    * Should we even save it to a cursor and erase local file?
    * in case a save is attempted in between
    IF FILE(this.myDocName)
      Create Cursor myCursor (doc m nocptrans,dt t)
      Insert into myCursor (doc,dt) values (FileToStr(this.myDocName),Datetime())
      *Erase (this.myDocName)
    endif
    ? 'Quitting word...',Datetime()
    IF TYPE('this.Host')='O' AND !ISNULL(this.host)
      Eventhandler(this.Host,this,.t.)
      this.Host = .null.
    endif
  Endproc
  Procedure ApplicationEvents2_DocumentChange() As VOID
    ? 'Document change detected...',Datetime()
  Endproc
  Procedure ApplicationEvents2_DocumentOpen(Doc As VARIANT) As VOID
    ? 'Document opened...'+Doc.FullName,Datetime()
  Endproc
  Procedure ApplicationEvents2_DocumentBeforeClose(Doc As VARIANT, Cancel As LOGICAL) As VOID
    ? 'Document closing...'+Doc.Fullname,Datetime()
    *!*	    * If current file name is different then ours
    *!*	    * save it to where we want it
    *!*	    * else do not even save
    *!*	    If !( Upper(Trim(Doc.Fullname)) == Upper(Trim(this.myDocName)) )
    *!*	      Doc.Saveas(this.myDocName)
    *!*	      Doc.Saved = .t.
    *!*	    endif
  Endproc
  Procedure ApplicationEvents2_DocumentBeforePrint(Doc As VARIANT, Cancel As LOGICAL) As VOID
    ? 'Document is about to print...',Datetime()
    * Cancel the printing so user calls IT staff:)
    Cancel = .t.
  Endproc
  Procedure ApplicationEvents2_DocumentBeforeSave(Doc As VARIANT, SaveAsUI As LOGICAL, Cancel As LOGICAL) As VOID
    LOCAL lcUserDocName
    lcUserDocName = Doc.FullName
    Cancel = .T.
    ? 'Attempted save..Cancelled save...',Datetime()
    Local loBalloon,lVisible
    loBalloon = Doc.Application.Assistant.NewBalloon
    lVisible  = Doc.Application.Assistant.Visible
    Doc.Application.Assistant.Visible = .T.
    With loBalloon
      .Heading = "Sorry. VFP doesn't let you to save '" + m.lcUserDocName + "'"
      .Show()
    ENDWITH
    Doc.Application.Assistant.Visible = m.lVisible
  Endproc
  Procedure ApplicationEvents2_NewDocument(Doc As VARIANT) As VOID
    ? "User created a new document..."+Doc.FullName,DATETIME()
  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
    IF sel.start < sel.end
      ? Datetime(),'User selected text:',sel.text
      * Change selected text if it contains " fox " - regexp for more fuzzy search/replace
      * If within a table cell would cause some nasty result-but this is just a sample
      sel.text = STRTRAN(sel.text," fox "," Visual FoxPro ",1,-1,1)
    endif
  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
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform