Insert PageNumbers by VBA

B

Brock

Ok, sounds simple but...

My issue is figuring out a way to replace the # and ## at
the bottom of the code with what you can do manually
using the "Insert Page Number" and "Insert Number of
Pages" from the Header and Footer Toolbar when editing a
document. This is the last piece in a very complex
reporting process that i would appreciate getting
resolved. If anyone has any info or can point me in the
right direction i would greatly appreciate it.

PS - I know about .PageNumbers.Add but you can see from
my code why it doesn't work. I need more and i need to
control it more than what is provided. You can do it
manually so you should be able to do it programatically
(well in theory anyway).

Brock W Denys
Sunbaked Software Inc.
(code below)


With docWD.Sections(1).Headers(wdHeaderFooterPrimary)
Set rngWD = docWD.Sections(1).Headers
(wdHeaderFooterPrimary).Range
.Range.Tables.Add rngWD, 1, 2
.Range.InlineShapes.AddPicture
FileName:="C:\bitmap.bmp"
.Range.Paragraphs.Alignment = wdAlignParagraphLeft
.Range.Tables(1).Cell(1, 2).Range.InsertAfter ("Line
1" & vbCrLf & "Line 2" & vbCrLf & "Page # of ##")
End With
 
D

Doug Robbins - Word MVP

Hi Brock,

I think that the right direction for you to be pointed in is to have you
reporting process make use of a template that already has the footer created
in it with those fields in the appropriate place in the footer.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Hi Brock,

Well, under protest <g>, use:

Dim rngWd As Range, RngWd1 As Range, docWd As Document
Set docWd = ActiveDocument
Set rngWd = docWd.Sections(1).Headers(wdHeaderFooterPrimary).Range
rngWd.Collapse wdCollapseStart
rngWd.Tables.Add Range:=rngWd, NumRows:=1, NumColumns:=2
rngWd.Paragraphs.Alignment = wdAlignParagraphLeft
rngWd.Tables(1).Cell(1, 2).Range.InsertAfter ("Line 1" & vbCr & "Line 2" &
vbCr)
Set RngWd1 = rngWd.Tables(1).Cell(1, 2).Range.Paragraphs(3).Range
RngWd1.InsertBefore "Page "
Set RngWd1 = rngWd.Tables(1).Cell(1, 2).Range.Paragraphs(3).Range
RngWd1.Start = RngWd1.End - 1
RngWd1.Collapse wdCollapseStart
ActiveDocument.Fields.Add Range:=RngWd1, Type:=wdFieldPage
Set RngWd1 = rngWd.Tables(1).Cell(1, 2).Range.Paragraphs(3).Range
RngWd1.Start = RngWd1.End - 1
RngWd1.Collapse wdCollapseStart
RngWd1.InsertAfter " of "
Set RngWd1 = rngWd.Tables(1).Cell(1, 2).Range.Paragraphs(3).Range
RngWd1.Start = RngWd1.End - 1
RngWd1.Collapse wdCollapseStart
ActiveDocument.Fields.Add Range:=RngWd1, Type:=wdFieldNumPages

You'll have to work out how to put the picture back into this.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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