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

Mail Merge Problem - VFP 9.0 2

Status
Not open for further replies.

KarenJB

IS-IT--Management
Dec 17, 2007
44
US
I can not get this code to work correctly:

#DEFINE wdMainAndDataSource 2
#DEFINE wdSendToPrinter 1
#DEFINE wdDoNotSaveChanges 0
#DEFINE wdWindowStateMinimize 2
oWord=CREATEOBJECT("Word.Application")
oWord.VISIBLE = .F.
oWord.WindowState = wdWindowStateMinimize
oWord.Documents.Open('C:\VFP Projects\PSTek\Reports\WordMail.Doc')
nSave = oWord.ActiveDocument.MailMerge.State
IF oWord.ActiveDocument.MailMerge.State = wdMainAndDataSource
oWord.ActiveDocument.MailMerge.Destination = wdSendToPrinter
oWord.ActiveDocument.MailMerge.DataSource.FirstRecord = 1
oWord.ActiveDocument.MailMerge.DataSource.LastRecord = 1
oWord.ActiveDocument.MailMerge.Execute
ENDIF
oWord.Quit(wdDoNotSaveChanges)
RELEASE oWord
WAIT WINDOW nSave

nSave = 0 and it never executes the merge

If I go and run the WordMail.doc it merges fine. What in the world am I doing wrong? Why does my oWord.ActiveDocument.MailMerge.State evaluate to 0?
 
I have found the piece that I was missing:
oWord.ActiveDocument.MailMerge.OpenDataSource('C:\VFP Projects\PSTek\Reports\LSheet.ODC')

Everything works fine now, but I'd like the person running this code to make sure that it's directed to the correct printer and tray. The way it is set up now it just prints it to your default printer automatically. How can I make it come up with the printer dialog box? And also, please feel free to point out any way that I can improve on the code that I submitted at first. For instance this is working fine on my pc but the application will ultimately end up on a network server which will cause a problem with my current data source.
 
Karen,

I have the same code as you in my mailmerge function, in particular:

oWord.ActiveDocument.MailMerge.Destination = wdSendToPrinter

This correctly shows the printer prompt. The only difference I can is that you are running Word invisibly, which might explain why the prompt is not appearing.

The only other thing I can suggest is that you set oWord.ActivePrinter to the printer name (as returned by VFP's GETPRINTER(), for example).

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks for the reply Mike. I put this app on the network and it does indeed come with the printer dialog box (it still doesn't come up on the development pc - mine). Today's issue is that it only prints out the first page of the labels. I'm not sure it the firstrecord = 1 and the lastrecord = 1 has anything to do with it.
 
Today's issue is that it only prints out the first page of the labels. I'm not sure it the firstrecord = 1 and the lastrecord = 1 has anything to do with it.

That's quite possible. Why don't you try removing lastrecord = 1, and see what difference it makes.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
That's indeed very probable, not to say obvious. What else do you think lastrecord = 1 would do, when not limiting merging to only 1 record? Googling mailmerge lastrecord I found the description of the LastRecord property:
So there you have it definitely: FirstRecord and Lastrecord determine what records are taken to merge. And while that is for office10, it surely is also valid for 11 and 12.

That's most probably a debug code to check, whether the code works or not fast, without having to do the merge with the whole data set. And if you don't find a comment saying that, it's bad of the programmer that put that debug code in there.

Maybe that code came from here?
You'll see the writer of that FAQ described the FirstRecord and LastRecord properties there: See the last few lines of the faq. If you read the properties insted of setting them, they will tell the first and last record of the source, in general firstrecord would read 1 and lastrecord would read reccount(). So if you remove those two lines and don't touch these properties you will MailMerge with all data.

The faq should probably be updated to document that.

Bye, Olaf.
 
Thanks Olaf, that fixed my problem. I did copy the code from the FAQ and never gave the first record and last record lines much thought until things weren't working just right. I was printing sheets of labels and most of my trials were less than 30 labels which didn't require a second page anyway. It's working nicely now, thanks again. And thank you also Mike for your help. This forum has certainly bailed me out several times, I'm glad I found it.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top