PDA

View Full Version : Solved: Refering to table in header



rsimm
11-19-2008, 11:21 AM
Hi
Would be grateful for help, completely new to Word VBA but have been using Access VBA for a couple of years.

I want to use Access to write some data out into a Word document. The code below puts a table into the header, but I cannot get the code below it to format it correctly

The function stops on the line
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
with error:
'Run-time error 91: Object variable or With block variable not set'


Public Function CreateWordReport_Test()
Set WordApp = New Word.Application
WordApp.Documents.Add
Set doc = WordApp.ActiveDocument
Set sel = WordApp.Selection
WordApp.Visible = True
With doc

'Insert table into header
sel.Range.Tables.Add Range:= _
doc.Sections(1).Headers(wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow



With sel.Tables(1)
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With

End With
End Function


If I change the code to put the table into the main document instead of the header, it works fine

ie. change


sel.Range.Tables.Add Range:= _
doc.Sections(1).Headers(wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow


to


sel.Range.Tables.Add Range:= _
doc.Sections(1).Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow


So I'm guessing the problem is that a table in the header needs to be referred to differently, as it clearly doesn't like sel.Tables(1). Any clues anyone?
many thanks

lucas
11-19-2008, 11:36 AM
Hi and welcome to the board.

Have you considered using a word template instead of creating a new document.
The table can already exist in the doc created from the template and be formatted the way you want it.
You can bookmark the table in the header to refer specifically to that header instead of using an index number.

fumei
11-19-2008, 11:45 AM
It does not like sel.Tables(1) because the Selection is NOT in the header. There are a couple of different ways to go about this.

1. you could put the selection in the header, and go on from there.

2. do not use the Selection at all. Use objects. The following makes a headerfooter object of the header you want to action. It makes a table object of THAT table. It actions the table object.
Dim aTable As Table
Dim oHF As HeaderFooter
Set oHF = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)

Set aTable = oHF.Range.Tables.Add(Range:= _
oHF.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)

With aTable
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With

Note the required use of brackets in the Set aTable instruction. Parameters passed as values to the application itself:

e.g.


sel.Range.Tables.Add Range:= _
doc.Sections(1).Range, NumRows:=1,

do NOT use brackets, but parameters passed to the Set of an object DO use brackets.
Set aTable = oHF.Range.Tables.Add(Range:= _
oHF.Range, NumRows:=1.......)


Also, of course you will replace my "ActiveDocument", with your "doc" - as that is the object you are using.

rsimm
11-21-2008, 03:17 AM
many thanks for the replies- that works fine. I'm also going to look at using bookmarks.