Creating Header / Footer pages in a loop ! - Grenie

26-Feb-08 03:11:02
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 !
reply
 
 

Creating Header / Footer pages in a loop ! - Doug Robbins - Word MVP

27-Feb-08 05:10:37
What do you mean by "an error occur on the second page".  What does or does
not happen?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
reply
 

Creating Header / Footer pages in a loop ! - Grenie

27-Feb-08 08:25:01
The error message is (french: # 6028 Impossible de supprimer la plage):  " #
6028 impossible to delete range" .  This message is displayed when the macro
try to set the header for the second page.   There is no problem on the first
pass of the for/next, so header and footer on first page 'aaaa' are OK but
the macro is stopped with this error when it try to set Header on page 'bbbb'.

I'm using this sub on an Access 2002 module with of course reference to word
10.0 object library.
reply
 

Creating Header / Footer pages in a loop ! - fumei via OfficeKB.com

27-Feb-08 03:28:54
For one thing, CurIndex is not incremented.  You may want to try using a
HeaderFooter object to do your head and footer work.  try this.  I moved the
CurIndex initializing out of the loop, as well as the DifferentFirstPage.  If
DifferentFirstPage is to apply for all Sections, you may as well do once, at
the beginning.  It should apply for the entire document.

There is no need to set a range object for the table in the document.  Just
put the text into the cells.


Sub test()
Dim MyArray()
Dim Tbl As Word.Table
Dim oHF As Word.HeaderFooter
Dim CurIndex As Long
MyArray = Split("aaaa bbbb cccc dddd eeee ffff")

Set wrd = CreateObject("Word.Application")
Set doc = wrd.Documents.Add
wrd.Visible = True

CurIndex = 1
wrd.ActiveDocument.PageSetup _
.DifferentFirstPageHeaderFooter = True

For x = 0 To UBound(MyArray)
Set Tbl = wrd.ActiveDocument.Tables _
.Add(wrd.Selection.Range, 1, 2)
Tbl.Cell(1, 1).Range.Text = MyArray(x)

' action Header as object
Set oHF = wrd.ActiveDocument.Sections(CurIndex) _
.Headers(wdHeaderFooterFirstPage)
With oHF
.LinkToPrevious = False
.Range.Tables _
.Add Range:=oHF.Range, numrows:=1, numcolumns:=2
.Range.Tables(1).Cell(1, 1).Range _
.Text = "Header for " & MyArray(x)
.Range.Tables(1).Cell(1, 2).Range _
.Text = "Header"
End With

' action Footer as object
Set oHF = wrd.ActiveDocument.Sections(CurIndex) _
.Footers(wdHeaderFooterFirstPage)
With oHF
.LinkToPrevious = False
.Range.Tables _
.Add Range:=oHF.Range, numrows:=1, numcolumns:=2
.Range.Tables(1).Cell(1, 1).Range _
.Text = "Footer for " & MyArray(x)
.Range.Tables(1).Cell(1, 2).Range _
.Text = "Footer"
End With

' go to end of document
With wrd.Selection
.EndKey Unit:=6 ' this is wdStory
.InsertBreak Type:=wdSectionBreakNextPage
' Selection will move into that Section
' so Set Tbl using wrd.Selection should work
End With
' increment CurIndex
CurIndex = CurIndex + 1
Next x

End Sub




--
Message posted via http://www.officekb.com
reply
 

Creating Header / Footer pages in a loop ! - Grenie

27-Feb-08 09:08:00
I agree with your comment Fumei
Thought that CurIndex would increment on each SectionBreak.

Tried your code but sadly still having the same error. I've cut and paste
the sub to an Access module with option explicit, add a few object but cannot
figure out why ?

Sub test()
Dim wrd As Word.Application
Dim doc As Word.Document
Dim MyArray As Variant
Dim Tbl As Word.Table
Dim oHF As Word.HeaderFooter
Dim CurIndex As Long
Dim x As Integer

MyArray = Split("aaaa bbbb cccc dddd eeee ffff")

Set wrd = CreateObject("Word.Application")
Set doc = wrd.Documents.Add
wrd.Visible = True

CurIndex = 1
wrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True

For x = 0 To UBound(MyArray)
Set Tbl = wrd.ActiveDocument.Tables.Add(wrd.Selection.Range, 1, 2)
Tbl.Cell(1, 1).Range.Text = MyArray(x)

' action Header as object
Set oHF =
wrd.ActiveDocument.Sections(CurIndex).Headers(wdHeaderFooterFirstPage)
With oHF
.LinkToPrevious = False
.Range.Tables.Add Range:=oHF.Range, numrows:=1, numcolumns:=2
.Range.Tables(1).Cell(1, 1).Range.Text = "Header for " & MyArray(x)
.Range.Tables(1).Cell(1, 2).Range.Text = "Header"
End With

' action Footer as object
Set oHF =
wrd.ActiveDocument.Sections(CurIndex).Footers(wdHeaderFooterFirstPage)
With oHF
.LinkToPrevious = False
.Range.Tables.Add Range:=oHF.Range, numrows:=1, numcolumns:=2
.Range.Tables(1).Cell(1, 1).Range.Text = "Footer for " & MyArray(x)
.Range.Tables(1).Cell(1, 2).Range.Text = "Footer"
End With

' go to end of document
With wrd.Selection
.EndKey Unit:=6 ' this is wdStory
.InsertBreak Type:=wdSectionBreakNextPage
' Selection will move into that Section
' so Set Tbl using wrd.Selection should work
End With
' increment CurIndex
CurIndex = CurIndex + 1
Next x

End Sub

------------------------------------
reply
 
Incredibly frustrated with Macro
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class