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
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