Grad student
06-24-2008, 08:18 AM
Hello. I do not know much about VBA or macros. I found the following on this site for splitting a Word document and it is wonderful:
Option Explicit
Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & _
" sections. Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000")
doc.Close True
End If
Next I
End Sub
Sub test()
' delimiter & filename
SplitNotes "///", "Notes "
End Sub .
~Oorang
However, I need one that names each new file by the first line of text within each section. I saw another thread on this forum where it seemed very similiar. That person wrote:
Setting FIle Name when splitting document
I have a large word mail merge file that contains 3000 or so letters (all 1 page), I have code to split and save the document but need to change the line of code that sets the file name to be the first line of text in the letter.
This line references an employee number I need to set it as the file name so that the documents can be filed/ scanned.
The code at the moment is
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
I would prefer to keep the first formula because I have sectioned off my original large documents with (///). But if anyone knows a formula to help me, I would greatly appreciate it. Thank you.
Option Explicit
Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & _
" sections. Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000")
doc.Close True
End If
Next I
End Sub
Sub test()
' delimiter & filename
SplitNotes "///", "Notes "
End Sub .
~Oorang
However, I need one that names each new file by the first line of text within each section. I saw another thread on this forum where it seemed very similiar. That person wrote:
Setting FIle Name when splitting document
I have a large word mail merge file that contains 3000 or so letters (all 1 page), I have code to split and save the document but need to change the line of code that sets the file name to be the first line of text in the letter.
This line references an employee number I need to set it as the file name so that the documents can be filed/ scanned.
The code at the moment is
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
I would prefer to keep the first formula because I have sectioned off my original large documents with (///). But if anyone knows a formula to help me, I would greatly appreciate it. Thank you.