?
Solved

Find-VBA Operation falling into an endless loop in tables

Posted on 2009-07-12
4
Medium Priority
?
407 Views
Last Modified: 2012-06-27
Dear Experts:

below macro counts the number of occurrences of bold formatting in a document. It is running fine on documents WITHOUT tables. As soon as there is a BOLD FORMATTED WORD within a TABLE, the macro falls into an endless loop.

How do I have to change the code to include also tables?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Sub CountNumber_FindOperation
 
Dim rngStory As range
Dim i As Long
 
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
    With rngStory.Find
      .Font.Bold = True
      While .Execute
        i = i + 1
      Wend
    End With
If i > 0 Then
  MsgBox "Occurrences of bold formatting found " & i & " time(s)."
Else
  MsgBox "No bold formatting found", vbOKOnly, "No bold formatting!"
  End If
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24833937
do not use the with with the .execute.
syntax: ok
side-effect: endless loop.

I had th esame problem once also ...

Sub CountNumber_FindOperation
 
Dim rngStory As range
Dim i As Long
 
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
rngStory.Find.Font.Bold = True
While rngStory.Find.Execute
  i = i + 1
Wend 
If i > 0 Then
  MsgBox "Occurrences of bold formatting found " & i & " time(s)."
Else
  MsgBox "No bold formatting found", vbOKOnly, "No bold formatting!"
  End If
End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 24834034
Hi angelIII,
thank you very much for your quick answer. I just tried out your code. I am afraid to tell you that the macro still falls into an endless loop as soon as it hits bold formatted words in a table.

Any idea why?

Help is much appreciated. Thank you very much in advance. Regards, Andreas
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834047
sorry...
you have to check the Found property also
Sub CountNumber_FindOperation
 
Dim rngStory As range
Dim i As Long
 
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
rngStory.Find.Font.Bold = True
rngStory.Find.Execute
While rngStory.Find.Found
  i = i + 1
  rngStory.Find.Execute
Wend 
If i > 0 Then
  MsgBox "Occurrences of bold formatting found " & i & " time(s)."
Else
  MsgBox "No bold formatting found", vbOKOnly, "No bold formatting!"
  End If
End Sub

Open in new window

0
 

Accepted Solution

by:
AndreasHermle earned 0 total points
ID: 24834114
Hi angelIII,

I am afraid to tell you that the macro still falls into an endless loop. Maybe I got a corrupt document but I tried it out on several documents.
Anyhow, in the meantime I found  out thru an internet query that this bug seems to be caused by 'end-of-cell-markers' in the matched text. It is recommended to move the range. I did exactly this, i.e. I just added 'rng.move, 1'. And now it works!

Anyhow, thank you very much for your kind and terrific help.

Regards, Andreas


Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
rngStory.Find.Font.Bold = True
While rngStory.Find.Execute
  i = i + 1
  rngStory.Move , 1 ' Added by Andreas Hermle
Wend
If i > 0 Then
  MsgBox "Occurrences of bold formatting found " & i & " time(s)."
Else
  MsgBox "No bold formatting found", vbOKOnly, "No bold formatting!"
  End If

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question