Using Find/Replace text as a variable?

J

Jason L

Hi all
I found this code from MVP site, and would like to use it in a larger macro,
but I need it to work for all "List Number" styles in a document, not just
the selected area. Is it possible to use Find/Replace text as the main
variable in this code? For example, prevace the code with something like Dim
stylelist as Range, and then in the block of code set stylelist to the found
style, such as the "List Number."

I hope this makes sense.

Sub ListNumber()
With Selection.Paragraphs
.Style = "List Number"
With .First.Range.ListFormat
.ApplyListTemplate .ListTemplate, False
End With
With .Last
.KeepWithNext = False
.SpaceAfter = 10
End With
End With
End Sub

TIA,
Jason
 
K

Klaus Linke

Hi Jason,

It's not clear how you define "lists". To start with, all paragraphs in
some list style belong to the same list.

The most obvious way to restart them is to make them part of an outline
numbered list, and have them restart after some higher level style (which
needn't be numbered), such as some heading style.

If that's not possible, and you want to apply manual restarts with your
macro, you need some way to select (or set a range) to a certain "list"
before you can restart it, and/or put in "space after" -- as in your macro.

In your other thread, you defined a "list" by beginning after a "Heading 6"
style that contained some specific text.
It would probably end at the next "Heading 6" containing that text, or at
the end of the document.

If you set a range for such a "list", you can either select it, or replace
"Selection" with your range in the macro, and it should work fine.

But maybe another method to do the restarts might be simpler and safer
(...manual restarts get lost easily, and are hard to maintain since you
can't see them...).
Say, put hidden LISTNUM fields in the "Heading 6" paragraphs. You could
probably do that with "Find/Replace", if you copy such a field to the
clipboard, and replace with the clipboard content ^c.
See the articles by Margaret Aldis linked in
http://word.mvps.org/FAQs/Numbering/ListRestartMethods.htm for different
options.

Regards,
Klaus
 
J

Jason L

Klaus,

Thanks for your response. The problem is that the lists in my document are
not lists in the Word template sense. They do not follow the styles of List
Number 1, List Number 2, etc. They are merged in from another source and
contain only a number, period and text following that. For non-Word users,
they look like lists, but they need to reformatted according to the template
styles. All of these lists are between two words - Recommendations and
Discussion. When the list comes to one of these words, it needs to stop
numbering, and when it begins after this heading, it needs to restart. The
macro I have thus far finds Recommendations and Discussions and converts them
into Heading 6. It also looks for the unformatted numbered lists and
converts them into formatted lists based on a style in that template (e.g.
List Number 1 or 2).

I have tried the Outline method before, but I had a lot of problems with it.
I will try that or the ListNum approach.

Thanks for your assistance.

-Jason
 
K

Klaus Linke

[...] I will try that or the ListNum approach.

I think it'll be the easiest method.

Good luck!
Klaus
 
J

Jason L

Klaus,

Okay, that's going to work. Wow. I really think this is gonna work out
great, but I got one problem (surprise, surprise). I recorded a macro that
goes to each heading six and inserts a ListNum field before the heading, then
formats it to a white font (so as not to be seen in the document) and then
copied the text. I repeated this process for the rest of the document. I
know I can trim this code down considerably using a Loop function, but I am
not sure how. Any ideas?

Selection.GoTo What:=wdGoToHeading, Which:=wdGoToFirst, Count:=6, name:=""
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, name:=""
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphJustify
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"LISTNUM \l 1 \s 0", PreserveFormatting:=False
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With Selection.Font
.name = "Times New Roman"
.Size = 11
.Bold = True
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorWhite
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
End With
Selection.Copy
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, name:=""
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, name:=""
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphJustify
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.PasteAndFormat (wdPasteDefault)
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, name:=""
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphJustify
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.PasteAndFormat (wdPasteDefault)
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToNext, Count:=1, name:=""
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphJustify
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.PasteAndFormat (wdPasteDefault)
End Sub

TIA,
Jason

Klaus Linke said:
[...] I will try that or the ListNum approach.

I think it'll be the easiest method.

Good luck!
Klaus
 
K

Klaus Linke

Hi Jason,

Great! A couple of ideas...

-- Instead of using GoTo heading:
Go to the start of the document, then use "Find" to locate the next
heading.
Then use something like
While .Execute
' code to insert and format the LISTNUM field
Wend
to repeat that until no more heading is found (.Wrap = wdFindStop).
You can find examples of that technique with Google:
<http://www.google.com/groups?as_q="While .Execute"&as_ugroup=microso
ft.public.word.*>


-- Use "Selection.Font.Hidden = True" instead of ".Color = wdColorWhite".
(All the other lines in "With Selection.Font ... End With" are redundant)
A white number could force the last word to the next line.

-- As you said, you can (and should) trim your code a bit.
Especially get rid of
Selection.Find.ParagraphFormat.Borders.Shadow = False
The macro recorder often puts in stuff like that, and it can kill your
macro.

-- Since you've already found the proper code to insert and format the
LISTNUM field, you don't really need Copy/Paste.
I only suggested that so you don't need to write that code ... or possibly
don't need a macro at all.
If you copy the hidden field (Format > Font > Hidden) to the clipboard, you
could just search for the paragraph mark (^p) in the heading style, and
replace with ^c^& (clipboard content followed by the found text/paragraph
mark).
And if you only want to match headings that contain "Recommendations:" you
could search for "Recommendations:" in the heading style, and again replace
with ^c^&.

Regards,
Klaus
 
J

Jason L

Klaus,

Okay, I am almost there - I can feel it. When I run the following code, I
get an error 5692, though. This has gone through several revisions, but
here is the most recent version. Here is the code:

Sub test8()
'
' test8 Macro
' Macro recorded 11/4/2004 by
'


With Selection.Find.Style = ActiveDocument.Styles("Heading 6")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^c^&"
.Forward = True
.Wrap = wdFindContinue
While .Execute
Selection.Collapse Direction:=wdCollapseStart
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldListNum
= True, Text:=("LISTNUM \l 1 \s 0")
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Hidden = True
Selection.Copy
Wend

End With

Selection.Find.Execute

Thanks for your patience and help.

-Jason
 
K

Klaus Linke

Okay, I am almost there

Yes! I'm not sure where the error message comes from, but if you fix the
things below, you should get it working.

The list looks long, but it's only a couple "small things" remaining:

-- Delete the "With" at the very beginning.

-- You can eiter create an empty field, and put "LISTNUM" into the text, or
create a listnum field, then you don't need that in the text:
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="LISTNUM \l 1 \s 0"
or
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldListNum, Text:="\l 1 \s 0"

-- Best go to the top of the document at the very beginning of the macro,
so you "get" all the headings:
Selection.HomeKey Unit:=wdStory

-- You should change
.Wrap = wdFindContinue
to
.Wrap = wdFindStop
so the macro stops if it doesn't find any more headings.

-- After you have hidden the listnum field, you should put the cursor below
the next paragraph mark, so the same heading isn't found again (and again):
' ...
Selection.Font.Hidden = True
Selection.MoveDown Unit:=wdParagraph, _
Count:=1, Extend:=wdMove
Wend

-- You can delete "Selection.Copy"

-- If you only want to restart at headings that contain the text
"Recommendations:", then replace
.Text = "^p"
with
.Text = "Recommendations:"

Greetings,
Klaus
 
J

Jason L

Klaus, et al,

Thanks again for your help. I've made the changes and run the macro, but
I'm still getting the error. The error box also has a picture of a small
square on it, too. I know, it's weird. Anyway. Here the exact code I'm
using:

Selection.HomeKey Unit:=wdStory

Selection.Find.Style = ActiveDocument.Styles("Heading 6")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"

While .Execute

Selection.Collapse Direction:=wdCollapseStart
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldListNum, Text:=" \s 0"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Hidden = True
Selection.MoveDown Unit:=wdParagraph, _
Count:=1, Extend:=wdMove
Wend


.Forward = True
.Wrap = wdFindStop
.Format = True
With .Replacement
.Text = "^c^&"
End With



End With


End Sub


TIA,
Jason
 
K

Klaus Linke

Hi Jason,

The macro runs fine here (Word2002)...
Which version of Word do you use?

I copied it into Word, and then from there into the VBA editor, to get line
breaks converted into paragraph marks.
Maybe you could try to recreate the macro the same way... in case there's
some weird (control) character in the macro that throws off the VBA editor.

I don't find an error 5692 documented, but from googling it seems to be
caused by some Find code that Word can't deal with.
You could try to put the lines between "Wend" and "End With"... before the
"While" (so Word knows about them before ".Execute"-ing the Find), although
it doesn't seem to matter in Word2002 (to my surprise).

You also could delete the three "With .Replacement" lines, though they
don't hurt in Word2002 either -- but might conceivably in Word97/2000...
say, if you don't have anything on the clipboard.

Regards,
Klaus
 
J

Jason L

Klaus,

Thanks so much for your help. I figured out that I had not run a
clearformatting, clear find/replace text before running that macro. Thanks
so much for your assistance.

-Jason
 
K

Klaus Linke

Jason said:
Thanks so much for your help. I figured out that I had not run a
clearformatting, clear find/replace text before running that macro.
Thanks so much for your assistance.

Argh, I didn't notice that either!

Glad you solved it!
Klaus
 

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