PDA

View Full Version : Solved: Search paragraph and remove numbering



AyeSee
02-22-2011, 02:41 PM
Hi,

I have a paragraph that always starts by "For example," in which i need to remove numbering (Word's automatic numbering)... I have been trying for quite a while, but I can't seem to get the document to move the the desired paragraph. Aslo, I can't seem to get the condition working to only allow this to happen for paragraphs that start by "For example,". :banghead:

EX:
FROM

4. For example, the required [...]

TO

For example, the required [...]

Here is the code that I attempted to write in order to achieve this:
Dim RemoveNum As Word.Range
Set RemoveNum = ActiveDocument.Range
With RemoveNum.Find
.Text = "For example"
.MatchWholeWord = False
.MatchCase = True
While .Execute
With RemoveNum
If .InRange(RemoveNum) = "For example" Then
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
End If
End With
Wend
End With
End Sub

Quite visibly, I am not understanding properly certain of these methods...

Many thanks in advance,
Alex

AyeSee
02-23-2011, 01:16 PM
Ok well i have Macro builder solution...

Sub Macro17()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "For example,"
.replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
End If
End Sub

Yay for simplicity

macropod
02-25-2011, 05:04 AM
Hi AyeSee,

What you should have done is to define an appropriate un-numbered paragraph Style and use Find/Replace to change all the affected paragraphs with that Style. The hard-formatting approach you've used is less than optimal.

AyeSee
02-28-2011, 06:02 AM
Hey Paul,

Thanks! Thats a great idea! What your saying is the search replace can search for styles and replace them by another style? Thats pretty cool!

The only problem is the desing of my word file is kind of uncommon. The wording comes from an Access database. In all cases, the only common denominator is the "For example," and after the mail merge it is the second paragraph of a statement, but automatically gets a numbering because the first paragraph is setup to have a auto-number.
Basically what that means is that the "For example," paragraph inherits the style of it's predecessor by default, unless I change the structure of the database, which has too many implications for what its worth.

Frosty
02-28-2011, 05:39 PM
AyeSee -- it can search for styles and replace with another style... but it can also search for text and replace with a style.

So you can still use Paul's suggestion. I believe his main point is that your methodology of ".RemoveNumbers" is a bad one, and even though it looks like it is doing what you want it to do, it is likely to cause a problem down the road.

It would be better to use: Selection.Style = "Normal"
In the two places where you currently use:
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph

And then do the rest of your direct formatting (and any more formatting you need to do to make it look the way you want it to).

An even better way would be to define a custom style with all of the formats you want, and apply it rather than Normal (it would also simplify your coding).

Try Recording a macro that does the following:

1. Open Find/Replace
2. Put "For Example," in the Find What box
3. Put your cursor in the Replace With box
4. Click the More button
5. Click Format > Style
6. Select the Normal Style
7. Click Replace
8. Make whatever additional formatting changes you want to that paragraph and the paragraph above

That should give you a bit of a road map...

macropod
03-01-2011, 12:08 AM
The only problem is the desing of my word file is kind of uncommon. The wording comes from an Access database. In all cases, the only common denominator is the "For example," and after the mail merge it is the second paragraph of a statement, but automatically gets a numbering because the first paragraph is setup to have a auto-number.
Basically what that means is that the "For example," paragraph inherits the style of it's predecessor by default, unless I change the structure of the database, which has too many implications for what its worth.
Hi AyeSee,

The fact that you're using a mailmerge suggests your mailmerge main document isn't configured correctly - either the "For example," paragraph is sitting in the body of the document with the wrong formatting, or it's generated as the result of an IF field test. In the former case, simply re-formatting the offending paragraph should suffice. In the latter case, correcting the problem is just a matter of constructing and positioning the IF field correctly in the document. To see how to deal with the latter case, take a look at the "Use a date test to provide pre-printing instructions" topic in my Date Calculation Tutorial, at:
http://lounge.windowssecrets.com/index.php?showtopic=249902 (wlmailhtml:{C7EF91AA-B2C7-45B8-98CA-046449FCFFF9}mid://00000036/!x-usc:http://lounge.windowssecrets.com/index.php?showtopic=249902)
or
http://www.gmayor.com/downloads.htm#Third_party (wlmailhtml:{C7EF91AA-B2C7-45B8-98CA-046449FCFFF9}mid://00000036/!x-usc:http://www.gmayor.com/downloads.htm#Third_party)