Help with Russ's Macro

F

Fuzzhead

We are converting thousands of documents from WordPerfect to Word. I have
write with the help from this site to clean up the bulk of the problems
created from the conversion. I found one that Russ wrote that works for most
of mine. The majority of the converted documents come across with the code as
{LISTNUM # \l #} or {LISTNUM ## \l #} where # is some number like in Russ's
macro. The problem is that there are a large number that have come across
with a second word after LISTNUM like {LISTNUM LEGAL \l #} or {LISTNUM
Paranum # \l #}. Is there a way to revise this macro to look for just the
word LISTNUM and the \l #, skipping the stuff in the middle?

Set SearchRange = ActiveDocument.Range
SearchRange.TextRetrievalMode.IncludeFieldCodes = True
With SearchRange.Find
.Wrap = wdFindStop
.Format = True
.Text = "^dLISTNUM ^# \l ^#"
While .Execute
'Select Found Text
SearchRange.Select

'Avoid Style Collection Error If Number is 0
If SearchRange.Characters.Last.Previous.Text = "0" Then
SearchRange.Characters.Last.Previous.Text = "1"
End If

'Apply Heading Format To Whole Paragraph Using Found Text Number
Selection.Paragraphs(1).Style = ActiveDocument.Styles("Heading " _
& SearchRange.Characters.Last.Previous)

'Delete Found Text
SearchRange.Text = ""

'Reset Range For Next Search Area
SearchRange.SetRange Start:=SearchRange.End, _
End:=ActiveDocument.Range.End
Wend
End With

Fuzzhead
 
R

Russ

Fuzzhead,
It might do what you want by just changing:
..Text = "^dLISTNUM"
It will find all fields starting with {LISTNUM and try to change them.
It won't find any with a space like { LISTNUM
Revised Subroutine with a couple of other changes, too:

Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
SearchRange.TextRetrievalMode.IncludeFieldCodes = True
With SearchRange.Find
.Wrap = wdFindStop
.Format = True
.Text = "^dLISTNUM"
While .Execute = True
'Select Found Text
SearchRange.Select

'Avoid Style Collection Error If Number is 0
If SearchRange.Characters.Last.Previous.Text = "0" Then
SearchRange.Characters.Last.Previous.Text = "1"
End If

'Apply Heading Format To Whole Paragraph Using Found Text Number
Selection.Paragraphs(1).Style = ActiveDocument.Styles("Heading " _
& SearchRange.Characters.Last.Previous)

'Apply List Format To Selection
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).Name = ""
Selection.Paragraphs(1).Range.ListFormat.ApplyListTemplate _
ListTemplate:=ListGalleries(wdOutlineNumberGallery _
).ListTemplates(5), ContinuePreviousList:=True, _
ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:=2

'Delete Found Text
SearchRange.Text = ""

Wend
End With
 
G

Graham Mayor

You will need a space between ^d and LISTNUM if Listnum has been inserted by
Word rather than manually from the keyboard. Word always inserts fields with
leading and trailing spaces inside the brackets.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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