View Full Version : Solved: Save Merged Document code
tkaplan
11-07-2005, 09:49 AM
I have a document with merged fields named F1,F2 etc.
I want code to save the merged file as a new document with the name being the value of F1.
I need to write code for the following logic:
dim adPath as string
adPath = activedocument.path
'find first merged record record
'value of F1 for first record
'merge to new document - current record only
'save as filename="Notification" & value of F1 - in same path as current document
'close new file and switch back to original file
'loop to next record
so if for example i have 3 records in my data source values A,B,and C, i should end up with three additional files named NotificationA, NotificationB and NotificationC.
Thanks in advance for any help.
mdmackillop
11-07-2005, 11:02 AM
Hi Tkaplan
There is a KB item here re automated saving of merge documents which might get you started. Any questions, let us know.
http://vbaexpress.com/kb/getarticle.php?kb_id=290
Regards
MD
tkaplan
11-07-2005, 11:06 AM
Thank you for your response.
I did come accross this code in my search however i could not figure out how i can modify it to fit my needs. I couldn't find or follow the logic for which line finds the value of the field in each record and save the file using that name.
Any help would be greatly appreciated.
Thank you
mdmackillop
11-07-2005, 12:05 PM
Here is a modified version. On opening AutoSave.doc, it will create a new document (in C:\Atest) for each record. There is a glitch in the code as the loop will not exit using RecordCount, but no more time just now.
Problem lines of code
Do
SaveName = Documents(MergeFileName).MailMerge.DataSource.DataFields(1) & ".doc"
DoSave SaveRoot, SaveName
If .ActiveRecord <> .RecordCount Then
.ActiveRecord = wdNextRecord
End If
Loop Until .ActiveRecord = .RecordCount
tkaplan
11-07-2005, 01:59 PM
I put the following code and it's not working. i know that logically it should not but i cant figure out why. I apologize - I am quite familiar with VBA in access and excel but this is my first time in word. i found this board and got really excited that maybe i can find help herehttp://vbaexpress.com/forum/images/smilies/001.gif
i will attach the sample files that i am working on. i am confident if i can get it working on here i should be able to transpose it to my scenerio.
Thanks in advance.
Sub SaveAsFileName()
'i put this in the normal.dot. i activated notification.doc and then ran the macro
Do
SaveName = Documents(ActiveDocument.Name).MailMerge.DataSource.DataFields(1) & ".doc"
DocSave SaveName
If .ActiveRecord <> .RecordCount Then
.ActiveRecord = wdNextRecord
End If
Loop Until .ActiveRecord = .RecordCount
End Sub
Sub DocSave(DocName)
'i put this in module of notification.doc
Dim Rec As Integer
Dim ToBeSaved As String, MergeFileName As String, JobNo As String, Direct As String
Dim Project As String, SaveName As String, SaveAsName As String
Dim Length As Integer
Dim Message As String, Title As String, Default As String, MyValue As String
Dim DataF As Variant, DRecord As String
'Obtain SavePath data from first two merge fields
Rec = 0
For Each DataF In _
Documents(MergeFileName).MailMerge.DataSource.DataFields
Rec = Rec + 1
DRecord = DataF.Value
If Rec = 1 Then JobNo = DRecord
If Rec = 2 Then Project = DRecord
If Rec = 3 Then GoTo MailMergeLine
Next DataF
'Create new merged document
MailMergeLine:
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
'Create name for file
SaveAsName = SaveName
ActiveDocument.SaveAs FileName:=SaveAsName
'Confirm path and filename used
MsgBox ActiveDocument.Path & Application.PathSeparator & ActiveDocument.Name
LastLine:
'Close mailmerge document
Windows(ActiveDocument.Name).Close savechanges:=False
End Sub
mdmackillop
11-07-2005, 03:02 PM
Try the following
tkaplan
11-08-2005, 05:50 AM
That's perfect. Thank you so much. I am going to transfer this over to the document i am actually using and i will post any questions.
Thanks again. You're a life saver.
tkaplan
11-08-2005, 06:21 AM
Ok, i transfered this over to my document and it works. However, I am coming accross another problem with the mail merge. One of my columns in my data source is a currency amount. it is coming over in the data source with 8 decimal places. i checked the example that you provided for me and it is happening there too. Do you know how i can change this, in the excel file or in word. the excel file only has 2 decimal places there as well.
Thanks.
tkaplan
11-08-2005, 06:35 AM
I found this question on a different forum and they explained how to do it. I now have what i need working perfectly.
Thank you so much mdmackillop for all of your help:)
tkaplan
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.