How to find position of a string or word ?

S

Summer

Hello,

I need to find the character position (start location) of a 'string' or
'word' in the current document .
Eg : The document has a string "<report>" . I have to find the range of this
word or the start location .

I tried the following(Python code). But the find never returned true .

for rge in document.Words:
r.Find.Text = 'mlrepor'
r.Find.Replacement.Text = 'XXX'
r.Find.Wrap = Word.WdFindWrap.wdFindContinue
found = r.Find.Execute(missing, missing, missing, missing, missing,missing,
missing, missing, missing, missing, replaceAll, missing, missing, missing,
missing)

#if (found == True):
# print rge.Start



Thanks !
 
P

Pesach Shelnitz

Hi,

The following is the simplest code that I can think of for doing this
without moving the cursor.

Sub FindAndShowLocation()
Dim myRange As Range

Set myRange = ActiveDocument.Range
If myRange.Find.Execute(findText:="<report>", _
Wrap:=wdFindStop, Forward:=True) Then
MsgBox "Start: " & myRange.Start & vbTab _
& "End: " & myRange.End
End If
End Sub
 
S

Summer

Hi ,

So here is the Python equivalent of your suggestion and it gave the
exception that follows the code .

for r in document.Range:
if (r.Find.Execute("<htmlreport>", missing, missing, missing, missing,
missing, missing, missing, missing, ,\
replaceAll, missing, missing, missing, missing) == True):
print r.Start

Exception :
TypeError: iteration over non-sequence of type <type 'DispMethod'> for the
for loop.

The question I had was if it ran wihtout exception, will the range.Start
thats printed in the MsgBox not be like start location of the Range and not
the actual string ?
 
P

Pesach Shelnitz

Hi,

Your Python code does not seem to me to be equivalent to my macro because it
sets the Replace parameter equal to replaceAll. Try changing replaceAll to
missing.
 
S

Summer

The problem is not resolved yet .
Yeah the python code was not equivalent . But 'ReplaceAll' had nothing to do
with the exception . Its evident that document.Range in this case is not a
list and so we can't iterate over it .
Same Exception as last time .


[code = python]
import clr
import System


clr.AddReference("Microsoft.Office.Interop.Word")

import Microsoft.Office.Interop.Word as Word
#Word.DisplayAlerts = False
missing = System.Type.Missing
replaceAll = Word.WdReplace.wdReplaceAll
source_filename = 'C:\\Sample_Report.doc'
destination_filename = 'c:\\Validation Report.doc'

# Open the Report Template MSWord File
word_application = Word.ApplicationClass()
word_application.Visible = False
document = word_application.Documents.Open(source_filename,missing,missing)

for r in document.Range:
if(r.Find.Execute("<report>", missing, missing, missing, missing, missing,
missing, missing, missing, missing,\
missing, missing, missing, missing, missing) == True):
print r.Start

[/code]
 
P

Pesach Shelnitz

If you are iterating in order to find all instances of your search string,
the VBA code would be as follows.

Sub FindAndShowLocation()
Dim myRange As Range

Set myRange = ActiveDocument.Range
Do While myRange.Find.Execute(findText:="<report>")
MsgBox "Start: " & myRange.Start & vbTab _
& "End: " & myRange.End
Loop
End Sub

In this case, the applicable piece of your Python code should be changed to
the following.

r = document.Range()
while(r.Find.Execute("<report>", missing, missing, missing, missing, missing,\
missing, missing, missing, missing,\
missing, missing, missing, missing, missing) == True):
print r.Start

If you want to find only one instance of the search string, use "if" instead
of "while".

In the case of repeated searches in a loop, there is no need for more than
one Range object, each time the search string is found the boundaries of the
Range are adjusted to the boundaries of the search string found, and, by
default, the next search continues from that point to the end of the doc.

--
Hope this helps,
Pesach Shelnitz


Summer said:
The problem is not resolved yet .
Yeah the python code was not equivalent . But 'ReplaceAll' had nothing to do
with the exception . Its evident that document.Range in this case is not a
list and so we can't iterate over it .
Same Exception as last time .


[code = python]
import clr
import System


clr.AddReference("Microsoft.Office.Interop.Word")

import Microsoft.Office.Interop.Word as Word
#Word.DisplayAlerts = False
missing = System.Type.Missing
replaceAll = Word.WdReplace.wdReplaceAll
source_filename = 'C:\\Sample_Report.doc'
destination_filename = 'c:\\Validation Report.doc'

# Open the Report Template MSWord File
word_application = Word.ApplicationClass()
word_application.Visible = False
document = word_application.Documents.Open(source_filename,missing,missing)

for r in document.Range:
if(r.Find.Execute("<report>", missing, missing, missing, missing, missing,
missing, missing, missing, missing,\
missing, missing, missing, missing, missing) == True):
print r.Start

[/code]





Pesach Shelnitz said:
Hi,

Your Python code does not seem to me to be equivalent to my macro because it
sets the Replace parameter equal to replaceAll. Try changing replaceAll to
missing.

--
Hope this helps,
Pesach Shelnitz


:
 

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