Endless loop if macro tries to process text in a table cell, otherwise running fine.

A

andreas

Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.

Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.

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


Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End

With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With

End Sub
 
D

Doug Robbins - Word MVP

I would do it this way:

Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

andreas

I would do it this way:

Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP




Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.
Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.
Help is appreciated. Thank you very much in advance.
Regards, Andreas
Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End
With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With
End Sub- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,

thanks for your quick answer. Regrettably it does not work. The macro
falls into an endless loop and is doing nothing.
Got an idea why this is so?

Regards, Andreas
 
D

Doug Robbins - Word MVP

It worked fine for me for the document that I set up, whether the text
within ( ) was within the cell of a table or elsewhere in the document.

Send me a copy of the document if you like and I will check it out.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

andreas said:
I would do it this way:

Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP




Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.
Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.
Help is appreciated. Thank you very much in advance.
Regards, Andreas
Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End
With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With
End Sub- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,

thanks for your quick answer. Regrettably it does not work. The macro
falls into an endless loop and is doing nothing.
Got an idea why this is so?

Regards, Andreas
 
H

Helmut Weber

Hi Andreas,

if you extend a range from within a cell
to the end of a document, then the start of
the range moves to the start of the cell.
Shouldn't do so, but it does.

My decent attempts to overcome this failed.
So I arrived at a workaround.

Sub Test40Z()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
While .Execute
rDcm.HighlightColorIndex = wdBrightGreen
Wend
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Highlight = True
While .Execute
If rDcm.HighlightColorIndex = wdBrightGreen Then
rDcm.HighlightColorIndex = wdNoHighlight
ActiveDocument.Range.InsertAfter rDcm.Text & vbCr
rDcm.End = rDcm.End - Len(rDcm.Text) - 1
' there might be more to it than you have
' thought of at first :)
End If
Wend
End With

End Sub

You could use a formatting which is otherwise not used
instead of highlighting. But that's mere theory.

You get the idea, I'm sure.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

andreas

I would do it this way:

Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP




Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.
Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.
Help is appreciated. Thank you very much in advance.
Regards, Andreas
Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End
With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With
End Sub- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,
you are right, it is working perfectly. THANK YOU. I tried it out on a
new fresh file and it ran fine there. The following day I got an error
message on the file on which I initially tested my macro, saying that
a table may be corrupt. Hence, the file where I tried out your macro
first had one or several corrupt tables.

Now here comes a new question:
How can I single out a corrupt table, if, say, there are more than 20
in a document? Is there a special procedure to follow to isolate a
corruption to a specific table?

Help is appreciated. Thank you again for your valuabel help in this
forum.
 
A

andreas

Hi Andreas,

if you extend a range from within a cell
to the end of a document, then the start of
the range moves to the start of the cell.
Shouldn't do so, but it does.

My decent attempts to overcome this failed.
So I arrived at a workaround.

Sub Test40Z()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
While .Execute
rDcm.HighlightColorIndex = wdBrightGreen
Wend
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Highlight = True
While .Execute
If rDcm.HighlightColorIndex = wdBrightGreen Then
rDcm.HighlightColorIndex = wdNoHighlight
ActiveDocument.Range.InsertAfter rDcm.Text & vbCr
rDcm.End = rDcm.End - Len(rDcm.Text) - 1
' there might be more to it than you have
' thought of at first :)
End If
Wend
End With

End Sub

You could use a formatting which is otherwise not used
instead of highlighting. But that's mere theory.

You get the idea, I'm sure.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Helmut,

as always, the things you are writing are running perfectly. Thank
you, Helmut. The workaround looks awesome. I sometimes wonder how the
brain structure of you and your programmer buddies must look like.
Regards, Andreas
 
H

Helmut Weber

Hi Andreas,

Doug's way not to paste immediately after something was found,
but to collect and concatenate the expressions in brackets
is pretty smart, by the way.

On the corrupt table issue,
Klaus Linke suggested the following:

