Error 5941 in trying to get VBA to number pages

L

Lisa

Hi. I'm new to Word VBA as of almost a week ago. I've been trying
to get the program to paginate starting at the current page starting
at number 1000. I'm hoping eventually to change the 1000 to a number
of the user's choice.

However, I have run into some difficulty ... I did this based upon
recording a macro, and the two commented out lines of code below when
not commented out have an error message : The requested member of the
collection does not exist.

I have been testing it out by creating a four page document, with
trying to put Page 1000 on page 3.

What does the error code mean? How do I fix it? Any other help you
can provide?

Thanks a lot

Sub Macro4()
'
' Macro4 Macro
'
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain
Number 3").
' Insert Where:=Selection.Range, RichText:=True
' If not commented out, what do these two lines above do?
' How do you get them to work right?
' Error: 5941: The requested member of a collection does not
exist.
With Selection.HeaderFooter.PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1000
End With
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use the following:

Dim prange As Range
With ActiveDocument
With .Sections(1)
Set prange = .Footers(wdHeaderFooterPrimary).Range
With .Footers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = InputBox("Enter the starting page number.")
End With
End With
.Fields.Add Range:=prange, Type:=wdFieldEmpty, Text:="PAGE"
prange.Paragraphs(1).Alignment = wdAlignParagraphCenter
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

Lisa

On Feb 25, 9:46 pm, "Doug Robbins - Word MVP on news.microsoft.com"

Hi Doug -- your snippet of code works except that I want the numbering
to start on the page with the cursor, not the first page of the
section. Any ideas?

I'll try to fix it myself too, but I look forward to seeing what you
or another MVP comes up with!

Lisa
 
L

Lisa

I added

Selection.InsertBreak Type:=wdSectionBreakNextPage

at the beginning, for better results, and changed With .Sections(1) to
With .Sections(2).


I'm still experimenting.

I'd rather not have page numbers at all until the new section where
the cursor is.
ti
I'll need a way to tell what section is the current section, i've
already inserted a page break, and how to paginate all sections after
the current section.

I'm still working -- Thanks a lot for your snippet!!!
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use:

Dim arange As Range
Dim prange As Range
Set arange = ActiveDocument.Range
arange.Start = Selection.Range.Start
Selection.InsertBreak wdSectionBreakNextPage
With ActiveDocument
With arange.Sections(2)
.PageSetup.DifferentFirstPageHeaderFooter = False
With .Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
Set prange = .Range
With .PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = InputBox("Enter the starting page
number.")
End With
End With
End With
.Fields.Add Range:=prange, Type:=wdFieldEmpty, Text:="PAGE"
prange.Paragraphs(1).Alignment = wdAlignParagraphCenter
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

I added

Selection.InsertBreak Type:=wdSectionBreakNextPage

at the beginning, for better results, and changed With .Sections(1) to
With .Sections(2).


I'm still experimenting.

I'd rather not have page numbers at all until the new section where
the cursor is.
ti
I'll need a way to tell what section is the current section, i've
already inserted a page break, and how to paginate all sections after
the current section.

I'm still working -- Thanks a lot for your snippet!!!
 
D

David Horowitz

Hi Lisa,
You mentioned you might like to know which section the cursor is in, and how
to work with all the sections from the cursor to the end of the document.
If you have say 10 Sections in your Document, you can tell which of those 10
Sections the cursor is in using:
Selection.Sections(1).Index
That gives you the Index (1 through 10) of the first Section in the
Selection.
The total number of Sections in the Document is given by:
ActiveDocument.Sections.Count
To loop over all Sections from that one to the end, you could use:
Dim iSection as Integer
For iSection = Selection.Sections(1).Index To ActiveDocument.Sections.Count
' Do something with ActiveDocument.Sections(iSection) ...
Next
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
I added

Selection.InsertBreak Type:=wdSectionBreakNextPage

at the beginning, for better results, and changed With .Sections(1) to
With .Sections(2).


I'm still experimenting.

I'd rather not have page numbers at all until the new section where
the cursor is.

I'll need a way to tell what section is the current section, i've
already inserted a page break, and how to paginate all sections after
the current section.

I'm still working -- Thanks a lot for your snippet!!!
 
P

pierre.soubourou

I think that if you want to access the current section you can also
use ActiveSection defined as:

ActiveSection = Selection.Information(wdActiveEndSectionNumber)

And then call with:

With .Sections(ActiveSection)

-Pierre
 

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