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

Mail Merge Source File 1

Status
Not open for further replies.
Feb 6, 2003
48
US
Hi-

I have created a Word document that pulls data from a database, writes that data to a text file and then uses that file as the datasource for a mail merge. I have set the code up to delete and recreate the source file whenever the form is run so that I only get current data. The form is doing what I want it to, save one thing: the source file becomes associated with the main document during the merge process. If I try to run the form again to get new data, I get a "permission denied" error when the code tries to delete the old source file.

Does anyone know how to disassociate the source file from the main document in VBA. I can do it manually, but I am going to release this non-technical user and I would like the process to be seamless.

Thanks in advance and Happy Thanksgiving.
 
I can do it manually
Have you tried the macrorecorder ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The "manual way" is to close the document, delete the source file and then re-open the document. At this point Word tells you the source file no longer exists and gives you the option of removing the data/header source. I created a macro to record the steps when I re-open the file and remove the data/header information but that was not helpful.
 
Hi Richard,

AFAIK, there is no way, even manually, to properly disassociate a mailmerge from a datasource from within Word. What you have to do is associate it with a new datasource (which must exist) and the easiest thing to do is, I think, to use an empty text file and ignore the error. If need be you could create the empty file in your temp folder first and delete it later. Try coding something like ..

Code:
[blue]Open Environ("TEMP") & "\~empty.txt" For Output As #1
Close #1

On Error Resume Next
ActiveDocument.MailMerge.OpenDataSource Environ("TEMP") & "\~empty.txt"

[green]' do your stuff and then reassociate your proper data[/green]

Kill Environ("TEMP") & "\~empty.txt" [/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
TonyJollans-

Thank you very much. I did not use Environ but accomplished what I needed to by reactivating the main file, and reassigning the datasource to a dummy file.

Documents("ServiceAward-mm.doc").Activate
ActiveDocument.MailMerge.OpenDataSource Name:="C:\temp-point.txt"

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top