Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find.ParagraphFormat fails

Status
Not open for further replies.

artj

Technical User
Oct 28, 2003
13
US
I'm trying to create a Word 2k macro that locates a paragraph based on the left indent as well as the font formatting, and then changes the style. Unfortunately, the Find fails, so the Find.Execute commands are applied to the currently selected paragraph. Code as follows:

Selection.Find.ClearFormatting
With Selection.Find
.Font.Name = "Arial"
.Font.Size = 10
.Font.Bold = True
.Font.Color = RGB(0, 128, 0)
.ParagraphFormat.LeftIndent = 0.56
End With
Selection.Find.Execute
Selection.ParagraphFormat.Style = "Heading 2"
Selection.MoveDown Unit:=wdLine, Count:=1

I'm new at this (obviously) so any help will be appreciated. Also, I would love to have the macro change the styles throughout the entire document and stop when it reaches the end of the document.

AJ
 
I'll give you a tip. And i think you'll find the problem.

Try to use find/search from the Word menu (CTRL+F) and see if it finds what you want.

If so then go to Macros->Record New macro and then do eaxctly as before press search and when finds it just stop recording and then press ALT+F11 and check the Module section of, you'll find there the right settings for your find/search.

Hope you'll solve it.

________
George, M
 
Hi AJ,

Finding indents is a little bit awkward as the dialog doesn't give you all the options. Find requires indenting to be specified in points. 0.56 points is an unlikely indent (0.02cm) and I suspect you may have inches or centimeters. Try using InchesToPoints(0.56) or CentimetersToPoints(0.56) as appropriate.

As for acton after the find, you can check Selection.Find.Found for True or False and take appropriate action depending on the result. This check can also be used to control a loop. Start at the beginning and loop until the Find fails, as shown below. Note that you must 'collapse' the selection before re-executing the Find or it will limit the search to the Selected result of the previous Find.

Code:
Selection.Find.ClearFormatting
With Selection.Find
    .Font.Name = "Arial"
    .Font.Size = 10
    .Font.Bold = True
    .Font.Color = RGB(0, 128, 0)
    .ParagraphFormat.LeftIndent = CentimetersToPoints(0.1)
End With
Code:
Selection.HomeKey wdStory

Do
Code:
    Selection.Find.Execute
    Selection.ParagraphFormat.Style = "Heading 2"
Code:
    Selection.Collapse wdCollapseEnd
Loop While Selection.Find.Found
Code:

Enjoy,
Tony
 
Thanks for the responses.

George, I've already tried that approach. I used the Find feature to create to the original macro that isn't working.

Tony, I tried your code and am still running into the same problem. The macro still fails to find the selection. It's probably something I'm doing, but I can't figure out what. Any suggestions?

AJ

 
Hi artj,

Oops! [blush] I don't check for not found on the first Execute so if there are no matches then you will find a problem.

However you suggest there should be some matches so there is something in your Find which does not exactly match the criteria - hard for me to say what it is; I suggest you try removing the criteria one by one until you do get a match and then adjusting the failing criterion until you find the problem - good, old fashioned, debuggging which I can't help much with from a distance.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top