PDA

View Full Version : Solved: ActiveDocument.Range.Find options



Mike100
01-13-2009, 09:23 AM
Can someone please help me understand some off the options available with the .Find command, and some problems that I am having?

First off,


MyRange.Find.Wrap = wdFindContinue / wdFindStop
MyRange.Find.Forward = True / False

Despite reading Microsofts excellent online documentation (try not to laugh) I am still unclear which of the above options I should using, when I should be using them, and what the differences are.

With the code below, the above options don't seem to make any difference when finding and replacing text.


Set MyRange = ActiveDocument.Range
MyRange.Find.Text = "test"
MyRange.Replacement.Text = "replaced"
MyRange.Wrap = wdFindStop
MyRange.Forward = True
MyRange.Find.Execute Replace:=wdReplaceAll



Secondly:

Why doesnt the code below work - is it something to do with the Range being redefined when the highlighting is being cleared?


Private Sub CommandButton1_Click()
Set MyRange = ActiveDocument.Range

' Clear highlighting between '[' and ']' characters
MyRange.Find.Text = "\[*\]"
MyRange.Find.Wrap = wdFindStop
MyRange.Find.MatchWildcards = True
Do While MyRange.Find.Execute = True
MyRange.HighlightColorIndex = 0 'Clear highlighting
Loop

' Delete all '[' characters
MyRange.Find.Text = "["
MyRange.Find.Replacement.Text = ""
MyRange.Find.MatchWildcards = False
MyRange.Find.Execute Replace:=wdReplaceAll

' Delete all ']' characters
MyRange.Find.Text = "]"
MyRange.Find.Execute Replace:=wdReplaceAll
End Sub



Thirdly:

Is there a vba help file anywhere with Word 2003 ?



And finally:

Can anyone recommend a good book on Word vba.


Kind regards in advance,
Mike.


Edit Lucas: VBA tags added to code. Select your code when posting and hit the vba button to format it for the forum.

lucas
01-13-2009, 10:00 AM
You can search this forum. I usually try to help with this but I'm short on time. You can do it though.....

TonyJollans
01-13-2009, 03:19 PM
One at a time ...

MyRange.Find.Wrap = wdFindContinue / wdFindStop

Find & Replace acts on a Range, which may be only part of a document. In the Word UI if you select part of a document and do an F&R operation, when it gets to the end of the Selection you will be prompted to that effect and asked if you want to continue; when it reaches the end of the Document you will be prompted again whether to wrap to the beginning of the document (one or other of these prompts may not occur if the Selection started at the start of the document or ended at the end of the document). The Wrap property controls how these prompts are dealt with when the F&R is run from VBA. The values are wdFindAsk (show the prompts for the user to answer), wdFindContinue (automatically reply Yes to the prompts), and wdFindStop (automatically reply No to the prompts).

When you act on the whole document (as in your example) the Range begins at the beginning of the document and ends at the end of the document and so there are no prompts and so the value of the property is irrelevant.

MyRange.Find.Forward = True / False

In the UI you can choose whether to look forward or backward; this is the VBA equivalent. Doing a search with wdReplaceAll, rather than one looking for a single instance at a time, it makes no difference (either in the UI or in VBA).

TonyJollans
01-13-2009, 03:30 PM
Why doesnt the code below work - is it something to do with the Range being redefined when the highlighting is being cleared?

Yes.


Is there a vba help file anywhere with Word 2003 ?

Yes.


Can anyone recommend a good book on Word vba.

I haven't written it yet!

A little bit more ...

myRange (the Range being used for the F&R) is redefined each time it finds a result so after the loop setting the highlight, it will be the last occurrence found. For the next search to work as you want you need to reset it to the document Range again.

Do you not get help in VBA? It should be there unless it wasn't installed (some sysadmins seem to think it's somehow clever not to install it for users - they should be taken out and shot).

I don't really know a good book - maybe someone else does. You might like to start here (http://word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm)

fumei
01-14-2009, 10:11 AM
1. It is not just sysadmins who do not install help. Help is not installed by default, and that is Microsoft's decision. In other words, Help has to be installed by an explicit decision to do so. Automated installs (or just installing) will not.

2. "Why doesnt the code below work - is it something to do with the Range being redefined when the highlighting is being cleared?"

Try using With statements, as has been suggested to you.

3. If you can find them (probably in a second-hand store) The VBA Developer's Handbook, and Word VBA - both by Sybex - are very useful. These only cover up to Word 2000, but even at that, they are VERY good for grasping a lot of concepts and objects.

TonyJollans
01-14-2009, 12:30 PM
2. "Why doesnt the code below work - is it something to do with the Range being redefined when the highlighting is being cleared?"

Try using With statements, as has been suggested to you.

In this case it still wouldn't work.

fumei
01-14-2009, 12:56 PM
Technically, yes you are correct, in that I did not mention that you would have to re-Set the range object. Other than that (an important point I admit), yes it would work.

Demo attached. Click "Remove Highlight and Brackets" on the top toolbar.

Code:

Dim MyRange As Range
Set MyRange = ActiveDocument.Range

' Clear highlighting between '[' and ']' characters
With MyRange.Find
.Text = "\[*\]"
.MatchWildcards = True
Do While (.Execute = True) = True
'Clear highlighting
MyRange.HighlightColorIndex = 0
Loop
End With
Set MyRange = ActiveDocument.Range

' Delete all '[' characters
With MyRange.Find
.Text = "["
.Replacement.Text = ""
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
' Delete all ']' characters
Set MyRange = ActiveDocument.Range
With MyRange.Find
.Text = "]"
.Execute Replace:=wdReplaceAll
End With

fumei
01-14-2009, 01:11 PM
Mike, the above code I posted actually reiterates Tony's point.

The Range object must exist properly in order to work. I adjusted your code using With statements (my point), but the ESSENTIAL point (Tony's) is that the Range object must be properly existing. My code does this:

1. Declare the range object for the entire document (Set MyRange = ActiveDocument.Range)

2. actions that object to remove the highlights. Using .Find of a Range object re-sizes the range as it processes.

3. RE-Sets the range object to be the entire document again.

4. actions that object to remove the "["

5. RE-Sets the range object to be the entire document again.

6. actions that object to remove the "]"

Mike100
01-15-2009, 03:38 PM
Many thanks for all your help again guys.

Your comments all now make sense.

I'm starting to get to grips with Word VBA now.

I've got the Help file working and have ordered a few Word VBA books.

Cheers

Mike

fumei
01-16-2009, 10:07 AM
Good for you. Keep at it. I do not know how comfortable you are with studying an object model, but it really helps to get a solid grip on certain objects with the Word object model. IMO, there are three absolutely crucial ones:

1. Paragraph. So much of how Word works is tied to what a Paragraph really means. In particular, how a Style functions in relation with paragraphs.

2. Range. Once you understand what a Range really is (and does) you will be able to make Word dance MUCH better.

3. Section. For example, many people do not truly understand headers and footers. They think of them as connected to "pages", as that is how we see them in the document. However, they are not really. Within the object model, headers and footers are child objects of Sections. Therefore to REALLY grasp headers and footers, you need to grasp Sections.

Good luck. I know Word VBA seems....odd, sometimes.

If you ever get stuck, you can always post a question. It does not have to be an explicit problem with a document. Conceptual questions are equally valid to ask.