Word 2003 Macro - need save file path code

Closed
VirtualInsanity - May 22, 2012 at 04:50 PM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - May 30, 2012 at 03:09 AM
Hello, I have an awesome little macro that I found on the net that saves pages from a merged doc to individual files. Through my testing of it I've found that when I run it directly from VBA it saves the resulting files in the same location as the original template file, which is fine. Except that I want to assign the macro to a button so it's available without having to go into VBA to run it. The only way I could save it to a button is by saving the macro under normal.dot which of course saves the resulting files wherever that file is (I'm not even sure where it is located!).
What I'm wondering is how do I adapt the code to specify a specific folder destination for the resulting files to be saved? I'm sure it's an easy tweek but I just can't figure it out!
Any help much appreciated.... (and I hope others find this code as helpful as I have. I got it from here: http://word.mvps.org/FAQs/MailMerge/index.htm)
Here's the code:
Sub SaveRecsAsFiles()
' Convert all sections to Subdocs
AllSectionsToSubDoc ActiveDocument
'Save each Subdoc as a separate file
SaveAllSubDocs ActiveDocument
End Sub
Sub AllSectionsToSubDoc(ByRef doc As Word.Document)
Dim secCounter As Long
Dim NrSecs As Long
NrSecs = doc.Sections.Count
'Start from the end because creating
'Subdocs inserts additional sections
For secCounter = NrSecs - 1 To 1 Step -1
doc.Subdocuments.AddFromRange _
doc.Sections(secCounter).Range
Next secCounter
End Sub
Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
.SaveAs FileName:="MergeResult" & CStr(docCounter)
.Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub
Sub RemoveAllSectionBreaks(doc As Word.Document)
With doc.Range.Find
.ClearFormatting
.Text = "^b"
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
Related:

2 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 26, 2012 at 07:59 AM
It is saving in this line


.SaveAs Filename:="MergeResult" & CStr(docCounter)

you can prefix it with hard coded value or you can use special folders location like (desktop, my documents etc"


.SaveAs Filename:="c:\MergeResult" & CStr(docCounter)
0
VirtualInsanity
May 29, 2012 at 04:47 PM
Hi there - thanks for this. I've tested it in quite a few ways but it doesn't seem to be paying attention to the file path I've give it and saves it in random places.. sometimes in the My Documents folder - sometimes in the same folder as the template file depending on the file path I've given it (neither of which saved it in the right place). I can't make out the pattern though. Here's the line that I have:

With newdoc
.SaveAs FileName:="Interviewees" & CStr(docCounter)
.SaveAs FileName:="C:\Documents and Settings\RicharAn\My Documents\Merged Letters" & CStr(docCounter)

I even tried directing it to the i: drive which is where I really want to save it but it still saved the resulting files to my My Documents folder on the c: drive!
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 30, 2012 at 03:09 AM
could you post a sample document on some file share site on which this macro can be ran ?
0