Can't get macro to add header or number pages in Word 2007

K

Kurt

I am trying to record a macro that does a variety of things, and can't get it
to work.

I want the macro, in addition to other things, to create a header; and I
want the header to include a line that says Page x of y.

When the macro gets to the point that it is to create the header, I get an
error message referring to "Run time error 5941" and telling me "the
requested member of the collection does not exist"

I also can't even find a page numbering option that allows me to have a Page
x of y format.
 
J

Jay Freedman

First things first. You shouldn't be using a macro to create headers.
You should create a template with everything already in the header,
and documents based on that template will inherit the header without
any programming at all.

To see how to set up Building Blocks for proper Page X of Y headers
and footers, see
http://groups.google.com/group/microsoft.public.word.formatting.longdocs/msg/ab71df395f60bad2?hl=en&.

Finally, if you _must_ use a macro for some reason, you can't record
it; you have to program it from the start. Here's a demo of the kind
of work you have to do:

Sub MakeXofYHeader()
' this makes a right-aligned header
' containing only "Page X of Y"
' in the first section of the document

Dim oRg As Range
Set oRg = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
With oRg
.Text = vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldPage
End With
Set oRg = oRg.Paragraphs(1).Range
With oRg
.Collapse wdCollapseEnd
.Text = " of "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldNumPages
End With
Set oRg = oRg.Paragraphs(1).Range
oRg.Fields.Update
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
C

christy c

Hi
along these same lines - do you know if it is possible to use VBA to add to an exisiting header? I do have a template with the header defined but each time the template is used I want it to automatically addd the project name to the existing header.
I was just going to use a message box on open "what is the name of your project". Then I want the response to be added to the header of the document.

Thanks in advance for any help.



Jay Freedman wrote:

First things first. You shouldn't be using a macro to create headers.
13-Feb-07

First things first. You shouldn't be using a macro to create headers
You should create a template with everything already in the header
and documents based on that template will inherit the header withou
any programming at all

To see how to set up Building Blocks for proper Page X of Y header
and footers, se
http://groups.google.com/group/microsoft.public.word.formatting.longdocs/msg/ab71df395f60bad2?hl=en&

Finally, if you _must_ use a macro for some reason, you can't recor
it; you have to program it from the start. Here's a demo of the kin
of work you have to do

