Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1744
  • Last Modified:

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!
0
Steven Reid
Asked:
Steven Reid
1 Solution
 
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 ReidAuthor Commented:
Sometimes the simple solution is staring you in the face and you just cant see it!

Thanks so much!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now