Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2006, 09:05 AM
Registered User
 
Join Date: Sep 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Find replace in table crashes

Hello,
I want to search a word table and replace bold text with <b>bold text </b>. (one word per cell, no returns)
I use code shown below, but everytime I start this routine the last bold text hangs up in a loop ending up generating text <b><b>...<b>text</b>>/b>...</b> (infinite loop). I have no clue what is happening and why the last bold text is causing this. All other occurrences of bold text are perfectly converted.

Can anyone help me with a solution either workaround or whatever to get this job done.

Thanks...

Code:
Sub tagBold()
ActiveDocument.Tables(1).Select
With Selection.Find 'ActiveDocument.Content.Find
    .Forward = True
    .Font.Bold = True
    .Execute
    While .Execute 
    .Parent.Text = "<b>" & .Parent.Text & "</b>"
    Wend

End With

End Sub
 
Old September 5th, 2006, 10:14 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Which line crashes? When it "crashes" is there an error message? (If so, knowing what the message is would be a really helpful piece of information to include...)
Your code would benefit enormously from formatting. To wit:
Code:
Sub tagBold()

    ActiveDocument.Tables(1).Select

    With Selection.Find              ' ActiveDocument.Content.Find
        .Forward = True
        .Font.Bold = True
        .Execute

        While .Execute 
            .Parent.Text = "<b>" & .Parent.Text & "</b>"
        Wend

    End With

End Sub
I really don't see what this code does. You use .Find instead of .Replace, so all it would seem to do if working as written is find every occurence (taking no additional action), then run off the end.

I'm really not clear on what Selection.Find.Parent.Text does...

Help says:
Quote:
quote:Parent Property

Returns the parent object of the specified object. Read-only.
The .Parent property has no properties (such as .Text).
Help additionally provides the following example:
Code:
With Selection.Find
    .Text = "hi"
    .ClearFormatting
    .Replacement.Text = "hello"
    .Replacement.ClearFormatting
    .Execute Replace:=wdReplaceOne, Forward:=True
End With
 
Old September 5th, 2006, 10:41 AM
Registered User
 
Join Date: Sep 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello BrianWren,

There is no error message... word does not respond anymore.
All lines work fine but only the last occurence of bold text get's treated/hang up in a loop as described. The while loop takes care of repeating the search.

(I could screencapture the event in a swf movie so you can see it, if only I knew how to get it posted.)


About the code: what it does is
..... find bold text...
..... select the found text being ".Parent.Text"
..... add something to it
..... and replace it again

Code:
Sub tagBold()

    ActiveDocument.Tables(1).Select

    With Selection.Find              ' ActiveDocument.Content.Find
        .Forward = True
        .Font.Bold = True

        While .Execute 
            .Parent.Text = "<b>" & .Parent.Text & "</b>"
        Wend

    End With

End Sub


 
Old September 18th, 2006, 04:31 PM
Registered User
 
Join Date: Sep 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Brian...

I reformulated the problem... please read next and view the video demo I created on this issue....

Hello,
Concerns MSWord:
See also http://home.tiscali.nl/adsl2029/
for detailed video demonstration of the problem.

Task: find and change bold words in tabel 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 as shown in the video.
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find&Replace DoctorWho ASP.NET 1.0 and 1.1 Professional 3 June 6th, 2007 09:40 AM
Find and Replace 2 characters at once!! HELP scovitch65 VB.NET 2002/2003 Basics 2 March 18th, 2006 01:40 AM
Find and replace? Stuart Stalker SQL Server 2000 8 October 13th, 2005 02:49 AM
Create a find and a find and replace in VB.NET snowy0 VB How-To 0 January 26th, 2004 07:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.