PDA

View Full Version : Mail Merge Macro Autorun behaviour help needed



FhM
07-11-2008, 05:06 PM
Hello all

I am just starting using VBA and have a few problems I cant get my head around.

I need to run a macro automatically after the documents have been created from the mail merge. I have a set of data that is output to a table and some of the data will be blank so I want to delete that row.

I have the code to perform the operation but I dont know how to automate after the mail merge. I can get it to run on the template using autoopen etc but this does not effect the mail merge output.

Any help greatly appreciated.

GregQik
07-12-2008, 01:12 AM
Re-defining the task may or may not be the simple answer.

Can you refine the mailmerge to exclude blank rows from the data source?

if not:

Can you describe how you are starting the mailmerge?
Who will run the mailmerge?
Is the mailmerge run on different data sources?
Can you run the mailmerge automatically via VBA and then run your table cleanup routine from the same vba procedure after the mailmerge is finished?

FhM
07-12-2008, 08:04 AM
Thanks for the reply. The mail merge is actually started from a third party program which is a school MIS system (SIMS.net) . Essentially this is a GUI front end to a SQL database. I can alter the template but thats about it.

This program starts the merge so I have no choice off kicking it of with VBA there as I also cannot connect direct to the SQL.

I cannot exclude the blank rows at the source because every record will be different. On one record the line will be blank and on others it will be populated.

Thanks for the help I look forward to your reply.

GregQik
07-12-2008, 10:28 PM
When the mailmerge is started, what is the template attached to the newly merged document?

Is this Normal.dot?
Does the mailmerged document have headings of standard text that might be used to identify this document as being a part of the nominated mailmerge task?

You might be able to run a vba procedure with an 'On Time' delay from an autonew procedure or a document new event to give the mailmerge process time to complete then run the cleanup procedure.

The process would depend on the actual template of the newly created mailmerge document.

FhM
07-13-2008, 05:00 AM
I will have to check tommorow what template is attached to the newly merged document.

That sorta brings up another question for me as well. I noticed (Using word 2003) that when I created a template and clicked on it the macros did not carry over to the new document. The setup of the document was correct otherwise but looking at the properties it said the template was normal.dot. Macros would only carry over if I defined them in normal.dot. Is this normal or can I change this?

I am liking your on time idea and will try that out but I may need the answer to my above question first.

GregQik
07-15-2008, 04:52 AM
That is the purpose of a template which contains:

* VBA procedures,
* Toolbars,
* Bolierplate text that is copied into the new document,
* Autotext Entries, and
* Styles.

The template procedures remain available to the attached document but the document contains no VBA procedures without some trickery or code attached to 'Form' buttons in the document.

If this new merge document is created from 'Normal.Dot',you will need to be able to separate this target document against all of the other 'New' documents that are created against 'Normal.Dot'. Not hard but just another layer of code to be checked to ensure this procedure is not run against the wrong document. That is why I asked about standard text that can be found in the new document after the merge.

FhM
07-16-2008, 06:24 AM
Ah ok I see where your going. Some of the text would be "has received commendations".

So do a search for text then run my routines?

GregQik
07-17-2008, 07:13 AM
I would be checking for headings on the first line of the merge data if possible.

You just need to identify the new document as having been merged from your source .
The AutoNew procedure will fire whenever a new document is fired from a template. As all new 'blank' documents are created from 'Normal.dot', you could fire a new procedure from that with the 'OnTime' method. After a suitable delay to allow an automated mailmerge to complete, check if the new document does infact contained the nominated merged data:

If yes run your table cleanup routine, or
If No then exit.

FhM
07-17-2008, 03:19 PM
Ok thanks for all your help I will give it a go :) I am off on holiday tommorow but I will be reading my VBA book on the beach :P