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
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