Detecting style in table cell

S

Steve Wylie

* This post previously appeared in vba.beginners on 6 Jan
but did not attract any replies so I am re-posting here.


The following macro finds Heading 1 styles that occur in
a table. But it doesn't work if an empty cell is in
Heading 1, but contains nothing. How can it be altered
to detect a Heading 1 in a table cell that contains no
text?

Steve Wylie

With Selection.Find
.Format = true
.ClearFormatting
.Style = "Heading 1"
.Text = ""
Do While .Execute
If Selection.Information(wdWithinTable) Then
Exit Do
End If
Loop
End With
 
J

Jonathan West

Hi Steve,

I can reproduce the behaviour. I think you might have to change your
approach on this, and check the style property of the range of each cell in
turn.
 
S

Steve

Hmm. I'm sure it worked before when you answered my
original query.

How do I check the style property of the range of each
cell in turn, then?

Steve
 
J

Jonathan West

Steve said:
Hmm. I'm sure it worked before when you answered my
original query.

How do I check the style property of the range of each
cell in turn, then?

Something like this, designed to catch only those empty cells that have
heading 1 style

Dim oTable as Table
Dim oCell as Cell

For Each oTable in ActiveDocument.Tables
For Each oCell in oTable
If oCell.Range.Characters.Count = 1 Then
If oCell.Range.Style.NameLocal = "Heading 1" Then
oCell.range.Select
Goto FoundOne
End If
End If
Next oCell
Next oTable

MsgBox "Not found"
Exit Sub

FoundOne:
MsgBox "Found one"
 
S

Steve Wylie

I'm getting an error "does not support this property or
method" when I run the macro, and the line that seems to
be the problem is:

For Each oCell In oTable

I cannot see anything wrong with this line... why does it
do this?

Steve
 
J

Jonathan West

Steve Wylie said:
I'm getting an error "does not support this property or
method" when I run the macro, and the line that seems to
be the problem is:

For Each oCell In oTable

I cannot see anything wrong with this line... why does it
do this?

Sorry, my mistake. Use this line instead

For Each oCell in oTable.Range.Cells
 
J

Jonathan West

Steve Wylie said:
Thanks for your help - the macro works now.

However I have encountered a new problem. I have
assigned this macro and four other related macros to a
toolbar stored in a template, which I add-in through
Tools/Templates & Add-Ins. However, since designing a
button for this macro I cannot add the template in as it
comes up with "The document template is not valid". The
macro text is OK, because I have exported this into a new
template and by themselves, the macros Add-In OK. But
once I copy across the toolbar (using the Organiser in
the Templates & Add-Ins dialog), I get that message. How
can I "fix" corrupt toolbar buttons? I'd hate to have to
design them again from scratch.

(If I should post this as a new query in another NG,
please advise).

I'm not quite sure what you mean. Could you describe the point at which the
error appears? Is is then you attempt to copy the toolbar, when you attempt
to load the add-in or when you attempt to click a button to run the macro?
 
S

Steve Wylie

I get the error when I attempt to load the add-in. I
find the template, click on it and instead the template
being added to the list, I get the error message.

Steve
 
J

Jonathan West

Steve Wylie said:
I get the error when I attempt to load the add-in. I
find the template, click on it and instead the template
being added to the list, I get the error message.

Are you saying that when you have the template with the same macros but
without the toolbar, it loads correctly?
 

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