G
Grenier
Automating a word report from access. Having problem with Header and Footer
when looping throught the recordset. I must create 1 page for each record and
each page must have it's owned Head/Foot. Note that the first page is OK but
an error occur on the second page. The sample code use an array instead of a
recordset.
sub test()
MyArray = Split("aaaa bbbb cccc dddd eeee ffff")
Set wrd = CreateObject("Word.Application")
Set doc = wrd.Documents.Add
wrd.Visible = True
For x = 0 To UBound(MyArray)
Set Tbl = wrd.ActiveDocument.Tables.Add(wrd.Selection.Range, 1, 2)
With Tbl
Set Rng = .Cell(1, 1).Range
Rng.Text = MyArray(x)
End With
CurIndex = wrd.Selection.Sections(1).Index
wrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
wrd.ActiveDocument.Sections(CurIndex).Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
Set Rng =
wrd.ActiveDocument.Sections(CurIndex).Headers(wdHeaderFooterFirstPage).Range
Set TblHeader = Rng.Tables.Add(Range:=Rng, numrows:=1, numcolumns:=2)
With TblHeader
Set Rng = .Cell(1, 1).Range
Rng.Text = "Header for " & MyArray(x)
Set Rng = .Cell(1, 2).Range
Rng.Text = "Header"
End With
wrd.ActiveDocument.Sections(CurIndex).Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
Set Rng =
wrd.ActiveDocument.Sections(CurIndex).Footers(wdHeaderFooterFirstPage).Range
Set TblFooter = Rng.Tables.Add(Range:=Rng, numrows:=1, numcolumns:=2)
With TblFooter
Set Rng = .Cell(1, 1).Range
Rng.Text = "Footer for " & MyArray(x)
Set Rng = .Cell(1, 2).Range
Rng.Text = "Footer"
End With
Tbl.Select
With wrd.Selection
.Move wdCharacter, 1 ' get past table marker
.InsertBreak Type:=wdSectionBreakNextPage
.Goto What:=wdGoToPage, Which:=wdGoToNext
End With
Next x
End Sub
Merci !
when looping throught the recordset. I must create 1 page for each record and
each page must have it's owned Head/Foot. Note that the first page is OK but
an error occur on the second page. The sample code use an array instead of a
recordset.
sub test()
MyArray = Split("aaaa bbbb cccc dddd eeee ffff")
Set wrd = CreateObject("Word.Application")
Set doc = wrd.Documents.Add
wrd.Visible = True
For x = 0 To UBound(MyArray)
Set Tbl = wrd.ActiveDocument.Tables.Add(wrd.Selection.Range, 1, 2)
With Tbl
Set Rng = .Cell(1, 1).Range
Rng.Text = MyArray(x)
End With
CurIndex = wrd.Selection.Sections(1).Index
wrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
wrd.ActiveDocument.Sections(CurIndex).Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
Set Rng =
wrd.ActiveDocument.Sections(CurIndex).Headers(wdHeaderFooterFirstPage).Range
Set TblHeader = Rng.Tables.Add(Range:=Rng, numrows:=1, numcolumns:=2)
With TblHeader
Set Rng = .Cell(1, 1).Range
Rng.Text = "Header for " & MyArray(x)
Set Rng = .Cell(1, 2).Range
Rng.Text = "Header"
End With
wrd.ActiveDocument.Sections(CurIndex).Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
Set Rng =
wrd.ActiveDocument.Sections(CurIndex).Footers(wdHeaderFooterFirstPage).Range
Set TblFooter = Rng.Tables.Add(Range:=Rng, numrows:=1, numcolumns:=2)
With TblFooter
Set Rng = .Cell(1, 1).Range
Rng.Text = "Footer for " & MyArray(x)
Set Rng = .Cell(1, 2).Range
Rng.Text = "Footer"
End With
Tbl.Select
With wrd.Selection
.Move wdCharacter, 1 ' get past table marker
.InsertBreak Type:=wdSectionBreakNextPage
.Goto What:=wdGoToPage, Which:=wdGoToNext
End With
Next x
End Sub
Merci !