Naming files from a data source

A

AnnP

I used code in a macro from the following article to
separate merged pages into separate files. I would like
to be able to name each file from a data source, e.g.,
from an excel speadsheet or text from each page that is
being separated. Can someone help me modify the code
below to do that?

D2000: How to Programmatically Save Each Page or Section
of a Document As Separate File.

Sub BreakOnPage()
' Used to set criteria for moving through the document
by page.
Application.Browser.Target = wdBrowsePage

For i = 1 To ActiveDocument.BuiltInDocumentProperties
("Number of Pages")

'Select and copy the text to the clipboard
ActiveDocument.Bookmarks("\page").Range.Copy

' Open new document to paste the content of the
clipboard into.
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the page,
if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "C:\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum
& ".doc"
ActiveDocument.Close

' Move the selection to the next page in the
document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
 
D

Doug Robbins - Word MVP

Hi Ann,

Here's a method that I have used that involves creating a separate
catalog type mailmerge maindocument which creates a word document containing
a table in each row of which would be your data from the database that you
want to use as the filename.

You first execute that mailmerge, then save that file and close it. Then
execute the mailmerge that you want to create the separate files from and
with the
result of that on the screen, run a macro containing the following code
and when the File open dialog appears, select the file containing the table
created by the first mailmerge

Dim Source As Document, oblist As Document, DocName As Range, DocumentName
As String
Set Source = ActiveDocument
With Dialogs(wdDialogFileOpen)
.Show
End With
Set oblist = ActiveDocument
Counter = 1
While Counter < oblist.Tables(1).Rows.Count
Set DocName = oblist.Tables(1).Cell(Counter, 1).Range
DocName.End = DocName.End - 1

'Change the path in the following command to suit where you want to save
the documents.
DocumentName = "I:\WorkArea\Documentum\" & DocName.Text
Source.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs filename:=DocumentName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Counter = Counter + 1
Wend

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top