Results 1 to 6 of 6

Thread: [RESOLVED] Putting text in a footer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    [RESOLVED] Putting text in a footer

    Hi Guys,

    How can I used VBA to put text into the header and footer on every page but the first in Microsoft Word? I am using bookmarks and Selection.TypeText for other places in the document but this wont work in the headers/footers.

    I'm not very good at this VBA stuff so I need a simple explanantion if possible.

    Shane
    Last edited by cassius; Dec 13th, 2005 at 10:22 PM. Reason: problem was resolved

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Putting text in a footer

    Try recording a macro, and then looking at the code generated. It should be no problem to do on your own.

  3. #3
    Junior Member
    Join Date
    Nov 2005
    Posts
    29

    Re: Putting text in a footer

    Hi
    See this Code and u may be knowing how to add Text to header and footer

    Sub SetHeadersFooters()

    Dim oApp As Word.Application
    Dim oSec As Word.Section
    Dim oDoc As Word.Document

    'Create a new document in Word
    Set oApp = New Word.Application
    Set oDoc = oApp.Documents.Add

    With oDoc

    '=== SECTION 1 ==================================================

    'Add two pages to the first section where the first page in the
    'section has different headers and footers than the second page
    Set oSec = .Sections(1)
    oSec.PageSetup.DifferentFirstPageHeaderFooter = True
    oSec.Range.InsertAfter "Text on Page 1 (Section 1)"
    .Range(oSec.Range.End - 1).InsertBreak wdPageBreak
    oSec.Range.InsertAfter "Text on Page 2 (Section 1)"
    Debug.Print oSec.Range.ComputeStatistics(wdStatisticCharacters)
    MsgBox oSec.Range.ComputeStatistics(wdStatisticCharacters)
    'Add the headers/footers for the first section (that contains two
    'pages)
    oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _
    "Page1 -- Section 1 First Page Header"
    oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
    "Page2 -- Section 1 Primary Header"
    oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _
    "Page1 -- Section 1 First Page Footer"
    oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
    "Page2 -- Section 1 Primary Footer"

    '=== SECTION 2 ==================================================

    'Add a new section containing three pages where the first page in
    'the section has different headers and footers than the other two
    'pages
    .Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage
    Set oSec = .Sections(2)
    oSec.PageSetup.DifferentFirstPageHeaderFooter = True
    oSec.Range.InsertAfter "Text on Page 3 (Section 2)"
    .Range(oSec.Range.End - 1).InsertBreak wdPageBreak
    oSec.Range.InsertAfter "Text on Page 4 (Section 2)"
    .Range(oSec.Range.End - 1).InsertBreak wdPageBreak
    oSec.Range.InsertAfter "Text on Page 5 (Section 2)"

    'Add the headers/footers for the second section (that contains
    'three pages) -- notice that the second and third pages in this
    'section will contain the primary header/footer
    oSec.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
    oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _
    "Page3 -- Section 2 First Page Header"
    oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
    "Page4and5 -- Section 2 Primary Header"
    oSec.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
    oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _
    "Page3 -- Section 2 First Page Footer"
    oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
    "Page4and5 -- Section 2 Primary Footer"

    '=== SECTION 3 ==================================================

    'Add a new section containing two pages that all have the same
    'header/footer
    .Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage
    Set oSec = .Sections(3)
    oSec.PageSetup.DifferentFirstPageHeaderFooter = False
    oSec.Range.InsertAfter "Text on Page 6 (Section 3)"
    .Range(oSec.Range.End - 1).InsertBreak wdPageBreak
    oSec.Range.InsertAfter "Text on Page 7 (Section 3)"

    'Add the headers/footers for the third section (that contains
    ' two pages)
    oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
    "Page6and7 -- Section 3 Primary Header Only"
    oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
    "Page6and7 -- Section 3 Primary Footer Only"

    'Save the document
    .SaveAs App.Path & "\mydoc.doc"

    End With

    'Make Word visible to examine the document
    oApp.Visible = True

    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    Re: Putting text in a footer

    Hi Guys,

    This my my current code;

    Dim oSec As Word.Section
    Dim oApp As Word.Application
    Dim oDoc As Word.Document

    Set oApp = Word.Application
    Set oDoc = oApp.Documents



    With oDoc
    Set oSec = .Sections(2)
    oSec.PageSetup.DifferentFirstPageHeaderFooter = True
    oSec.Range.InsertAfter "testing text"
    End With

    I already have a document open with all my pages and everything in there... all i want to do now is add my footer text to the second, third, fourth, etc pages... this code doesn't work.

    How should I change it?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Putting text in a footer

    You can do something like so.
    VB Code:
    1. Private Sub Test()
    2.  
    3.     ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
    4.     Selection.Extend
    5.     Selection.Extend
    6.     Selection.Extend
    7.     Selection.Text = "sgsf gnfn rthty"
    8.     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    9.  
    10. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    Re: Putting text in a footer

    I fixed it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width