Creating headers using macros

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using Microsoft Word 2002 SP3 on XP to try and use macros to create
different headers within a document with, the user is prompted with MsgBox
for input. There seems to be two problems. 1) When I use a macro to go to the
current header ir does not. It may go to the last or the next header. 2) The
curser location in the header can not be controlled with a macro. When
instructed to go to the top then move down two lines it does not.

Any help would be appreciated.
 
The trick is to avoid the Selection object completely. Don't try to 'go' to
the header. Refer to the headers directly, eg

With Activedocument.Sections(3).Headers(wdHeaderFooterFirstPage)
.LinkToPrevious = false
.Range = "New header for first page of section 3"
End with
 
Thanks for the try Jezebel, I tried your suggestion but I couldn’t get it to
work. I’m not that savvy with VB so that no doubt is a obstacle. For further
clarification I want to create a section break and then go to this new
section, there could be any number of sections in the document, prompt for
user input to create the new header then return to the body of the document
until the next new section needs to be created. Or create new headers for the
next section as you go along in the document. If I’m reading what you sent me
right it would work for section 3. I need to create new headers for each new
section.
 
Section 3 was just for the example. You can use Document.Sections.Count to
find out how many sections there are in the document. You can use
Selection.Sections(1).Index to get the number of the first section
containing the current selection.

You could have a macro that inserts a section break at end of document, then
adds a new header to it, along these lines --

With ActiveDocument
.Sections.Add Start:=wdSectionNewPage
With .Sections(.Sections.Count).Headers(wdHeaderFooterFirstPage)
.LinkToPrevious = false
.Range = "New header for first page of new section"
End with
End with

Check Help for the arguments to the Sections.Add function and Headers()
collection.
 
I tried the last one with no sucess here is what I came up with. As you can
see the text from this macro is entered in the body and not the header of the
next section. help: thanks

With ActiveDocument
.Sections.Add Start:=wdSectionNewPage
With .Sections(.Sections.Count).Headers(wdHeaderFooterFirstPage)
.LinkToPrevious = False
.Range = "New header for first page of new section"
headervar = InputBox("1-Chapter 1 2-Chapter 2 3-Capter 3", "Header", 1)
Select Case headervar
Case "1"
Selection.TypeText Text:=" Chapter One "
Case "2"
Selection.TypeText Text:=" Chapter Two "
Case "3"
Selection.TypeText Text:=" Chapter Three "
End Select

End With
End With
 
Writing macros by trial and error like this is the devil's own task. If you
want to learn to write macros, take the time to read Help on it.

Your text is being entered in the body because that's where the selection
is. You'll either need to select the header, or -- better -- don't use the
Selection object in macros if you can avoid it -- and you nearly always can.

1. The Range statement is where you insert the text

..Range = "Chapter One" etc

2. Change wdHeaderFooterFirstPage if you don't have a different first page
header for each section.
 
Back
Top