View Single Post
 
Old 05-02-2017, 05:15 AM
babysatch babysatch is offline Windows 10 Office 2016
Novice
 
Join Date: Apr 2017
Posts: 6
babysatch is on a distinguished road
Default Adding Headers and Footers to a Document

Hi,

does an body have any idea what I'm doing wrong. I would like too add a header and a footer with the coresponding textfields.

I get always the runtime error 5941.

Here is the code:

Private Sub CommandButton1_Click()
Dim MSWord As Object
Dim Datei As String
Dim pdfName As String
Set MSWord = CreateObject("Word.Application")
MSWord.Visible = True

Datei = "D:\WordTest.doc"
pdfName = "D:\WordTest.pdf"

MSWord.Documents.Add
With MSWord.Selection
.Font.Name = "Helvetica"
.Font.Size = 20
.Font.Color = 255
.Font.Bold = True
.Font.Italic = True
.TypeText Text:="Text"
.TypeParagraph
End With

MSWord.Selection.insertbreak 2



'Here is the problematic part:

With MSWord.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = "Header text"
.Footers(wdHeaderFooterPrimary).Range.Text = "Footer text"
End With







MSWord.ActiveDocument.SaveAs Datei

MSWord.ActiveDocument.ExportAsFixedFormat _
OutputFileName:=pdfName, _
ExportFormat:=17, _
OpenAfterExport:=False, _
OptimizeFor:=0, _
Range:=0, _
Item:=0, _
IncludeDocProps:=False, _
KeepIRM:=True, _
CreateBookmarks:=0, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False

MSWord.ActiveDocument.Close
MSWord.Quit
Set MSWord = Nothing
End Sub


I would be grateful of any help.
Reply With Quote