Space added to variable

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
 
J

Jonathan West

What is happening is that there is an extra carriage return character on the
end of the string. To fix it, all you have to do is chop the last character
fromthe string.

After this line of code

docname = docnamerange.Paragraphs(1).Range.Text

add this

docname = Left$(docname, Len(docname) - 1)
 
C

Carlos Chalhoub

Thanks, it works now.

Jonathan West said:
What is happening is that there is an extra carriage return character on the
end of the string. To fix it, all you have to do is chop the last character
fromthe string.

After this line of code

docname = docnamerange.Paragraphs(1).Range.Text

add this

docname = Left$(docname, Len(docname) - 1)

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup


trying (docname
 

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