Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detecting Word's Invalid Merge Field message from VFP
Message
From
05/04/2007 05:46:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
04/04/2007 13:32:42
Loraine Spaeth
Housing Data Systems, Inc.
Wisconsin, United States
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
01211485
Message ID:
01212366
Views:
31
This message has been marked as the solution to the initial question of the thread.
>>>Can anyone tell me how I can detect messages issued by MS Word when automating a mailmerge from my VFP application?
>>>
>>>I make the Word application invisible while doing the mailmerge and everything works great... usually. The hitch is that we allow our clients to modify the Word documents and sometimes they change or insert a mergefield that is not in the datasource. When this happens, Word issues an "Invalid Merge Field" message and waits for user interaction to resolve any field conflicts. Since we make Word invisible until the merge is all completed, the message gets hidden behind our application and the application appears to be hanging. No one can see that Word has issued a message that requires a response before the next line of code will run. Does anyone know how I can, from VFP, detect that Word has issued a message and either respond to it programmatically or make Word visible so the error can be resolved?
>>>
>>>Here is a (condensed) description of the code:
>>>
>>>* Already created the object ref to Word.application and opened my document
>>>
>>>SELECT (cDataSrcAlias)
>>>
>>>COPY TO (cTxtFilename) TYPE CSV
>>>
>>>loMailMerge = oWordApp.activedocument.mailmerge
>>>
>>>IF nWordVersion < 10
>>> loMailMerge.opendatasource(cTxtFilename,4,0,0,0,0,"","",0,"","",,)
>>>ELSE
>>> * Word XP/10 has additional parameters at the end (last is wdMergeSubtypeWord2000=8)
>>> loMailMerge.opendatasource(cTxtFilename,4,0,0,0,0,"","",0,"","",,,"",0,8)
>>>ENDIF
>>>
>>>loMailMerge.Destination = 0 && 0=SendToNewDocument 'Form Letter1'
>>>
>>>loMailMerge.Execute && Run the mailmerge
>>>
>>>* AT THIS POINT WORD MAY HAVE ISSUED THE 'INVALID MERGE FIELD' MESSAGE AND IS WAITING...
>>>* IF I DO AN ALT-TAB TO GET TO THE MESSAGE WINDOW AND RESOLVE THE MERGEFIELD ERRORS, THEN
>>>* PROCESSING CONTINUES, OTHERWISE IT HALTS UNTIL SOMEONE DOES AN ENDTASK ON THE APPLICATION.
>>>
>>>* I SOMETIMES INSERT TABLES/RECORDSETS AT THIS POINT IN CODE, DEPENDING ON THE LETTER, AND
>>>* I DON'T WANT THE USER TO SEE THIS UNTIL I AM ALL DONE; THEN I MAKE WORD VISIBLE.
>>>
>>>oWordapp.Activate()
>>>oWordApp.WindowState = 1 && wdWindowStateMaximize
>>>
>>>Can you help me to detect and handle the Word message?
>>>Thanks!
>>>
>>>Loraine
>>
>>Loraine,
>>Before merging you can check list of mergefields against your merge fields.
>>Cetin
>
>
>Cetin, I decided to go ahead and pre-check the merge fields. Can you help me find a property for the field object which gives me just the name of the field, without the MERGEFORMAT and other formatting stuff that I get when I use field(x).code.text?
>
>
>LOCAL ARRAY laFields[1]
>LOCAL lnDSFieldcnt, lcBadList
>
>SELECT (cDataSrcAlias)
>
>lnDSFieldcnt = AFIELDS(laFields)
>
>lcBadList = ''
>	 	
>WITH oWordApp.ActiveDocument
>    FOR Each fld In .Fields
>        * check if all mergefields are in the datasource, 59 = wdFieldMergeField
>        IF fld.Type = 59
>            IF ASCAN(laFields, fld.Code.Text,1,lnDSFieldcnt,2,1) = 0
>                lcBadList = IIF(EMPTY(lcBadList),"", lcBadlist + ",") + fld.Code.Text
>            ENDIF
>        ENDIF
>    ENDFOR
>ENDWITH
>
>
>BTW, The reason I check the document fields against the datasource, rather than checking datasource fields against the document fields, is because the datasource usually has more (sometimes quite a few more) fields that what the document may have.
>
>Thank you!
>Loraine

Hi Loraine,
I would do something like:
WITH oWordApp.ActiveDocument
    * check if all mergefields are in the datasource, 59 = wdFieldMergeField
    FOR Each fld In .Mailmerge.Fields
        lcMergeField = GetWordNum(fld.Code.Text,2)
        if ascan(laFields, m.lcMergeField , 1, -1, 1+2+4) = 0 
            lcBadList = IIF(EMPTY(m.lcBadList),"", m.lcBadlist + ",") + m.lcMergeField
        ENDIF
    ENDFOR
ENDWITH
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