Automation and mail merge?

W

Wilson Pye

Hi,

Problem is when performing a mail merge the template, header, and data docs
are opened behind the preview screen.

If they are not manually closed by the user, the header.doc remains open
and cannot be accessed again.

I would like to use the preview option before printing.


Is there a way to only have the mail merge doc open and make sure header
doc is closed?

I tried another approach which was simply to print the doc and quit word...
problem was the nasty message "If you quit you dont print"....
is there a way to issue the quit after printing is completed - no prompts?

I am automating Word from a VFP app. I am producing a doc for the purpose
of printing only. First I create a template. The template is a mail merge
document attached to a header source only. I run a query, then call a
routine to attach the header, populate and save template. I then create a
new .doc from the .dot, create the actual data source and perform the
merge.
If anyone is familiar with "Microsoft Office Automation with Visual FoxPro"
published by Hentzenwerke... I am using the their ideas and supplied code as
a guideline.

The end of the routine where I am doing the mail merge looks like this:

*****************
oWord.Visible = .T.
* Create a new document based on the template
cTemplatePath = oWord.Options.DefaultFilePath( wdUserTemplatesPath )
oDocument = oWord.Documents.Add( ADDBS(cTemplatePath) + "AuctionReceipt" )

SELECT auctionnam,itemnumber,itemdescri,;
charitynam,starttime,bid,tax,total,;
marketvalu,deductible,purflname,purco,;
purstreet1,purcity,purstate,purzip;
FROM tempaucitems;
INTO CURSOR tempaucitemscurs

DO CreateDataSource WITH oDocument, DBF("tempaucitems"), AddBs(SYS(2023)) +
"AucitemsReceiptData"

USE IN tempaucitemscurs

* Perform the mail merge
oDocument.MailMerge.Execute()
oMergedDocument = oWord.ActiveDocument

WITH oMergedDocument
IF .ActiveWindow.WindowState <> wdWindowStateMaximize
* Move it to make it visible - for some reason, it comes up
* way off screen
.ActiveWindow.Left = 0
.ActiveWindow.Top = 0
ENDIF

* Print document
* .PrintOut( , , wdPrintAllDocument)

* Preview it
.PrintPreview()
ENDWITH

ClOSE TABLES
RETURN
*****************



Any help would be much appreciated.

Wilson
 
C

Charles Kenyon

When performing the mail merge, are you creating a new merge document based
on your template? The template should not be open (as a document on the
screen) although it will be open as a vba project.

Are you merging to a new document rather than to the printer?

Application.Documents("header.doc").close

or put a test in your code to see if it is already open before opening it
again


--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
W

Wilson Pye

Hi Charles,

Thanks for the close code. I subscript responded to you in your message.

For me, I'm swimming in uncharted waters. Basically I want to produce some
receipts and such that do not have to saved, just printed. I am finding
the preview option to be a problem because the user has to close files and
word manualy. How do you handle this?

I'm thinking of just seting visible to false and sending the doc to the
printer.
My next problem with this is how to issue the quit word when the printing is
done?

Wilson
 
C

Charles Kenyon

You might want to post these questions in the vba.beginners newsgroup.
You'll need to first check whether Word is already open when you start your
procedure, if it is, you don't want to close it on the user. You would want
to track which documents you open/create for your procedure and close them
without saving when they are not needed any longer. You could do some kind
of timed loop that checks on the printing status before closing. Word
generates a message box when you try closing sooner; I'm not sure how you
would intercept this, but believe you can. Then close Word.

As you said, you could make your documents not visible.

As to the preview option, what is the purpose of the preview? Do you want
the user to be able to back out and make changes if it doesn't look right?
--

Charles Kenyon

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Wilson,

I am not sure why you need the header doc. Doesn't your query have field
names?

You can cause the balance of the code in the macro not to execute until the
printing is finished by using

..PrintOut( False, , wdPrintAllDocument)

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
C

Cindy M -WordMVP-

Hi Wilson,

Let's start with the printing problem you have during automation, as that's
fairly clear cut:

You need to set the BackgroundPrinting option (part of the
Application.Options collection) to FALSE. This way, your VFP code will wait
with execution (closing everything) until the print job has completed
spooling.

Now what said:
Problem is when performing a mail merge the template, header, and data docs
are opened behind the preview screen.

If they are not manually closed by the user, the header.doc remains open
and cannot be accessed again.

I would like to use the preview option before printing.


Is there a way to only have the mail merge doc open and make sure header
doc is closed?

I tried another approach which was simply to print the doc and quit word...
problem was the nasty message "If you quit you dont print"....
is there a way to issue the quit after printing is completed - no prompts?

I am automating Word from a VFP app. I am producing a doc for the purpose
of printing only. First I create a template. The template is a mail merge
document attached to a header source only. I run a query, then call a
routine to attach the header, populate and save template. I then create a
new .doc from the .dot, create the actual data source and perform the
merge.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top