Access VBA - insert table into word header (1 Viewer)

Status
Not open for further replies.

mdnuts

Registered User.
Local time
Today, 14:45
Joined
May 28, 2014
Messages
128
for probably no reason at all I banged my head on this for about a week looking at it here and there.

Inserting a table of contents was straightforward, as was setting the margins. the table in the header though.... oye.

Code:
   Dim oDoc As Word.Document
  Dim oWord As Word.Application
 
  Set oWord = CreateObject("Word.Application")
  oWord.Visible = True
  Set oDoc = oWord.Documents.Add
  With oWord
     .Activate
  
 '.....whatever radically cool vba you have here

 'add the table of contents to first page in document set
  Dim oWordRange As Word.Range
  Set oWordRange = oWord.ActiveDocument.Range(0, 0)
  oWord.ActiveDocument.TablesOfContents.Add _
    Range:=oWordRange, _
    UseFields:=False, _
    UseHeadingStyles:=True, _
    LowerHeadingLevel:=3, _
    UpperHeadingLevel:=1
  End With  'end with oWord

 'put a title to the toc 
  oWord.Selection.HomeKey Unit:=wdStory
  oWord.Selection.TypeParagraph
  oWord.Selection.MoveUp Unit:=wdLine, Count:=1
  oWord.Selection.TypeText Text:="TABLE OF CONTENTS"
  oWord.Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
  oWord.Selection.Font.Bold = False
  oWord.Selection.Font.Size = 16
  oWord.Selection.Font.Color = 13998939

 'add a table in all the headers and all the footers in all the land   
  oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables.Add _
    Range:=oDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range, _
    NumRows:=1, _
    NumColumns:=3
    
  Dim oTbl
  Set oTbl = oDoc.Sections(1).Headers(1).Range.Tables(1)
    oTbl.Cell(1, 1).Range.Text = "String1"
    oTbl.Cell(1, 2).Range.Text = "String2"
    oTbl.Cell(1, 3).Range.Text = "String3"
    oTbl.Cell(1, 3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
  Set oTbl = Nothing
    
  oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Tables.Add _
    Range:=oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range, _
    NumRows:=1, _
    NumColumns:=3
 
  Set oTbl = oDoc.Sections(1).Footers(1).Range.Tables(1)
    oTbl.Cell(1, 1).Range.Text = "String4"
    oTbl.Cell(1, 2).Range.Text = "String5"
    oTbl.Cell(1, 3).Range.Text = "String6"
    oTbl.Cell(1, 3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
  Set oTbl = Nothing

    
'set the margins in tip top shape 
  With oWord.ActiveDocument.PageSetup
    .TopMargin = oWord.InchesToPoints(0.88)
    .LeftMargin = oWord.InchesToPoints(1)
    .RightMargin = oWord.InchesToPoints(1)
    .BottomMargin = oWord.InchesToPoints(1)
  End With

'dont you hate it when you forget to save     
 oDoc.SaveAs2 "temp.doc"

 'clean up, clean up, everybody everywhere, clean up. 
  Set oDoc = Nothing
  Set oWord = Nothing
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:45
Joined
Jul 9, 2003
Messages
16,244
Very nice thank you mdnuts. I would add that you will need to set some variables somewhere in the code. You also need to set a reference to Microsoft Word...

I'd suggest possibly in the Declarations Section:-
Dim oWord As Object '(There might be a better object choice available)
Dim oDoc As Object '(There might be a better object choice available)

and in the Code:-
Private Sub Command0_Click()
'Requires a reference to:-
'Microsoft Word 15.0 Object Library - Or the version compatible with your MS Access Version
 

mdnuts

Registered User.
Local time
Today, 14:45
Joined
May 28, 2014
Messages
128
you're right, I meant to add the dim's but alas, I forgot.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom