How do I search for a Word style without flagging end-of-row marke

B

Benjamino5

I need to search for all styles that are not on a whitelist of approved
styles. When I find an "illegal" style, I need to highlight the text in red.
So far, so good.

The problem is that end-of-row markers in tables get flagged if they have an
illegal style. I want them to be skipped, but I'm not sure how to approach
that. Any ideas?

Here's a code fragment:

___________________________________
' this is part of a loop; "s" is a style; "adoc" is the active document
'
If s.InUse = True And s.Type = 1 And Not IsInList(s.NameLocal, whitelist) Then
wd.app.options.DefaultHighlightColorIndex = 6 'wdRed
With adoc.range.Find
.ClearFormatting()
.Text = ""
.Style = s.NameLocal
.Replacement.Highlight = True
.Replacement.Text = ""
.Execute(Replace:=2, Format:=True) 'wdReplaceAll
If .Found = True Then
' something was found, but it MIGHT be an
end-of-cell marker
' how do I handle it here??


' add the name to the foundList and log
' if it's not in there already
If Not foundList.Contains(s.NameLocal + ",")
Then
foundList = foundList + s.NameLocal + ","
End If
End If
End With
End If
 
B

Benjamino5

A quick clarification: I'm actually writing in VB.Net, which is why there are
expressions like "foundList.Contains..." and I'm using "style.NameLocal"
instead of just "style."

But the problem I have is strictly a VBA, not VB.Net, problem, so that's why
I'm posting here.

Thanks!
 
D

Doug Robbins - Word MVP

You should be able to use the .Information(wdWithinTable) attribute of the
Range object to determine if the range you are dealing with is in a table
and act accordingly.

--
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
 
B

Benjamino5

Doug,

Once I know the range is inside a table, how can I find out if it's an
end-of-row marker? If it's a regular table cell, I want to highlight the
range as normal--it's only the end-of-row markers themselves I want to avoid
flagging.

Thanks so much for your help--I really appreciate it.

Ben
 
D

Doug Robbins - Word MVP

When using the .Range of a cell to get its contents, I set the .End of the
range to .End-1 to eliminate the end of cell marker. So I guess if you set
the .Start of the range to .End - 1, you will probably have the end of cell
marker in the range.

--
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
 
B

Benjamino5

Thanks for your suggestions, Doug! But I was actually trying to find the
end-of-row marker, not the end-of-cell marker.

Maybe it's easier if I explain the big picture of what I'm trying to do:

I'm running a Find. The Find looks for instances of a certain paragraph
style. When it finds them, it highlights them in red.

The problem is that some of those instances of the style are actually just
end-of-row markers; not cells or or end-of-cell markers or even any text at
all; just the spot after the last cell but before the end-of-row marker,
where you can put your cursor, but where you can't type (anything you type
gets dumped into the next row).

I want the code to skip right over those places, and not flag them as having
the style. But I'm not sure how to do that.

Perhaps I'm approaching this the wrong way. But in my code sample below,
when I find something, i.e. when .Found=True, I want to figure out if that
range itself, the found range, is JUST that spot at the end of a row.

Not sure if I'm explaining this well, but any suggestions for how to
approach this would be quite welcome.

Thanks again, Doug!

Ben
 
D

Doug Robbins - Word MVP

Can you use the Len() of the range. I imagine that if the found range is
just the end of row marker, it will have a length of (probably) not more
than 1 while the ranges upon which you want to act, would have a length
somewhat greater than that.

--
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
 
B

Benjamino5

Doug, I think that could work! I have yet another question, though (thanks
for your patience).

How do I get the range in the first place? I'm not too familiar with the
Find object, but here's my code:

________


If s.InUse = True And s.Type = 1 And Not IsInList(s.NameLocal, whitelist) Then
wd.app.options.DefaultHighlightColorIndex = 6 'wdRed
With adoc.range.Find
.ClearFormatting()
.Text = ""
.Style = s.NameLocal
.Replacement.Highlight = True
.Replacement.Text = ""
.Execute(Replace:=2, Format:=True) 'wdReplaceAll
If .Found = True Then
' I need to check the range here, but how?



' add the name to the foundList and log
' if it's not in there already
If Not foundList.Contains(s.NameLocal + ",")
Then
foundList = foundList + s.NameLocal + ","
End If
End If
End With
End If
 
D

Doug Robbins - Word MVP

This highlights text to which the Heading 1 style has been applied, but does
not highlight the end of row marker even if it has that format.

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
Do While .Execute(findText:="", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop) = True
Set myrange = Selection.Range
myrange.HighlightColorIndex = wdRed
Selection.Collapse wdCollapseEnd
Loop
End With


--
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
 
F

fumei via OfficeKB.com

Hi Benjamin05,

"How do I get the range in the first place? I'm not too familiar with the
Find object"

You get the range in the first place by simply using it. The .Find method of
Range resizes the range itself with every find Found.

IF the found is the end-of-row marker, then you can in fact test for that.

The end-of-row marker is an odd duck and is made up of Chr(13) and Chr(7), in
that order. Note this is also the same as the end-of-cell marker.

BTW: I would make a Range object and use the Find on that, rather than adoc.
Range.Find. Let's say r.

Function IsCellMarker(strIn As String) As Boolean
If Len(strIn) = 2 And _
Asc(strIn) = 13 And _
Asc(Right(strIn, 1)) = 7 Then
IsCellMarker = True
End Function


Dim r As Range
Set r = adoc.Range
With r.Find
.....yadda yadda
Do While .Execute (yadda yadda - BTW: using Do is a better way)
' remember r (the Range itself) is actually resized
If IsCellMarker(r.Text) Then
' skip it and continue
 

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