How to use findall utility for the range

J

Jigar Shah

Hi,

I have created a below function for finding all the occuracne of
"Requirements" in the given range. I am not able to execute it properly.
Can any one suggest me what is the problem ?


Public Function FindInLoopAndFormat(ByVal objRange As Range) As Integer

'Dim oWord As ApplicationClass
'Dim oWD As Document

'Dim Start As Integer
'Dim [End] As Integer
'Dim objFindRange As Object
'Dim objRange As Object
'Dim objFind As Microsoft.Office.Interop.Word.Find

Dim intFound As Integer
Try
'oWord = New ApplicationClass
'oWD = oWord.Documents.Open(Path)
'Dim intFound As Integer
Dim rngResult As Microsoft.Office.Interop.Word.Range
MessageBox.Show("Search Text = " & objRange.Text)
Dim fnd As Microsoft.Office.Interop.Word.Find

Do
'Reseting Range After Finding The Word.
rngResult = objRange.Duplicate
fnd = rngResult.Find

' Find all instances of the word "lorem" and bold each.
fnd.ClearFormatting()
fnd.Forward = True
fnd.MatchWholeWord = True
fnd.MatchSoundsLike = False
fnd.MatchCase = False
fnd.Format = False
fnd.Wrap = WdFindWrap.wdFindStop
fnd.Text = "Requirements"
fnd.Execute()

If Not fnd.Found Then
Exit Do
End If

rngResult.Select()
Debug.WriteLine(fnd.Parent.start)
Debug.WriteLine(fnd.Parent.end)


intFound += 1

Loop Until Not fnd.Found

MessageBox.Show( _
String.Format(fnd.Text & " found {0} times.", intFound), _
"FindInLoopAndFormat")
Catch ex As Exception

End Try

End Function

Another think I want is how to make chm from the word document.
any body knows about any free tool or souce available.
Thanks in Advance

From
Jigar Shah
 
H

Helmut Weber

Hi Jigar,
I don't know about interop,
but using Word VBA, this seems to work.
Find all occurences of "Pending" in the
ranges of paragraph 2 and 3, format them
as bold and red and count them:

Sub test0000()
Dim l As Long
Dim P1 As Long
Dim P2 As Long
Dim rTmp As Range
Set rTmp = Selection.Range
P1 = ActiveDocument.Range.Paragraphs(2).Range.Start
P2 = ActiveDocument.Range.Paragraphs(3).Range.End
rTmp.Start = P1
rTmp.End = P2
ResetSearch
With rTmp.Find
.Text = "Pending"
Do While .Execute And rTmp.End <= P2
rTmp.Font.Bold = True
rTmp.Font.Color = wdColorRed
l = l + 1
Loop
End With
ResetSearch
MsgBox l
End Sub
' -----------
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub


Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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