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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP 6 Word - Using a template for labels 1

Status
Not open for further replies.

eric43

Programmer
Apr 29, 2005
94
AU
I am trying to do a Mail Merge type operation.

I have a table - Contacts - with 6 fields.
I have created a template in MSWord for 12 labels on a page.

I create contactlabels.doc based on template.

I have fields for each line of the table.

All other aspects of my code are working ok and I see the template on screen as I step through.

I am using the following ( borrowed from foxite ) code to try and populate the fields.

Code:
PROCEDURE create
* THIS is the code handling the template and creating
* the document.
    LOCAL loApp, loDoc
    loApp=THIS.WordApplication
    loDoc=THIS.WordDocument
    loDoc.SaveAs(THIS.ReportFile)
    local lcFoundText, lcCommand
    loApp.Selection.Find.Execute('\<*\>',,,.T.,,,.T.,1)
    lcFoundText=loApp.Selection.Text

    DO WHILE SUBSTR(lcFoundText,1,1)='<' AND ;
      SUBSTR(lcFoundText,LEN(lcFoundText),1)='>'

        lcCommand=loApp.Selection.Text
        lcCommand=SUBSTR(lcCommand,1,LEN(lcCommand)-1)
        lcCommand=SUBSTR(lcCommand,2)
        loApp.Selection.Text=EVALUATE(lcCommand)
        loApp.Selection.Find.Execute('\<*\>',,,.T.,,,.T.,1)
        lcFoundText=loApp.Selection.Text

    ENDDO
    loDoc.Save()
ENDPROC

as I step through I see the '<<' - chr (171)- being placed in lcFoundText.

I would then expect to pick up the whole word within the << - >> field markers as lcCommand.

This code does not do that.

Can anyone help with a 'snippet' that will work correctly?

Many thanks

Eric
 
I know this is another approach, but why not let word to the work?
After creating a new mail merge document and selecting labels, then connecting to the database I put the following macro into the mailmerge document

Sub TESTLABEL()
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
End Sub

Then I call it with the run command
run /n c:\program files\microsoft office\office\winword.exe document-name /mtestlabel

wjwjr
 
wjwjr

Thanks for your prompt reply.

I'm not sure I can do that in code for a distributable app?

I don't know how to get the macro into MSWord programmatically.

Eric
 

Eric,

I must say that this seems like a horribly complicated way of doing what should be simple.

If I've understood it right, your code is searching for the mailmerge delimiter characters (<< and >>), highlighting the text between them, doing some sort of substring operation on the text, then writing the contents of the data field back at the highlight position.

I would suggest that you consider two much easier alternatives. First, run the entire mailmerge programmatically. You only need to call two methods: one to set the data source and the other to do the mailmerge. If you need it, I can dig out some code for you to use as a sample.

Alternatively, is there any reason why you absolutely have to do this in Word? I've done a lot of mailmerge stuff with Word, and usually it works well. But when it comes to doing mailing labels, VFP's label designer is very much easier.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Mike,

Thanks for that - as a self taught programmer I'm not always aware of all the things VFP will do.

I shall certainly look at that

regards

erci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top