Combining Stings

G

Greg Maxey

I am playing with a macro that I first saw posted by Doug Robbins. It take
each page of a document and saves as a new document. I am trying to provide
a User interface with where the files are to be saved, but I can't work out
combine two strings. One is the Path and the other is the File name.

I am able to get both strings individually, but when I try to combine them,
the Full Path and Name looks something like this which is an invalid file
name: ""C\My Documents\""FileNam""

Here is the code. How do you combine tow strings like "Path\" and
"FileName" to come of with "Path\Filename" ?

Thanks.

Sub SaveEachPageOfCurrentDocumentAsANewDocument()
Dim pCounter As Long
Dim pNumPages As Long
Dim pParent As Document
Dim pChild As Document
Dim pParentName As String
Dim pChildName As String
Dim pChildNameRange As Range
Dim PathToUse As String

If ActiveDocument.Saved = False Then
Documents.Save NoPrompt:=False, _
OriginalFormat:=wdOriginalDocumentFormat
End If

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With

Set pParent = ActiveDocument

pParentName = pParent.FullName
Selection.HomeKey Unit:=wdStory
pNumPages = pParent.BuiltInDocumentProperties(wdPropertyPages)
pCounter = 0
While pCounter < pNumPages
pCounter = pCounter + 1
pParent.Bookmarks("\Page").Range.Cut
Set pChild = Documents.Add
pChild.Range.Paste
Set pChildNameRange = pChild.Range
pChildNameRange.Collapse wdCollapseStart '
pChildNameRange.Expand Unit:=wdParagraph
pChildNameRange.MoveEnd Unit:=wdCharacter, Count:=-1
pChildName = pChildNameRange
pChild.SaveAs FileName:="PathToUse & pChildName"
pChild.Close
Wend
pParent.Close SaveChanges:=wdDoNotSaveChanges
Documents.Open pParentName
End Sub
 
J

Jezebel

Just drop the quotes --

pChild.SaveAs FileName:=PathToUse & pChildName

Do you need to check that PathToUse has a trailing slash?
 
G

Greg Maxey

Jezebel,

Thanks, I tried that earlier. I have found a solution in the meantime. For
some reason the pPathToUse = .Directory was appearing in double quotes e.g.,
""C:\Some Path\Some Path""

I have dabbled for the first time with this Len business to come up with a
way to chop that to
"C:\SomePage\SomePath"

Now things are combining correctly. Here is the code:

Sub SaveEachPageOfCurrentDocumentAsANewDocument()
Dim pCounter As Long
Dim pNumPages As Long
Dim pParent As Document
Dim pChild As Document
Dim pParentName As String
Dim pChildName As String
Dim pChildNameRange As Range
Dim pPathToUse As String
Dim myLen As Long

If ActiveDocument.Saved = False Then
Documents.Save NoPrompt:=False, _
OriginalFormat:=wdOriginalDocumentFormat
End If

With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
pPathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With

Set pParent = ActiveDocument

pPathToUse = Mid(pPathToUse, 2)
myLen = Len(pPathToUse)
pPathToUse = Left(pPathToUse, myLen - 1)

pParentName = pParent.FullName
Selection.HomeKey Unit:=wdStory
pNumPages = pParent.BuiltInDocumentProperties(wdPropertyPages)
pCounter = 0
While pCounter < pNumPages
pCounter = pCounter + 1
pParent.Bookmarks("\Page").Range.Cut
Set pChild = Documents.Add
pChild.Range.Paste
Set pChildNameRange = pChild.Range
pChildNameRange.Collapse wdCollapseStart '
pChildNameRange.Expand Unit:=wdParagraph
pChildNameRange.MoveEnd Unit:=wdCharacter, Count:=-1
pChildName = pPathToUse & pChildNameRange
pChild.SaveAs FileName:=pChildName
pChild.Close
Wend
pParent.Close SaveChanges:=wdDoNotSaveChanges
Documents.Open pParentName
End Sub
 
J

Jezebel

How odd. Very unusual to have a string value returned with quotes around it.
Anyway, a simpler way to get rid of them is

pPathToUse = Replace(pPathToUse, chr(34), "")

which zaps them all in one hit.
 

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