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!
Steven ReidHead of Process ImprovementAsked:
Who is Participating?
 
irudykCommented:
Try eliminating Line 11 and change Line 12 to:
For Each cel In tbl.Columns(tbl.Columns.Count).Cells
So your code would be like:
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
For Each cel In tbl.Columns(tbl.Columns.Count).Cells
cel.Range.Paragraphs.Alignment = wdAlignParagraphCenter
Next cel
tbl.Columns.AutoFit
tbl.Borders.Enable = False
End Sub

Open in new window

0
 
Steven ReidHead of Process ImprovementAuthor Commented:
Sometimes the simple solution is staring you in the face and you just cant see it!

Thanks so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.