BaseName help - Can't access/grab text

F

fulotta

I'm trying to take a merged document, have the VB macro chop each range
as a separate file and save it as a filename specified by the first
field. For instance, if the first line of text (field is called
"story") in the merged range appears as "car", I would want the file
saved as C:\car001.doc


However, I'm having trouble with the BaseName creation to actually grab
that text from the first line in the document. Please help!?!


Sub Splitter()
Selection.EndKey Unit:=wdStory
numlets = Selection.Information(wdActiveEndSectionNumber)
If numlets > 1 Then numlets = numlets - 1
Selection.HomeKey Unit:=wdStory
For Counter = 1 To numlets
BaseName = "c:\" + ActiveDocument.Bookmarks.Field("story")
DocName = BaseName & Right$("000" & LTrim$(Str$(Counter)), 3)
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveDocument.SaveAs FileName:=DocName
ActiveWindow.Close
Next Counter
End Sub
 
H

Helmut Weber

Hi,

bookmarks are unique to a document.
If you create a mailmerge document from a document,
which contains bookmarks, the bookmarks are gone
in the merged document, IMHO.

ActiveDocument.Bookmarks.Field("story")
won't work anyway.

There are no fields in the bookmarks' collection, only Bookmarks.

And I wonder, whether "story" could be a field's index.

There may be fields in a certain bookmark's range, like

msgbox activedocument.bookmarks("Mark1").range.fields(1).result

No use anyway, as there are no bookmarks in the merged doc.

And for bulding the files' fullnames:

Example:

Sub Test0023()
Dim l As Long
Dim sFln As String ' filename
Dim sPth As String ' path
sPth = "c:\test\"
For l = 1 To 10
sFln = sPth & ActiveDocument.Fields(1).Result
ActiveDocument.Fields(1).Delete
sFln = sFln & Format(l, "000") & ".doc"
MsgBox sFln
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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