suddenly experiencing error messages with table macro

T

Tom

My document was running the following macro without any problems:

Sub ConvertRefTables()

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl

.Style = ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75

End With
End If
Next oTbl
End Sub

However, when I run it now, I receive an error message that says
"Object variable or with block variable not set." And it highlights
this part of the macro code:

If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Table Header") Then

My document does have a table with the Table Header style is cell
(1,1), so I'm not sure why I'm getting the error.

I am running the macro with other macros in the document -- maybe
that's the problem? I copied the macro into a new document and it
worked fine. It's just when I include it with the other macros that it
starts to give me with error message. Can anyone help me?

Thanks,

Tom
 
T

Tom

I discovered what was wrong. Some of my tables have two different kinds
of styles in cell(1,1). That's why the macro was working before, but
then suddenly threw error messages at me -- because previously, all my
tables had only one style.

How do I modify the following macro so that I can allow more than one
style in Cell(1,1)? In other words, how can I write an Or statement in
there -- like If (1,1) has Table Header style or Image style or Figure
Caption style, then ....

Sub ConvertRefTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl
.Style = ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oTbl
End Sub
 
T

Tom

I found a site that provides some instruction on including Or
statements, but modification of the following code doesn't seem to
work:


Sub ConvertRefTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = ActiveDocument.Styles("Table
Header") _
Or oTbl.Cell(1, 1).Range.Style =
ActiveDocument.Styles("Caption-figure") _
Then
With oTbl
.Style = ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oTbl
End Sub

Can someone point out what I'm doing wrong?
 
M

macropod

hi Tom,

Try:

Sub ConvertRefTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Paragraphs(1).Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl
.Range.Paragraphs(1).Style = ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oTbl
End Sub

Cheers

--
macropod
[MVP - Microsoft Word]


| My document was running the following macro without any problems:
|
| Sub ConvertRefTables()
|
| Dim oTbl As Table
| For Each oTbl In ActiveDocument.Tables
| If oTbl.Cell(1, 1).Range.Style = _
| ActiveDocument.Styles("Table Header") Then
| With oTbl
|
| .Style = ActiveDocument.Styles("Reftable")
| .PreferredWidthType = wdPreferredWidthPercent
| .PreferredWidth = 75
|
| End With
| End If
| Next oTbl
| End Sub
|
| However, when I run it now, I receive an error message that says
| "Object variable or with block variable not set." And it highlights
| this part of the macro code:
|
| If oTbl.Cell(1, 1).Range.Style = _
| ActiveDocument.Styles("Table Header") Then
|
| My document does have a table with the Table Header style is cell
| (1,1), so I'm not sure why I'm getting the error.
|
| I am running the macro with other macros in the document -- maybe
| that's the problem? I copied the macro into a new document and it
| worked fine. It's just when I include it with the other macros that it
| starts to give me with error message. Can anyone help me?
|
| Thanks,
|
| Tom
|
 
M

macropod

Hi Tom,

If your cell might have more than one paragraph formatted in the 'Table
Header' style, or that paragraph isn't necessarily the first, try:

Sub ConvertRefTables()
Dim oTbl As Table
Dim oPara As Paragraph
For Each oTbl In ActiveDocument.Tables
For Each oPara In oTbl.Cell(1, 1).Range.Paragraphs
If oPara.Style = ActiveDocument.Styles("Table Header") Then
oPara.Style = ActiveDocument.Styles("Reftable")
With oTbl
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oPara
Next oTbl
End Sub

Cheers

--
macropod
[MVP - Microsoft Word]


| My document was running the following macro without any problems:
|
| Sub ConvertRefTables()
|
| Dim oTbl As Table
| For Each oTbl In ActiveDocument.Tables
| If oTbl.Cell(1, 1).Range.Style = _
| ActiveDocument.Styles("Table Header") Then
| With oTbl
|
| .Style = ActiveDocument.Styles("Reftable")
| .PreferredWidthType = wdPreferredWidthPercent
| .PreferredWidth = 75
|
| End With
| End If
| Next oTbl
| End Sub
|
| However, when I run it now, I receive an error message that says
| "Object variable or with block variable not set." And it highlights
| this part of the macro code:
|
| If oTbl.Cell(1, 1).Range.Style = _
| ActiveDocument.Styles("Table Header") Then
|
| My document does have a table with the Table Header style is cell
| (1,1), so I'm not sure why I'm getting the error.
|
| I am running the macro with other macros in the document -- maybe
| that's the problem? I copied the macro into a new document and it
| worked fine. It's just when I include it with the other macros that it
| starts to give me with error message. Can anyone help me?
|
| Thanks,
|
| Tom
|
 
T

Tom

Thanks for your help with this. I tried that code and it seems to work,
but then my code breaks elsewhere (not sure why). I'll have to
investigate a little more. Thanks again.
 
T

Tom

I can't quite get it to work. I have two table macros working, and one
of them always gets stuck on the following line:

..Range.Paragraphs(1).Style = ActiveDocument.Styles("Reftable Flush")

I've triple checked to make sure what I have a Reftable Flush style. It
is a table style, but I didn't think that mattered. Is there anything
else I can try?

Here are the two macros:

Sub ConvertRefTables2()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Paragraphs(1).Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl
.Range.Paragraphs(1).Style =
ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oTbl
End Sub

Sub ConvertRefTablesFlush2()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Paragraphs(1).Style = _
ActiveDocument.Styles("Table Header Flush") Then
With oTbl
.Range.Paragraphs(1).Style =
ActiveDocument.Styles("Reftable Flush")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 83
End With
End If
Next oTbl
End Sub

I don't know why, but every time I
 
T

Tom

Macropod,

You were kind enough to help me with the code, but it just didn't work
for me. I think if I switch what I'm trying to do a little, I can get
around the problem.

What if I could change my IF statement so that instead of saying, "If
in cell(1,1) it has the Table Header style, THEN do this...

To the following:

IF in cell(1,1) the first word is "Note," THEN do this....

How would I apply that new IF statement to the original macro?

Sub ConvertRefTables()

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl

.Style = ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75

End With
End If
Next oTbl
End Sub

Thanks in advance for your help.

Tom
 
M

macropod

Hi Tom,

There's no logical reason why the macro would work with one set of styles and
not another - unless the errant one refers to a non-existent style. Are you
sure it's "Reftable Flush" and not "ReftableFlush", for example?

As an aside, you could combine the two macros into one, with an 'ElseIf for
the second style.

Cheers

--
macropod
[MVP - Microsoft Word]


| I can't quite get it to work. I have two table macros working, and one
| of them always gets stuck on the following line:
|
| .Range.Paragraphs(1).Style = ActiveDocument.Styles("Reftable Flush")
|
| I've triple checked to make sure what I have a Reftable Flush style. It
| is a table style, but I didn't think that mattered. Is there anything
| else I can try?
|
| Here are the two macros:
|
| Sub ConvertRefTables2()
| Dim oTbl As Table
| For Each oTbl In ActiveDocument.Tables
| If oTbl.Cell(1, 1).Range.Paragraphs(1).Style = _
| ActiveDocument.Styles("Table Header") Then
| With oTbl
| .Range.Paragraphs(1).Style =
| ActiveDocument.Styles("Reftable")
| .PreferredWidthType = wdPreferredWidthPercent
| .PreferredWidth = 75
| End With
| End If
| Next oTbl
| End Sub
|
| Sub ConvertRefTablesFlush2()
| Dim oTbl As Table
| For Each oTbl In ActiveDocument.Tables
| If oTbl.Cell(1, 1).Range.Paragraphs(1).Style = _
| ActiveDocument.Styles("Table Header Flush") Then
| With oTbl
| .Range.Paragraphs(1).Style =
| ActiveDocument.Styles("Reftable Flush")
| .PreferredWidthType = wdPreferredWidthPercent
| .PreferredWidth = 83
| End With
| End If
| Next oTbl
| End Sub
|
| I don't know why, but every time I
|
 
T

Tom

I tweaked your code a little and it fixed it. I changed Paragraphs(1)
to Characters(1) in this macro and my other table macros and it no
longer gives me an error msg when I run them. Here's the slightly
tweaked code:

Sub ConvertRefTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Characters(1).Style = _
ActiveDocument.Styles("Table Header") Then
With oTbl
.Range.Characters(1).Style =
ActiveDocument.Styles("Reftable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75
End With
End If
Next oTbl
End Sub

The first characters in my table always have the correct style. If the
table ever has any other style applied to it, that style comes in
later. Thanks again for your help Macropod.
 

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