Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

finding string in word docs....

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vonclausowitz@gmail.com - 20 Feb 2006 18:46 GMT
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
Greg Maxey - 20 Feb 2006 20:09 GMT
I do see how your code finds anything or how it even runs.  What are you
actually trying to do?

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi All,
>
[quoted text clipped - 26 lines]
> Regards
> Marco
vonclausowitz@gmail.com - 20 Feb 2006 20:16 GMT
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
Jezebel - 20 Feb 2006 22:25 GMT
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

> Hi All,
>
[quoted text clipped - 26 lines]
> Regards
> Marco
vonclausowitz@gmail.com - 21 Feb 2006 17:37 GMT
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
Jezebel - 21 Feb 2006 21:39 GMT
This code does nothing useful ---

>    Do While Not (myStoryRange.NextStoryRange Is Nothing)
>        Set myStoryRange = myStoryRange.NextStoryRange
[quoted text clipped - 4 lines]
>        End With
>    Loop

> This was my whole code, that's why I have the Exit Do in there.
>
[quoted text clipped - 25 lines]
> rstNaam.MoveNext
> Loop
vonclausowitz@gmail.com - 22 Feb 2006 04:07 GMT
how should my final code look like, cause I'm a bit lost.

Marco
Jezebel - 22 Feb 2006 04:56 GMT
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

> how should my final code look like, cause I'm a bit lost.
>
> Marco
vonclausowitz@gmail.com - 22 Feb 2006 16:34 GMT
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
Jezebel - 22 Feb 2006 21:11 GMT
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.

> Jezebel,
>
[quoted text clipped - 47 lines]
>
> Marco
vonclausowitz@gmail.com - 22 Feb 2006 21:58 GMT
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
Jezebel - 22 Feb 2006 23:02 GMT
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.

> 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
vonclausowitz@gmail.com - 25 Feb 2006 08:31 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.