Multiple text selection in Word

I

Igor

Hi everyone!

I have a small problem; please help me to solve it if somebody knows.

I have to find and select all occurrences of any string in Word document.
I'm working with Word document by OLE.
The sample code is :
If (oWord_FoundRange.Find.Execute("Any string", , , , , , True)) Then
oWord_FoundRange.Select
End If
oWord_Document.Activate

But it selects only the first occurrence of searching string. I have to
highlight all occurrences. In addition, I have not to change the document,
colors etc.

On other words, I have to simulate the same behavior like check box of the
Word 2003 of the Find window "Select all elements, found in".

Please, help me!

Thanks!

Igor.
 
S

Shauna Kelly

Hi Igor

As far as I know, Word's object model does not expose any methods or
properties to allow you to select multiple ranges. As you said, you can do
it through the UI, but it's not possible in code.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
I

Igor

Thanks!

I think, I found another option to highlight multiple ranges. Just add
bookmarks and highlight each of them.

Best regards!
 
H

Helmut Weber

Hi Igor,

I usually recommend to use highlighting
as a substitute for a multiple selection.

Sub SetHighLight()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "quick"
While .Execute
rDcm.HighlightColorIndex = wdYellow
Wend
End With
End Sub

' now you can do whatever you want with the highliohted text.

Sub RemoveHighLight()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = ""
.Highlight = True
While .Execute
If rDcm.HighlightColorIndex = wdYellow Then
' your code
rDcm.HighlightColorIndex = wdNoHighlight
rDcm.Collapse Direction:=wdCollapseEnd
rDcm.End = ActiveDocument.Range.End
End If
Wend
End With
End Sub

In theory, there are limits,
if your doc contains all possible kinds of hightlighting.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Klaus Linke

Word 2007 has a new .HitHighlight method.
From what I've seen, you can use it instead of .Execute in a Find.

But I don't have Word2007 installed and can't test how/if it works...
Not even sure whether "highlight" means "select", or if it's just redundant
for ".Find.Highlight=True".

Regards,
Klaus
 
I

Igor

I made as you offered and it works fine. But I have another problem.

I opened document not directly, but in Visual Basic 6.0 WebBrowser control.
When I selected any text the Saved property of the document did not change,
but when I highlighting even one character, I cannot prevent the WebControl
from displaying save message. Undo method also has no effect.
Even if I set oWebControl.Document.Saved = true, the message displayed
anyway and WebControl performs save on Yes button. The same trick works well
if I open Excel document.

How can I force WebControl not to save Word document.

Thanks!
 

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