Including paragraph text into filename

S

Saf

I found the original code through this link:
http://support.microsoft.com/?kbid=216201

It is meant to split a document at the section breaks and save each section
as its own brand new word document. I'm running Word2003 and the code works
wonderfully well.

I attempted to change the file name provided within the vba code. My doc is
from a mail merge and I want it to pull the text from paragraph 8 as part of
the new file names (ActiveDocument.SaveAs FileName:="Mail09_" & para & ".doc")

I'm very new to this and I get the following error when I compile: "Run-time
error '5487' Word cannot complete the save due to a file permission error."

Can someone please help me fix the code? I'm not even sure what the error
means. Also, is it possible to pull only a portion of the paragraph, i.e.
only the last word?? If not, or if its too cumbersome, thats ok.

Sub BreakOnSection()
'Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy

'Create a new document to paste text from clipboard.
Documents.Add
Selection.Paste

'Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

ChangeFileOpenDirectory "S:\"

'Changed this portion of the code to use paragraph # 8 from the section
para = ActiveDocument.Paragraphs(8)
ActiveDocument.SaveAs FileName:="Mail09_" & para & ".doc"

ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Thank you in advance for any help!
Saf
 
F

Fumei2 via OfficeKB.com

Let's deal with this first:

"Also, is it possible to pull only a portion of the paragraph, i.e.
only the last word??"

Yes.

You do not have the variable para declared - a bad thing really - but if it
is a RANGE, then you can get the last word out.

Dim strLastWord As String
Dim rngPara as Range

strLastWord = rngPara.Words(rngPara.Words.Count)

Next...

You do NOT have to make a copy and paste operation. You can - again - use a
Range object (with a Section object).

Dim oSection As Section
Dim r As Range
Dim TempDoc As Document

For Each oSection In ActiveDocument.Sections
Set r = oSection.Range
Set TempDoc = Documents.Add
With TempDoc
.Range = r
.SaveAs Filename:= "whateverNameYou want"
.Close
End With
Set TempDoc = Nothing
Next

Gerry
 
F

Fumei2 via OfficeKB.com

Ooops, I forgot your 8th word thing.

Dim oSection As Section
Dim r As Range
Dim ParaRange As Range
Dim TempDoc As Document

For Each oSection In ActiveDocument.Sections
Set r = oSection.Range
Set TempDoc = Documents.Add
With TempDoc
.Range = r
Set ParaRange = r.Paragraphs(8).Range
.SaveAs Filename:= ParaRange.Words(ParaRange.Words.Count - 1) & ".
doc"
.Close
End With
Set TempDoc = Nothing
Next

This assumes the the 8th paragraph terminates with a paragraph mark. which is
counted as a "word", thus it is .Words.Count - 1 - to get the last text word
before the paragraph mark.


Gerry said:
Let's deal with this first:

"Also, is it possible to pull only a portion of the paragraph, i.e.
only the last word??"

Yes.

You do not have the variable para declared - a bad thing really - but if it
is a RANGE, then you can get the last word out.

Dim strLastWord As String
Dim rngPara as Range

strLastWord = rngPara.Words(rngPara.Words.Count)

Next...

You do NOT have to make a copy and paste operation. You can - again - use a
Range object (with a Section object).

Dim oSection As Section
Dim r As Range
Dim TempDoc As Document

For Each oSection In ActiveDocument.Sections
Set r = oSection.Range
Set TempDoc = Documents.Add
With TempDoc
.Range = r
.SaveAs Filename:= "whateverNameYou want"
.Close
End With
Set TempDoc = Nothing
Next

Gerry

I found the original code through this link:
http://support.microsoft.com/?kbid=216201
[quoted text clipped - 48 lines]
Thank you in advance for any help!
Saf
 
F

Fumei2 via OfficeKB.com

Lastly, it should be mentioned that by hard-coding the paragraph number (8),
you had better be using proper styles, and none of those "empty" paragraphs
people use to make space between paragraphs. Each one of those is a real
paragraph and is counted!
Ooops, I forgot your 8th word thing.

Dim oSection As Section
Dim r As Range
Dim ParaRange As Range
Dim TempDoc As Document

For Each oSection In ActiveDocument.Sections
Set r = oSection.Range
Set TempDoc = Documents.Add
With TempDoc
.Range = r
Set ParaRange = r.Paragraphs(8).Range
.SaveAs Filename:= ParaRange.Words(ParaRange.Words.Count - 1) & ".
doc"
.Close
End With
Set TempDoc = Nothing
Next

This assumes the the 8th paragraph terminates with a paragraph mark. which is
counted as a "word", thus it is .Words.Count - 1 - to get the last text word
before the paragraph mark.

Gerry
Let's deal with this first:
[quoted text clipped - 39 lines]
 

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