formatting of words in a table throws exception

G

Gabriel

Greetings,
I am have made an application that opens word documents and removes the
shading patterns from all words. since it will not work on paragraphs i
have to do it per word.
all is working well except when there is a table.
the application start going in each cell word by word and fixes it but
at the end of the row it selects something right outside of the table
(like an extra cell) which throws an exception when i run the code to
remove the shading.

code: (i remove more than just shading)

For Each actualword In Doc.Words
'just for visual help
actualword.Select()
actualword.FormattedText.Font.Color = WdColor.wdColorAutomatic
actualword.HighlightColorIndex = WdColorIndex.wdNoHighlight
actualword.Shading.BackgroundPatternColorIndex =
WdColorIndex.wdNoHighlight
actualword.Shading.ForegroundPatternColorIndex =
WdColorIndex.wdNoHighlight
Next



how can i overcome this ?

thank you
 
H

Helmut Weber

Hi Gabriel,

you are encountering problems with the end-of-row mark.

Have a look at this one,
somewhat simplified for ease of testing.
You have selected the end-of-row mark,
if the selection in within a table
and the selection.cells count is zero.

Sub Test45()
Dim rWrd As Range
For Each rWrd In ActiveDocument.Range.Words
'just for visual help
rWrd.Select
If selection.Information(wdWithInTable) = True Then
If selection.Cells.Count = 0 Then
' do nothing
End If
Else
rWrd.FormattedText.Font.Color = WdColor.wdColorAutomatic
rWrd.HighlightColorIndex = WdColorIndex.wdNoHighlight
rWrd.Shading.BackgroundPatternColorIndex _
= WdColorIndex.wdNoHighlight
rWrd.Shading.ForegroundPatternColorIndex _
= WdColorIndex.wdNoHighlight
End If
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

Hi,

not my day, not my time,
If selection.Cells.Count = 0 Then
' do nothing
End If

redundant, absolutely useless.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

Disregard the other two postings.

It may serve as an excuse for coding nonsense,
that Word crashed invariably when testing,
more than a dozen times.

Try to adapt this one to your needs:

Sub Test455()
Dim rWrd As Range
For Each rWrd In ActiveDocument.Range.Words
'just for visual help
rWrd.Select
If selection.Information(wdWithInTable) = True Then
If selection.Cells.Count = 1 Then
rWrd.Font.Color = wdColorRed
' or whatever
End If
Else
rWrd.Font.Color = wdColorRed
' or whatever
End If
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Klaus Linke

I put such "do nothing" comment lines all the time in "If ... Then" or
"Select Case" statements...
Just to make it clear what was intended.

Klaus
 

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