At my wit's end with macro

K

Kimm

Hello,

Hopefully someone can rescue me before I jump off the bridge. I am
attempting to create a macro that finds all occurences of a specific style
("Business Rule") in a table and highlights them. Once they are
highlighted,
I would like the macro to then shade the cells. Here is what I have but
when
I run it, absolutely nothing is changed so I don't even know if the macro
runs.

Is there anyone who can save me before it's too late?

Dim oCell As Cell
Dim sTextToFind As String

If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not currently in a table"
Else
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Business Rule")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
Do While Selection.Find.Execute = True
Selection.EndKey
With Selection.Cells.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray25
End With
Loop
End With
End If
End Sub
 
K

Karl E. Peterson

Kimm said:
Hopefully someone can rescue me before I jump off the bridge.

By telling you you'll only cripple yourself, because the bridge isn't *that*
high? said:
I run it, absolutely nothing is changed so I don't even know if the
macro runs.

Is there anyone who can save me before it's too late?

What you really need are a few basic troubleshooting skills. First off,
stick a breakpoint near the top of that routine. (Put the cursor on an
executable line, and press F9.) That'll tell ya if it's running or not.
Then, step through it one line at a time, using F8, to determine what it's
(not) doing. Open the Locals window to track variable values as you go, or
hover the cursor over individual variables to see if they contain what you
expect.

You may have more questions, after doing that, but they'll at least be a bit
more informed. On the other hand, that could be all it takes for _you_ to
see where it's falling down!
 
J

Jean-Guy Marcil

Kimm was telling us:
Kimm nous racontait que :
Hello,

Hopefully someone can rescue me before I jump off the bridge. I am
attempting to create a macro that finds all occurences of a specific
style ("Business Rule") in a table and highlights them. Once they are
highlighted,
I would like the macro to then shade the cells. Here is what I have
but when
I run it, absolutely nothing is changed so I don't even know if the
macro runs.

Is there anyone who can save me before it's too late?

Dim oCell As Cell
Dim sTextToFind As String

These two variables are declared, but not used at all...
If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not currently in a table"
Else

The Selection object will not work if the table is not selected as a whole.
In my suggestion, you only have to place the cursor anywhere in the table.
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Business Rule")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
Do While Selection.Find.Execute = True
Selection.EndKey
With Selection.Cells.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray25
End With
Loop
End With
End If
End Sub

Try this instead:

Sub ShadeCell()

Dim StartRange As Range

If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not currently in a table"
Else
Set StartRange = Selection.Tables(1).Range
With StartRange
With .Find
.ClearFormatting
.Style = ActiveDocument.Styles("Business Rule")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Do While .Find.Execute = True
With .Cells(1).Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray25
End With
.Collapse wdCollapseEnd
Loop
End With
End If

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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