A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

suddenly experiencing error messages with table macro



 
 
Thread Tools Display Modes
  #1  
Old January 22nd, 2007, 06:40 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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

  #2  
Old January 22nd, 2007, 07:51 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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

  #3  
Old January 22nd, 2007, 10:39 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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?

  #4  
Old January 23rd, 2007, 02:10 AM posted to microsoft.public.word.tables
macropod
external usenet poster
 
Posts: 1,231
Default suddenly experiencing error messages with table macro

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]


"Tom" wrote in message
ups.com...
| 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
|


  #5  
Old January 23rd, 2007, 07:42 AM posted to microsoft.public.word.tables
macropod
external usenet poster
 
Posts: 1,231
Default suddenly experiencing error messages with table macro

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]


"Tom" wrote in message
ups.com...
| 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
|


  #6  
Old January 23rd, 2007, 02:50 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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.

  #7  
Old January 23rd, 2007, 03:28 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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

  #8  
Old January 23rd, 2007, 08:38 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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

  #9  
Old January 24th, 2007, 01:49 AM posted to microsoft.public.word.tables
macropod
external usenet poster
 
Posts: 1,231
Default suddenly experiencing error messages with table macro

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]


"Tom" wrote in message
oups.com...
| 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
|


  #10  
Old January 24th, 2007, 06:05 PM posted to microsoft.public.word.tables
Tom
external usenet poster
 
Posts: 61
Default suddenly experiencing error messages with table macro

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.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:44 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.