Selecting multiple words in Word 2000
I currently have some VBA code in Word that is piece-mealed together from some Microsoft articles and suggestions from other people. What the code does is take an .rtf document that has been created from an Access 2000 report, make seperate documents out of each report, and save it with the first word of the document. However, I would like now to include more than 1 word in the file name. I would like to have up to 6 words, seperated by an _ . It would look something like:
Word1_word2_word3_word4_word5_word6.doc
The code I have is:
Dim rngTitle As Range
Dim strTitle As String
' Find "xyz" and replace with Section Break Next Page, remove Page Break
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "xyz"
.Forward = True
.Wrap = wdFindContinue
End With
If Selection.Find.Execute = False Then
Exit Do
Else
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
Loop
Selection.HomeKey Unit:=wdStory
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'A mailmerge documents 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.Cop y
'Create a new document to paste text from clipboard.
Documents.Add Template:="P:\650-00_ABC Company_2003\Reporting\Individual Reports.dot"
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
Selection.Delete Unit:=wdCharacter, Count:=1
ChangeFileOpenDirectory "P:\650-00_ABC Company_2003\Reporting\Individual Reports"
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
'Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Color = wdColorWhite
Selection.HomeKey Unit:=wdStory
Set rngTitle = ActiveDocument.Range.Words(1)
strTitle = rngTitle.Text
ActiveDocument.SaveAs FileName:=strTitle & ".doc"
ActiveDocument.Close
' Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
I have limited coding knowledge, but I believe it is something in the Set rngTitle = ActiveDocument.Range.Words(1) that needs to be changed. I tried using
Set rngTitle = ActiveDocument.Range.Paragraphs(1), but I get a Runtime error that says "Type Mismatch". Any help would be GREATLY appreciated!!! Thanks.
Jeff
|