davidalexsmith
Wed May 11 03:26:04 CDT 2005
Merci Jean-Guy,
Thanks for the tips on good practice. I still get the same error though.
- "Object variable or With block variable not set".
Dave.
"Jean-Guy Marcil" wrote:
> david_alex_smith was telling us:
> david_alex_smith nous racontait que :
>
> > Hello.
> >
> > I'm trying to align all table cell contents to the bottom of the cell
> > if they are "Table Heading Style".
> >
> > I've written the following macro to do this.
> >
> > Sub AlignTables()
> > 'Aligns table headings
> > Dim Table, aRow, aCell, myRange
> > For Each Table In ActiveDocument.Tables
> > For Each aRow In Table.Rows
> > For Each aCell In aRow.Cells
> > myRange = aCell
> > If myRange.Style = "Table Heading" Then
> > aCell.VerticalAlignment = wdAlignVerticalBottom
> > End If
> > Next aCell
> > Next aRow
> > Next Table
> > End Sub
> >
> >
> > This doesn't work though. I get an error - "Object variable or With
> > block variable not set".
> >
> > Any ideas how I can get this working. Thanks in advance.
> >
>
> Five comments:
>
> 1) It is not a good idea to use a variable name that happens to be a
> constant, an object name, a method name or a property name in the Object
> model. Table is an object name.
>
> 2) It is much better practice to explicitly cast the variable to a type.
> When you do that, you and the compiler both know what you are dealing with
> anywhere in the code. This way you don't need to let the compiler decide for
> you and then produce funny results...as you have experienced. As a big plus
> to this practice, Intellisense will work when you write your code, so if you
> try to use a method/property with an object that does not support it, you
> will know right away.
>
> 3) It is also much better not to use default assignment ("myRange = aCell"
> You want myRange to be a range object, but have not said so anywhere in your
> code, and you want aCell to be the range in the cell represented by aCell
> which the compiler had to infer...) Also, we do not know if in future
> versions of the Word object model if the default will remain the same, or
> even be allowed to be used.
>
> 4) It is recommended not to use the object in the Next statement.
> For Each myThingy In ActiveDocument.Thingies
> Test myThingy
> Next
> If you want, you can comment it so that you know what each next is referring
> to:
> For Each myThingy In ActiveDocument.Thingies
> Test myThingy
> Next 'myThingy
>
> 5) Finally, and this is where you had the problem generating the error
> message, you need a "Set" statement to assign a document range to a range
> object in the code.
>
> Here is a revised version of your code.
>
> '_______________________________________
> Sub AlignTables()
> 'Aligns table headings
> Dim aTable As Table
> Dim aRow As Row
> Dim aCell As Cell
> Dim myRange As Range
>
> For Each aTable In ActiveDocument.Tables
> For Each aRow In aTable.Rows
> For Each aCell In aRow.Cells
> Set myRange = aCell.Range
> If myRange.Style = "Table Heading" Then
> aCell.VerticalAlignment = wdAlignVerticalBottom
> End If
> Next
> Next
> Next
>
> End Sub
> '_______________________________________
>
> --
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site:
http://www.word.mvps.org
>
>
>
>