C
Carlos Chalhoub
Hi listmates,
This macro was posted a couple of weeks back by Doug Robbins. I am trying to
use it for my pruposes, but it fails because the docname variable (docname =
docnamerange.Paragraphs(1).Range.Text) adds an extra space at the end of the
string. When that happens, the space is followed by the extension (.doc),
and the filename construct is not valid anymore. I think that the extra
space is replacing the paragraph mark, but I'm not sure. Can somebody help?
Carlos
Sub SaveEachPageAsSeparateFile()
Dim Pages As Long
Dim docname As String
Dim docnamerange As Range
Dim Source As Document
Dim Target As Document
Dim SourceName As String
Set Source = ActiveDocument
SourceName = Source.FullName
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Counter = Counter + 1
Source.Bookmarks("\Page").Range.Cut
Set Target = Documents.Add
Target.Range.Paste
Set docnamerange = Target.Range
docnamerange.Collapse wdCollapseStart
docname = docnamerange.Paragraphs(1).Range.Text
docnamerange.Paragraphs(1).Range.Delete
Target.SaveAs fileName:=docname, FileFormat:=wdFormatDocument
Target.Close
Wend
Source.Close wdDoNotSaveChanges
Documents.Open SourceName
End Sub
This macro was posted a couple of weeks back by Doug Robbins. I am trying to
use it for my pruposes, but it fails because the docname variable (docname =
docnamerange.Paragraphs(1).Range.Text) adds an extra space at the end of the
string. When that happens, the space is followed by the extension (.doc),
and the filename construct is not valid anymore. I think that the extra
space is replacing the paragraph mark, but I'm not sure. Can somebody help?
Carlos
Sub SaveEachPageAsSeparateFile()
Dim Pages As Long
Dim docname As String
Dim docnamerange As Range
Dim Source As Document
Dim Target As Document
Dim SourceName As String
Set Source = ActiveDocument
SourceName = Source.FullName
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Counter = Counter + 1
Source.Bookmarks("\Page").Range.Cut
Set Target = Documents.Add
Target.Range.Paste
Set docnamerange = Target.Range
docnamerange.Collapse wdCollapseStart
docname = docnamerange.Paragraphs(1).Range.Text
docnamerange.Paragraphs(1).Range.Delete
Target.SaveAs fileName:=docname, FileFormat:=wdFormatDocument
Target.Close
Wend
Source.Close wdDoNotSaveChanges
Documents.Open SourceName
End Sub