Link to home
Start Free TrialLog in
Avatar of Steven Reid
Steven ReidFlag for Australia

asked on

Object variable or With block variable not set error when trying to use Selection in Access 2010 vba

Hi All,

I am updating a word document from Access 2010 and am coming up against a "Object variable or With block variable not set" error when I try to format a column in a table.

I am using the following code:

Sub FormatTable(tbl As Word.Table)
'Variable declaration 
Dim cel As Word.Cell
Dim s As String
'Bold the header row
With tbl.Rows(1).Range.Font
.Bold = True
.Underline = wdUnderlineSingle
End With
'Center the last column
tbl.Columns(tbl.Columns.Count).Select
For Each cel In Selection.Cells
cel.Range.Paragraphs.Alignment = wdAlignParagraphCenter
Next cel
tbl.Columns.AutoFit
tbl.Borders.Enable = False
End Sub

Open in new window


and I receive the "Object variable or With block variable not set" when I get to the
For Each cel In Selection.Cells

Open in new window

  line.

Any help greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steven Reid

ASKER

Sometimes the simple solution is staring you in the face and you just cant see it!

Thanks so much!