Creating Several .doc files from one big .doc file

S

Shadowfax

I have been give a Word file that contains 25-50 letters to different people.
The only thing separating them is a Section Break. I need to know how I can
make them separate files. I will then need to make each one a .pdf - Any
help would be greatly appreciated. Of course this is a file that I will be
receiving weekly, so I really don't want to go through each one and cut and
paste to new docs.

Thanks,
Shadowfax
 
S

StevenM

To: Shadowfax,

The following should be a start:

Sub SectionsToNewDocs()
Dim sFilePath As String
Dim sFileName As String
Dim nSections As Long
Dim nIndex As Long
Dim actDoc As Document
Dim newDoc As Document
Dim oRange As Range

Set actDoc = ActiveDocument
sFileName = actDoc.Name
sFilePath = actDoc.Path
If sFilePath = "" Then
MsgBox "This document does not have an active path." & Chr(13) _
& "Please save this document and try again."
Exit Sub
End If
nSections = actDoc.Sections.Count
For nIndex = 1 To nSections
Set oRange = actDoc.Sections(nIndex).Range
oRange.Copy
Set newDoc = Documents.Add
newDoc.Range.Paste
sNewFileName = UniqueFileName(sFilePath, sFileName, "")
newDoc.SaveAs FileName:=sNewFileName
newDoc.Close
Next nIndex
End Sub

'****
'*
'* UniqueFileName
'*
'* This function creates a unique name for a document based on three parts:
'* (1) the name of the active document from which this macro was ran;
'* (2) a suffix string;
'* (3) and a two digit number starting with "01"; it also tests to see if
'* such a document exists, if so, it increments the number and tries again.
'*
'****
Function UniqueFileName(ByVal sFilePath As String, _
ByVal sFileName As String, _
ByVal sSuffix As String) As String
Dim sNum As String
Dim sNewFileName As String
Dim nNumber As Long
Dim nPos As Long

nPos = InStrRev(sFileName, ".")
If nPos > 0 Then
sFileName = Left(sFileName, nPos - 1)
End If
Do
nNumber = nNumber + 1
sNum = CStr(nNumber)
If Len(sNum) = 1 Then
sNum = "0" & sNum
End If
sNewFileName = sFilePath & Application.PathSeparator _
& sFileName & "_" & sSuffix & sNum & ".Doc"
Loop While (Dir(sNewFileName) <> "" And nNumber < 1000)
UniqueFileName = sNewFileName
End Function

Steven Craig Miller

P.S. Is "Shadowfax" a name taken from the music group of same name, or the
name of Gandalf's horse?
 
S

Shadowfax

Thanks Steven, I'll give it a try.

StevenM said:
To: Shadowfax,

The following should be a start:

Sub SectionsToNewDocs()
Dim sFilePath As String
Dim sFileName As String
Dim nSections As Long
Dim nIndex As Long
Dim actDoc As Document
Dim newDoc As Document
Dim oRange As Range

Set actDoc = ActiveDocument
sFileName = actDoc.Name
sFilePath = actDoc.Path
If sFilePath = "" Then
MsgBox "This document does not have an active path." & Chr(13) _
& "Please save this document and try again."
Exit Sub
End If
nSections = actDoc.Sections.Count
For nIndex = 1 To nSections
Set oRange = actDoc.Sections(nIndex).Range
oRange.Copy
Set newDoc = Documents.Add
newDoc.Range.Paste
sNewFileName = UniqueFileName(sFilePath, sFileName, "")
newDoc.SaveAs FileName:=sNewFileName
newDoc.Close
Next nIndex
End Sub

'****
'*
'* UniqueFileName
'*
'* This function creates a unique name for a document based on three parts:
'* (1) the name of the active document from which this macro was ran;
'* (2) a suffix string;
'* (3) and a two digit number starting with "01"; it also tests to see if
'* such a document exists, if so, it increments the number and tries again.
'*
'****
Function UniqueFileName(ByVal sFilePath As String, _
ByVal sFileName As String, _
ByVal sSuffix As String) As String
Dim sNum As String
Dim sNewFileName As String
Dim nNumber As Long
Dim nPos As Long

nPos = InStrRev(sFileName, ".")
If nPos > 0 Then
sFileName = Left(sFileName, nPos - 1)
End If
Do
nNumber = nNumber + 1
sNum = CStr(nNumber)
If Len(sNum) = 1 Then
sNum = "0" & sNum
End If
sNewFileName = sFilePath & Application.PathSeparator _
& sFileName & "_" & sSuffix & sNum & ".Doc"
Loop While (Dir(sNewFileName) <> "" And nNumber < 1000)
UniqueFileName = sNewFileName
End Function

Steven Craig Miller

P.S. Is "Shadowfax" a name taken from the music group of same name, or the
name of Gandalf's horse?
 
S

Shadowfax

Gandalf's horse of course! - Fastest horse of Middle Earth
Another question, I'm new to q&a's in this type of forum, any way we can
communicate via email or IM? Or is that not allowed?
 

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