|
-
Nov 20th, 2005, 09:08 PM
#1
Thread Starter
Junior Member
[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
-
Nov 20th, 2005, 09:10 PM
#2
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.
-
Nov 21st, 2005, 12:09 AM
#3
Junior Member
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
-
Dec 13th, 2005, 09:53 PM
#4
Thread Starter
Junior Member
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?
-
Dec 13th, 2005, 10:15 PM
#5
Re: Putting text in a footer
You can do something like so.
VB Code:
Private Sub Test()
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Text = "sgsf gnfn rthty"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 13th, 2005, 10:22 PM
#6
Thread Starter
Junior Member
Re: Putting text in a footer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|