PDA

View Full Version : mailmerge problem



pickd3
08-01-2006, 08:25 AM
I'm not real skilled with vb macros. I have a template that has the following macro to merge data from a text file and save the new document with a document name stored in the text file. The macro works fine but if there is another Word document already open, it is getting renamed to the new name and saved while the new document created by the merge is left open with the merged data but with the default name. Obviously I've lost the active document someway. Your help would be appreciated. Thanks

Sub AutoNew()
'
' AutoNew Macro CMS MERGE ONLY
' Macro created 7/31/06 by pickd3
Dim strOldDoc As String
Dim strDocPath As String
Dim strNewDoc As String
strOldDoc = ActiveDocument.Name
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:="C:\temp\cmsmerge.txt", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1 _
:="", SubType:=wdMergeSubTypeOther
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
strDocPath = .DataFields("CMSPATH").Value
strNewDoc = .DataFields("DOCNAME").Value
End With
.Execute Pause:=False
End With
Documents(strOldDoc).Close SaveChanges:=wdDoNotSaveChanges
ChangeFileOpenDirectory strDocPath
ActiveDocument.AttachedTemplate = ""
ActiveDocument.SaveAs FileName:=strNewDoc, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub

Edited 11-Aug-2006 by geekgirlau. Reason: insert vba tags

fumei
08-02-2006, 06:15 AM
1. Please use the VBA tags for your code.

2. You have strOldDoc = ActiveDocument.Name
' other stuff
Documents(strOldDoc).Close SaveChanges:=wdDoNotSaveChangesYou are taking the Active Document and closing it.

This is an AutoNew procedure. It makes a new file, does the mail merge....then you close it with no changes. If there is a file already open, IT becomes the active document. You made the variable strNewDoc = .DataFields("DOCNAME").Value, (but closed the active document), which means again if a file is already open it now becomes the active document, and ActiveDocument.SaveAs FileName:=strNewDoc, of course gets renamed (as you are doing a SaveAs).

First off...you could check to see if there are any other documents open, and if there is, close them. Either than or move your code lines around.

Why are you doingDocuments(strOldDoc).Close SaveChanges:=wdDoNotSaveChangesanyway????

pickd3
08-10-2006, 06:27 PM
Thanks for the reply. I'm using a template (.dot) with an imbeded macro to generate a new document to be saved in specific folder. The problem and the reason I attempted to close the old document was that it seemed I was having an open document like 'document 1' left hanging open after the merge was done. I probably need to revisit that and see if maybe I had another problem that I tried to solve with that line of code. I did find that if I moved that code to the end of macro after the saveas that my document was created and saved as desired. Based on your comments, I would guess that I probably don't need that code at all.
Thanks again.

mdmackillop
08-11-2006, 12:12 AM
I find it simpler to merge to a Document rather than a template. There's a KB item here http://vbaexpress.com/kb/getarticle.php?kb_id=290 for automating the new document creation from a merge.
Regards
MD