mdnuts
Registered User.
- Local time
- Today, 10:49
- Joined
- May 28, 2014
- Messages
- 131
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.
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: