Log in

View Full Version : [SOLVED:] find style and paragraphformat issue



HenryAte
11-25-2008, 10:08 AM
Hi There.

Seriously hoping someone can help with this one.

I need to find an occurrence of a specific style, The problem is that word seems to be adding in some weird stuff.

Using word 2003 on Win XP Pro.

Steps followed.
1) open doc
2) record a macro to find a style
3) style is found, end recording.
4) run macro - no results found.
5) go back into word and record a second macro
6) open find dialogue and click find next to see what happens, no results found.
7) stop recording and look at code of second macro
8) more than 20 lines of paragraphformat stuff added to find function.

Here is the code from the first recording


Sub Macro1()

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("LG-section")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

then the second recorded macro comes out like so


Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("LG-section")
With Selection.Find.ParagraphFormat
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorAutomatic
End With
With .Borders
.DistanceFromTop = 0
.DistanceFromLeft = 0
.DistanceFromBottom = 0
.DistanceFromRight = 0
.Shadow = False
End With
End With
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

Does anyone have any suggestions on how I can get it to find a style?

Many thanks indeed

fumei
11-25-2008, 10:14 AM
This is a prime example of the difference between understanding and writing macro code, and recording macro code.

Recording ONLY uses Selection, and when actioning paragraphs sets ALL parameters.

You do not specify what structure you wish to find a style for. Let's assume any amount of text.


Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Style = "LG-section"
Do While .Execute(FindText:="", _
Forward:=True)=True
' do whatever it is with the chunk
' found with the LG-section style
Loop
End With

This finds all instances of text with the LG-section style.

On the other hand, if you want to find paragraphs with LG-section:

Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "LG-section" Then
oPara.Range.HighlightColorIndex = wdBrightGreen
End If
Next

This uses paragraph objects. Any paragraph with the LG-section style will (in this example) be highlighted bright green. Notice the above does NOT use Find.

HenryAte
11-25-2008, 10:22 AM
Ok, Thanks for the rapid reply, sadly it does the same thing. doesnt find anything.

It is a macro that used to run perfectly in word 2000, Have been using selection.find to find a paragraph with a specific style for ages now. All it needs to do is find the first occurance of a paragraph with that style and place the cursor at that point. I then do the remainder of the the needed stuff to that paragraph then re-call the find to move to the next paragraph.

fumei
11-25-2008, 10:43 AM
Perhaps it would help if you stated what you are trying to do.

Do not use Selection if you do not have to. WHY do you need to "find the first occurance of a paragraph with that style and place the cursor at that point."

There is no need, whatsoever, to put the cursor anywhere, in order to "do the remainder of the the needed stuff to that paragraph " (although it may help if you actually stated what that is...); nor to move to the next paragraph.

Demo attached. I created a LG-section style, and applied it to two paragraphs. Click "LG Section yadda" on the top toolbar.

It finds the paragraphs with LG-section style and makes them bright green.

My point being is that my action (making it bright green) can be ANYTHING. Whatever is the "remainder of the the needed stuff to that paragraph" can be done.

Note that it works without using Selection, or Find....at all.

HenryAte
11-25-2008, 10:48 AM
basically what I am doing is finding the LG-section paragraph, then I need to pull out a section number and some other numeric data from that, then I need to select from the start of that paragraph up til just before the next occurance of LG-section. All of this is then put into a secondary document.

Will have a look at the example you have put up.

fumei
11-25-2008, 11:08 AM
If you are going to be doing stuff like that then I strongly recommend you use Range, not Selection.

Also, work out, PRECISELY, what your logic is.

"I need to select from the start of that paragraph up til just before the next occurance of LG-section."

Does that mean there may be paragraphs NOT LG-section style in that chunk of text - and therefore NOT included in the Found?

In other words:

1. find LG-section
2. get some stuff out of that Found chunk
3. extract the Found chunk AND any other chunks after that chunk, up to the next LG-section. No matter what style those additional chunks are.

If this is what you are trying to do (and I am not clear on this), it can be done, but it would take a bit more fussing and work.

HenryAte
11-25-2008, 11:37 AM
yep. the LG-section is a header type paragraph, that header and all subsequent paragraphs regardless of style need to be selected. Using range is the way it should have been done originally but it wasnt. This is mainly a patch up job to try get the horrible thing working in 2003. It was originally written by a contractor a number of years ago.

The thing I really dont get though is why a selection.find worked in word 97-word xp and then suddenly in 2003 it stops working. thats a bit backward if you ask me.

HenryAte
11-26-2008, 02:38 AM
ok i managed to get the beast working. Thanks for the replies and help though. greatly appreciated. solution was :

Selection.Find.Style = ActiveDocument.Styles("LG-section")
Selection.Find.Font.Bold = True
With Selection.Find .Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = False
End With
Selection.Find.Execute
does the job and means I have no need to modify the remainder of the macro. I can cross this horrible monstrosity off of my to-do list.

fumei
11-26-2008, 10:21 AM
Well I am glad you got it working for you.

It is still better to NOT use Selection...but...shrug....whatever. The most important thing is to have a functioning macro.

If this is essentially closed for you, please mark the thread as "Solved".

Thanks.