Friday, August 17, 2012

Spell Checking & Correcting Form Captions

Here is a little something that is asked just now inside foxite forum, i.e., how to get the captions of each objects inside a form, use Word's Spell Checking feature to correct those, and then update the object's captions with the corrected one.

While I do not know why this is needed, well we try to give what they request.  So here is a sample of how to do exactly just that via word automation:



Close Databases All
Local lcFile, lcWord, lcCaption
Create Cursor junk (cID c(30), cObject c(30), oldCaption c(30), newCaption c(30))
lcFile = Getfile('scx')
Use (m.lcFile) Alias xform In 2

*Check if Escape key is not pressed or selection cancelled
If !Empty(m.lcFile)
      * create a word document
      Public oWord As Word.Application
      lcWord = Addbs(Getenv("TMP"))+Sys(3)
      Strtofile('',m.lcWord)
      oWord = Createobject("word.application")
      oWord.DisplayAlerts =.F.
      oWord.Documents.Open(m.lcWord)

      * Get objects of form with captions
      Select xform
      Scan
            If 'Caption' $ properties
                  lcCaption = Strextract(xform.properties,'Caption = "','"')
                  Insert Into junk Values (xform.UniqueID, xform.ObjName,m.lcCaption,CheckSpell(m.lcCaption))
            Endif
      Endscan

      * close word
      oWord.ActiveDocument.Save
      oWord.Quit

      * show collected captions
      Select junk
      Go Top

      * Check/Review/Change More if you like
      Browse Normal
      * Replace or Not?
      If Messagebox('Proceed with updating captions?',4+32,'Yeh baby!') = 6
            Scan
                  Replace properties With Strtran(properties,Alltrim(junk.oldCaption),Alltrim(junk.newCaption)) ;
                        For UniqueID = junk.cID In xform
            Endscan
            Close Databases All
            Messagebox('Done!')
      Else
            Messagebox('What?!!!!')
      Endif
Endif

*******
Function CheckSpell(m.lcCaption)
      ********
      Try
            _Cliptext = m.lcCaption
            oWord.Selection.Paste
            oWord.ActiveDocument.CheckSpelling()
            oWord.Selection.wholestory()
            oWord.Selection.Cut
      Catch
      Endtry
      Return _Cliptext
Endfunc



This maybe is still crude because I just created this now and I am really about to go home.  So if you find something wrong, just adjust it on your end; and better post here some adjustments by way of comment.  I don't know if this will be useful to anyone other than the OP in the forum, but hey, the goal of this is to share ideas and to show how things can be done.

Cheers!

P.S.  Try on test forms first before actually using on real forms.  If you do use on real forms, back it up first.  I won't hold any responsibility on form corruption or loss.