Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Word] Find..Replace individual per find action 2

Status
Not open for further replies.

pieterdeboer61

Technical User
Aug 27, 2006
4
NL
Hello,
Concerns MSWord:
See also for detailed video demonstration of the problem.

Task: find and change bold words with same words surrounded by bold tags: boldword = <b>boldword</b>
Code used is shown below and runs fine until last boldword, which the procedure keeps selecting infinitely....

Any solutions welcome
Thanks:

By the way code assumes tabel with content present.
Code:
Sub boldTags()
'Er wordt een LxM tabel aanwezig verondersteld in het word document.
'Elke cel kan een bold woord bevatten. Dit woord wordt dan gezocht en voorzien van
'omringende tags.... bijvoorbeeld:  <b>bold_woord</b>

'boldWordFound zal steeds het gevonden bold woord bevatten
Dim boldWord As String
boldWord = ""

'er is maar een enkele tabel en deze wordt geselecteerd
ActiveDocument.Tables(1).Select

'Binnen de selectie, gebruik Find als volgt
With Selection.Find
    .Forward = True                'geeft de richting aan waarin gezocht wordt
    .Font.Bold = True              'geeft het bold type aan waarna gezocht zal worden
    
    '====HIER GAAT HET STRAKS MIS: bij het laatst gevonden boldWord blijft het programma loopen
    'zolang er bold text gevonden wordt doe hetvolgende
    While .Execute
    boldWord = .Parent.Text    'hiermee wordt de text in de variabele boldWord geplaats
    .Parent.Text = "<b>" & boldWord & "</b>"
    'controle msgbox toont gevonden woord. Bij "loopen" de Cancel knop gebruiken om netjes af te sluiten
    If MsgBox("gevonden woord = " + boldWord, vbOKCancel) = vbCancel Then GoTo ENDSUB
        
    Wend
    
End With

ENDSUB:
End Sub
 
pieterdeboer61,
[tt]Execute[/tt] does not tell you if something was found, your might try replacing it with [tt]Found[/tt].

Code:
...
    '[s]While .Execute[/s]
    [b]Do[/b]
    .Execute
    boldWord = .Parent.Text    'hiermee wor...
    .Parent.Text = "<b>" & boldWord & "</b>"
    'controle msgbox toont gevonden woord. ...
    If MsgBox("gevonden woord = " + boldWord, vbOKCancel) = vbCancel Then GoTo ENDSUB
    [b]Loop Until Not .Found[/b]
    '[s]Wend[/s]
...

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Thanks for you reply...

Unfortunately the same effect takes place at the last bold found word with your suggested code.

<regards>
 
A work around:
Do
.Execute
boldWord = .Parent.Text 'hiermee wor...
If Left(boldWord, 3) = "<b>" Then Exit Do
.Parent.Text = "<b>" & boldWord & "</b>"
'controle msgbox toont gevonden woord. ...
If MsgBox("gevonden woord = " + boldWord, vbOKCancel) = vbCancel Then Exit Do
Loop Until Not .Found

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
pieterdeboer61,
Last thought, try setting the [tt]Wrap[/tt] option:
Code:
...
'Binnen de selectie, gebruik Find als volgt
With Selection.Find
    .Forward = True                'geeft de richting aan...
    .Font.Bold = True              'geeft het bold type a...
    [b].Wrap = wdFindStop    [/b]
    '====HIER GAAT HET STRAKS MIS: bij het laatst gevonde...
'zolang er bold text gevonden wordt doe hetvolgende
    While .Execute
    ...

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Here is an alternative. It will find every word that is bold and put the bold tags before and after. It includes the lasy bold word as it uses the True or False success of .Execute.
Code:
Sub TagBoldWords()
Selection.HomeKey Unit:=wdStory
With Selection.Find
  .Font.Bold = True
  Do While (.Execute(findtext:="", Forward:=True) = True) _
      = True
    With Selection
      .InsertBefore Text:="<b>"
      .Collapse Direction:=wdCollapseEnd
[COLOR=red]' need to use explicit Selection Font Bold
' previous one was for Find[/color red]
      .Font.Bold = False
      .TypeText Text:="</b>"
      .Collapse Direction:=wdCollapseEnd
    End With
  Loop
End With
End Sub

Gerry
My paintings and sculpture
 
Hello Gerry,

Unfortunately this code run into a hangup as well. I do have a working workaround now but this means you really have to check for the bold tag to be present and then stop the find process.

Thanks
 
I am not following.

What hangup?

Also, I may not have understood the original post. Are you saying there are bold tags to begin with? I thought it was to looking for bold text, and ADDing tags.

Gerry
My paintings and sculpture
 
Hello Gerry,

You are right. There are no bold tags to start with. What I found out in a "clean" test of your code is that it works perfectly. Probably in earlier test there was some "left over" probably something in the word table itself. It is good to see that there is code for this functionality without checking on bold tags. This approach also allows me to run through the table and check for additional underline and italic presence without extra "tag-checking" code.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top