"If the tables are uniform (= no merged cells),
and don't have complicated formatting,
it may be simplest to convert all tables to text,
then back to table, with a macro."

More ideas here: http://tinyurl.com/2oz7nz

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

Otherwise, you would get into an endless loop for sure, table or no table.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

One way to find a corrupt is to split the document into two, then run the
macro on each half. Then split the half where it goes into an enless loop
into two and try it again. If you keep splitting the half that fails into
two, you will eventually end up with a document in which it fails that just
contains one table, which will be the corrupt one.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

andreas said:
I would do it this way:

Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP




Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.
Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.
Help is appreciated. Thank you very much in advance.
Regards, Andreas
Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End
With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With
End Sub- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,
you are right, it is working perfectly. THANK YOU. I tried it out on a
new fresh file and it ran fine there. The following day I got an error
message on the file on which I initially tested my macro, saying that
a table may be corrupt. Hence, the file where I tried out your macro
first had one or several corrupt tables.

Now here comes a new question:
How can I single out a corrupt table, if, say, there are more than 20
in a document? Is there a special procedure to follow to isolate a
corruption to a specific table?

Help is appreciated. Thank you again for your valuabel help in this
forum.
 
H

Helmut Weber

Hi Doug,
Otherwise, you would get into an endless loop for sure, table or no table.

not if you deduct from the end of the range to be searched
the length of what you have inserted at the doc's end.

Your approach is much smarter, though.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

andreas

One way to find a corrupt is to split the document into two, then run the
macro on each half. Then split the half where it goes into an enless loop
into two and try it again. If you keep splitting the half that fails into
two, you will eventually end up with a document in which it fails that just
contains one table, which will be the corrupt one.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP




I would do it this way:
Dim str As String
str = ""
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\([!\)]@\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True
str = str & Selection.Range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.Range.InsertAfter str
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP

Below macro which I recently wrote is supposed to COPY all the text
that has been placed between brackets TO the end of the document and
list them one below the other. It is running fine with one exception.
If text between brackets is found in a table cell, the macro falls
into an endless loop. In theses cases, it copies the text between the
brackets over and over again and won't stop.
Hence, how do I have to re-write this code so that text between
brackets located in a table cell is also properly copied and put at
the end of the document.
Help is appreciated. Thank you very much in advance.
Regards, Andreas
Dim rng As Range
Dim rngEnd As Long
Set rng = ActiveDocument.Range
rngEnd = ActiveDocument.Range.End
With rng.Find
.Text = "\([!\)]@\)"
.MatchWildcards = True
Do While .Execute()
rng.Copy
ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertAfter
vbCr
rng.Collapse wdCollapseEnd
rng.End = rngEnd
Loop
End With
End Sub- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Doug,
you are right, it is working perfectly. THANK YOU. I tried it out on a
new fresh file and it ran fine there. The following day I got an error
message on the file on which I initially tested my macro, saying that
a table may be corrupt. Hence, the file where I tried out your macro
first had one or several corrupt tables.
Now here comes a new question:
How can I single out a corrupt table, if, say, there are more than 20
in a document? Is there a special procedure to follow to isolate a
corruption to a specific table?
Help is appreciated. Thank you again for your valuabel help in this
forum.- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Doug,

ok, great help. Thank you
Regards, Andreas
 
A

andreas

Hi Andreas,

Doug's way not to paste immediately after something was found,
but to collect and concatenate the expressions in brackets
is pretty smart, by the way.

On the corrupt table issue,
Klaus Linke suggested the following:

"If the tables are uniform (= no merged cells),
and don't have complicated formatting,
it may be simplest to convert all tables to text,
then back to table, with a macro."

More ideas here:http://tinyurl.com/2oz7nz

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Helmut, ok. Yes I am fully aware that Doug's way to code is really
great. As for isolating the corruption to a specifc table, I'll give
both procedures a try and found out which one suits me best.
I guess the "splitting" procedure may be not some cumbersome. Let's
see!
Helmut, thank you and have a nice weekend.
 

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