Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-02-2017, 05:15 AM
babysatch babysatch is offline Adding Headers and Footers to a Document Windows 10 Adding Headers and Footers to a Document Office 2016
Novice
Adding Headers and Footers to a Document
 
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
  #2  
Old 05-02-2017, 06:43 AM
gmayor's Avatar
gmayor gmayor is offline Adding Headers and Footers to a Document Windows 10 Adding Headers and Footers to a Document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I believe you have missed the significance about what I told you yesterday about using the numeric equivalents of Word commands when using late binding to Word i.e. those beginning 'wd' and in this instance wdHeaderFooterPrimary.

However I would have written it as follows, which does not produce an error.

Code:
Private Sub CommandButton1_Click()
Dim MSWord As Object
Dim oDoc As Object
Dim oRng 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"
    Set oDoc = MSWord.Documents.Add
    Set oRng = oDoc.Range
    With oRng
        .Collapse 1
        .Text = "Text" & vbCr
        .Font.Name = "Helvetica"
        .Font.Size = 20
        .Font.Color = 255
        .Font.Bold = True
        .Font.Italic = True
        .Collapse 0
        .InsertBreak 2
    End With
    With oDoc.Sections(1)
        .Headers(1).Range.Text = "Header text"
        .Footers(1).Range.Text = "Footer text"
    End With

    oDoc.SaveAs Datei
    oDoc.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

    oDoc.Close
    MSWord.Quit
    Set MSWord = Nothing
    Set oDoc = Nothing
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 05-02-2017, 07:05 AM
babysatch babysatch is offline Adding Headers and Footers to a Document Windows 10 Adding Headers and Footers to a Document Office 2016
Novice
Adding Headers and Footers to a Document
 
Join Date: Apr 2017
Posts: 6
babysatch is on a distinguished road
Default

Thank you. You're absolutely right.

But I couldn't find the numeric equivalents of Word commands in the object library reference here (see the link below). I couldn't even find the english version. This is the german version:

https://msdn.microsoft.com/de-de/library/ff837519.aspx

Would you be so kind and give me a hint?

Thank you.

Greetings from Munich.
Reply With Quote
  #4  
Old 05-02-2017, 10:08 AM
gmayor's Avatar
gmayor gmayor is offline Adding Headers and Footers to a Document Windows 10 Adding Headers and Footers to a Document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Sadly Microsoft removed the VBA Help after Word 2010, but had you typed wdHeaderFooterPrimary enumeration into Google you would have found the value 1.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Headers & Footers - Show Document Text Aprilann Word 6 04-13-2017 09:34 AM
Adding Headers and Footers to a Document Headers/Footers not in new document Jim-ski Word 7 12-07-2015 09:30 PM
Adding Headers and Footers to a Document Adding different headers and footers in Microsoft word camzie Word 4 08-22-2013 06:39 PM
Headers and Footers OverAchiever13 Word 1 05-27-2010 01:30 PM
How do I have headers and footers appear as regular text for the whole document? user908045 Word 0 03-13-2010 12:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft