finding string in word docs....

V

vonclausowitz

Hi All,

I use following code to search in a Word document for a string of text.
Although I think it should it doesn't always finds formatted text.
Anyone know what is wromg in my code?

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue
End With
Loop

If myStoryRange.Find.Execute = True Then
sTempNaam = rstNaam("bestandsnaam")
Exit Do
End If

Next myStoryRange

Regards
Marco
 
G

Greg Maxey

I do see how your code finds anything or how it even runs. What are you
actually trying to do?
 
V

vonclausowitz

My code finds a string of text in a word document.
The string is taken from an access table. rstNaam("zoekstring")
If the string is found then the document will be saved under a specific
name
which is in the same access table sTempNaam = rstNaam("bestandsnaam").

Marco
 
J

Jezebel

You're getting your loops confused. Currently your code resets the Find
object once for each story range without actually looking for anything; then
does a single search on MyStoryRange, when mystoryrange is nothing.
(Although as it stands, it still doesn't make much sense: you're setting
your temp name once for each time you find the string...)

Your code needs to be something like --

'Iterate the storyRange types
For Each myStoryRange In ActiveDocument.StoryRanges

'Iterate the St0ryRanges of this type
Do

'Set up the Find object for this range
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue

'Look for the string in this range
If while myStoryRange.Find.Execute = True Then
sTempNaam = rstNaam("bestandsnaam")
exit do 'Or do you mean 'exit for' ???
End If
End With

'Get the next range of this type
Set myStoryRange = myStoryRange.NextStoryRange

Loop until myStoryRange.NextStoryRange Is Nothing

Next
 
V

vonclausowitz

This was my whole code, that's why I have the Exit Do in there.

Do While rstNaam.EOF = False

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.ClearFormatting
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue
End With
Loop

If myStoryRange.Find.Execute = True Then
sTempNaam = rstNaam("bestandsnaam")
Exit Do
End If

Next myStoryRange

rstNaam.MoveNext
Loop
 
J

Jezebel

This code does nothing useful ---
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue
End With
Loop
 
J

Jezebel

As previously posted, something like this ---

'Iterate the storyRange types
For Each myStoryRange In ActiveDocument.StoryRanges

'Iterate the StoryRanges of this type
Do

'Set up the Find object for this range
With myStoryRange.Find
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue

'Look for the string in this range
If while myStoryRange.Find.Execute = True Then
--- Do whatever you do when you find the string you're
searching for ---
End If
End With

'Get the next range of this type
Set myStoryRange = myStoryRange.NextStoryRange

Loop until myStoryRange.NextStoryRange Is Nothing

Next
 
V

vonclausowitz

Jezebel,

This is the code I'm running at the moment, which I haven't tested yet:

Dim myStoryRange As Range

sTempNaam = ""
rstNaam.MoveFirst
Do While rstNaam.EOF = False

For Each myStoryRange In ActiveDocument.StoryRanges

'Set up the Find object for this range
With myStoryRange.Find
.ClearFormatting
.Text = rstNaam("zoekstring")
.Replacement.Text = ""
.Wrap = wdFindContinue

'Look for the string in this range
If myStoryRange.Find.Execute = True Then
sTempNaam = rstNaam("bestandsnaam")
Exit Do
End If
End With

'Get the next range of this type
Set myStoryRange = myStoryRange.NextStoryRange

'Loop Until myStoryRange.NextStoryRange Is Nothing

Next
rstNaam.MoveNext

Loop

With wdApp
With .Dialogs(wdDialogFileSaveAs)
.Name = gDiskLetter & sTempNaam & Format(Date - 1,
"YYYYMMDD")
.Show
End With
End With

So open the worddoc and search for the first searchstring in the table.
If the string is found then exit the search and use the found name in
rstNaam("bestandsnaam") to save the document.

Is this working like it is now?

Marco
 
J

Jezebel

Yes, looks right as far as I can see. A couple of minor points, not related
to the searching --

1. 'rstNaam.MoveFirst' will throw an error if the recordset is empty.

2. 'Do While rstNaam.EOF = False' would be better written as 'Do until
rstNaam.EOF' It's better not to use equality tests with booleans. Doesn't
matter with False, but 'If x then' and 'If x = true then' are not the same
thing; the second tends to introduce bugs into your code.
 
V

vonclausowitz

Another question concerning the use of StoryRanges.

Will I also be able to search in Wordart?
and if not can I change wordart back to plaintext so I can search it?

Marco
 
J

Jezebel

Yes, but not using the Find object.

WordArt objects will be in the Shapes or InlineShapes collections (depending
on whether they are floating or inline -- your document might have both).
You can retrieve the WordArt text from the Shape's .AlternativeText
property. You'll have to use text functions -- ie Instr() -- to see if your
word is in there.

Some, but not all, StoryRanges can have Shapes and InlineShapes collections.
I don't know if the .AlternativeText property is valid for all types of
shape.
 
V

vonclausowitz

This is what I did:

Dim myShape As Shape
Dim testWordArt As Variant

Dim sTempNaam As String

For Each myShape In ActiveDocument.Shapes

If Left(myShape.Name, 7) = "WordArt" Then
MsgBox myShape.Name

With myShape
.Select
testWordArt = .AlternativeText

If testWordArt = "THIS IS MY TEXT" Then
sTempNaam = "I FOUND YOUR TEXT"
Exit For
End If
End With
End If
Next

Regards
MArco
 

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