Sub MakeXofYHeader(
' this makes a right-aligned heade
' containing only "Page X of Y
' in the first section of the documen

Dim oRg As Rang
Set oRg = ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Rang
With oR
.Text = vbTab & vbTab & "Page
.Collapse wdCollapseEn
.Fields.Add Range:=oRg, Type:=wdFieldPag
End Wit
Set oRg = oRg.Paragraphs(1).Rang
With oR
.Collapse wdCollapseEn
.Text = " of
.Collapse wdCollapseEn
.Fields.Add Range:=oRg, Type:=wdFieldNumPage
End Wit
Set oRg = oRg.Paragraphs(1).Rang
oRg.Fields.Updat
End Su

-
Regards
Jay Freedma
Microsoft Word MVP FAQ: http://word.mvps.or
Email cannot be acknowledged; please post all follow-ups to th
newsgroup so all may benefit

On Tue, 13 Feb 2007 17:09:00 -0800, Kur

Previous Posts In This Thread:

Can't get macro to add header or number pages in Word 2007
I am trying to record a macro that does a variety of things, and can't get it
to work

I want the macro, in addition to other things, to create a header; and I
want the header to include a line that says Page x of y

When the macro gets to the point that it is to create the header, I get an
error message referring to "Run time error 5941" and telling me "the
requested member of the collection does not exist"

I also can't even find a page numbering option that allows me to have a Page
x of y format.

First things first. You shouldn't be using a macro to create headers.
First things first. You shouldn't be using a macro to create headers
You should create a template with everything already in the header
and documents based on that template will inherit the header withou
any programming at all

To see how to set up Building Blocks for proper Page X of Y header
and footers, se
http://groups.google.com/group/microsoft.public.word.formatting.longdocs/msg/ab71df395f60bad2?hl=en&

Finally, if you _must_ use a macro for some reason, you can't recor
it; you have to program it from the start. Here's a demo of the kin
of work you have to do

Sub MakeXofYHeader(
' this makes a right-aligned heade
' containing only "Page X of Y
' in the first section of the documen

Dim oRg As Rang
Set oRg = ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Rang
With oR
.Text = vbTab & vbTab & "Page
.Collapse wdCollapseEn
.Fields.Add Range:=oRg, Type:=wdFieldPag
End Wit
Set oRg = oRg.Paragraphs(1).Rang
With oR
.Collapse wdCollapseEn
.Text = " of
.Collapse wdCollapseEn
.Fields.Add Range:=oRg, Type:=wdFieldNumPage
End Wit
Set oRg = oRg.Paragraphs(1).Rang
oRg.Fields.Update
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Tue, 13 Feb 2007 17:09:00 -0800, Kurt

EggHeadCafe - Software Developer Portal of Choice
WPF - XAML TabControl MouseEnter TabItem TextBox SelectAll
http://www.eggheadcafe.com/tutorial...9e-a12b3fdf7971/wpf--xaml-tabcontrol-mou.aspx
 
M

macropod

Hi christy c,

You probably don't need vba for this:
1. If the document's filename is the same as the project name, a FILENAME field; or
2. If a particular paragraph contains just the project name, formatting that paragraph (or at least the project name) with a special
Style (eg 'Project', the attributes of which you define), combined with a STYLEREF field,
in the header may be all you need.
 
G

Gordon Bentley-Mix on news.microsoft.com

Alternatively, you could write the value you collect through your message box
(really an Input box or UserForm) into a document property or, more securely,
a document variable and insert a field in the header to display this value.
However, you may need to jump through a few hoops to get the field to update.
The easiest method would be to just open and close the Print Preview screen
(assuming that you have set the option to update fields on printing), or you
could write a bit of code to cycle through the headers in the document and
update the fields therein.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!


macropod said:
Hi christy c,

You probably don't need vba for this:
1. If the document's filename is the same as the project name, a FILENAME field; or
2. If a particular paragraph contains just the project name, formatting that paragraph (or at least the project name) with a special
Style (eg 'Project', the attributes of which you define), combined with a STYLEREF field,
in the header may be all you need.

--
Cheers
macropod
[Microsoft MVP - Word]


Hi
along these same lines - do you know if it is possible to use VBA to add to an exisiting header? I do have a template with the
header defined but each time the template is used I want it to automatically addd the project name to the existing header.
I was just going to use a message box on open "what is the name of your project". Then I want the response to be added to the
header of the document.

Thanks in advance for any help.



Jay Freedman wrote:

First things first. You shouldn't be using a macro to create headers.
13-Feb-07

First things first. You shouldn't be using a macro to create headers.
You should create a template with everything already in the header,
and documents based on that template will inherit the header without
any programming at all.

To see how to set up Building Blocks for proper Page X of Y headers
and footers, see
http://groups.google.com/group/microsoft.public.word.formatting.longdocs/msg/ab71df395f60bad2?hl=en&.

Finally, if you _must_ use a macro for some reason, you can't record
it; you have to program it from the start. Here's a demo of the kind
of work you have to do:

Sub MakeXofYHeader()
' this makes a right-aligned header
' containing only "Page X of Y"
' in the first section of the document

Dim oRg As Range
Set oRg = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
With oRg
.Text = vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldPage
End With
Set oRg = oRg.Paragraphs(1).Range
With oRg
.Collapse wdCollapseEnd
.Text = " of "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldNumPages
End With
Set oRg = oRg.Paragraphs(1).Range
oRg.Fields.Update
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Tue, 13 Feb 2007 17:09:00 -0800, Kurt

Previous Posts In This Thread:

Can't get macro to add header or number pages in Word 2007
I am trying to record a macro that does a variety of things, and can't get it
to work.

I want the macro, in addition to other things, to create a header; and I
want the header to include a line that says Page x of y.

When the macro gets to the point that it is to create the header, I get an
error message referring to "Run time error 5941" and telling me "the
requested member of the collection does not exist"

I also can't even find a page numbering option that allows me to have a Page
x of y format.

First things first. You shouldn't be using a macro to create headers.
First things first. You shouldn't be using a macro to create headers.
You should create a template with everything already in the header,
and documents based on that template will inherit the header without
any programming at all.

To see how to set up Building Blocks for proper Page X of Y headers
and footers, see
http://groups.google.com/group/microsoft.public.word.formatting.longdocs/msg/ab71df395f60bad2?hl=en&.

Finally, if you _must_ use a macro for some reason, you can't record
it; you have to program it from the start. Here's a demo of the kind
of work you have to do:

Sub MakeXofYHeader()
' this makes a right-aligned header
' containing only "Page X of Y"
' in the first section of the document

Dim oRg As Range
Set oRg = ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
With oRg
.Text = vbTab & vbTab & "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldPage
End With
Set oRg = oRg.Paragraphs(1).Range
With oRg
.Collapse wdCollapseEnd
.Text = " of "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldNumPages
End With
Set oRg = oRg.Paragraphs(1).Range
oRg.Fields.Update
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Tue, 13 Feb 2007 17:09:00 -0800, Kurt

EggHeadCafe - Software Developer Portal of Choice
WPF - XAML TabControl MouseEnter TabItem TextBox SelectAll
http://www.eggheadcafe.com/tutorial...9e-a12b3fdf7971/wpf--xaml-tabcontrol-mou.aspx

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top