Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Page X of Y into a footer from a VBA function?

Status
Not open for further replies.

blinocac

Programmer
Jan 9, 2007
7
US
I am working on a form, and the form includes a checkbox that changes the wording of the footer. How do I insert the Page X of Y page numbering format from within my VBA Function?
 
Which application ? Which code in which function ?
Please, read carefully either FAQs in my sig.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
My apologies. I am working in Word.

The funtion looks like this:

Private Sub CheckBox31_Click()

With ActiveDocument.Sections(1)

CurPage = Selection.Information(wdActiveEndPageNumber)
TtlPgs = Selection.Information(wdNumberOfPagesInDocument)

If CheckBox31 = True Then .Footers(wdHeaderFooterPrimary).Range.Text = "Draft Copy" Else: .Footers(wdHeaderFooterPrimary).Range.Text = "Revised July 2006 DEQ Form #205-002 "
End With


Rem With ActiveDocument.Sections(1)
Rem .Footers(wdHeaderFooterPrimary).PageNumbers.Add _
Rem PageNumberAlignment:=wdAlignPageNumberRight, _
Rem FirstPage:=True
Rem End With

End Sub
 
A bit more information would help. If it is Word you are asking about (and you need to mention things like that) there is an AutoText entry for "Page X of Y". Have you tried using that?

This is a way of asking...what HAVE you tried? And what did you try to do it in (the application)?

Mentioning a "Function" is pointless for us, as we have no clue at all what that function is.

Gerry
My paintings and sculpture
 



Hi,

Did you try turning on your macro recorder and inserting [page] of [pages] using the Header/Footer Toolbar?

Skip,

[glasses] [red][/red]
[tongue]
 
Tools,Macro,RecordNewMacro
View,Header and Footer
A floating "Header and Footer" menu appears

Choose "Switch between Header and Footer"
Then the relevant AutoText from the "Insert AutoText" dropdown.

Stop the recording and examine your code.


Gavin
 
Straight from the macro recorder:
Sub Macro1()
'
' Macro1 Macro
' Macro enregistrée le 09/01/2007 par PHV
'
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages
End Sub


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
There are so many ways to go about this. Here is another.
Code:
Sub CheckBox31_Click()
Dim r As Word.Range
' for footers 1 = wdHeaderFooterPrimary
Set r = ActiveDocument.Sections(1).Footers(1).Range
  If CheckBox31 = True Then
    With r
      .Text = "Draft Copy  "
      .Collapse Direction:=wdCollapseEnd
    End With
    ActiveDocument.AttachedTemplate. _
        AutoTextEntries("Page X of Y").Insert _
        Where:=r
  Else
    With r
     .Text = "Revised July 2006     DEQ Form #205-002      "
    End With
  End If
Set r = Nothing
End Sub
This makes a Range of the footer and either:

Inserts text ("Draft Copy "), collapses the Range to the end, and inserts the AutoText Page X of Y,

OR, just inserts different text.

I was not sure where you were thinking of putting the Page X of Y. Or if you wanted it in both cases. Adjust accordingly.

Generally speaking, it is better to use a Range, than use Selection.

Gerry
My paintings and sculpture
 
I finally ended up using this solution


Private Sub CheckBox31_Click()

With ActiveDocument.Sections(1)
Dim CurPage As String
Dim TtlPgs As String

CurPage = Selection.Information(wdActiveEndPageNumber)
TtlPgs = Selection.Information(wdNumberOfPagesInDocument)

If CheckBox31 = True Then .Footers(wdHeaderFooterPrimary).Range.Text = "Draft Copy" Else: .Footers(wdHeaderFooterPrimary).Range.Text = "Revised July 2006 DEQ Form #205-002 Page " + CurPage + " of " + TtlPgs


End With


End Sub
 
Well that will work. But if you ever change the number of pages it will be incorrect. You are not putting in anything that will adjust. You are putting in text, not fields.

You add a page, you delete some text that reduces the page numbers...poof, the numbers will be wrong.

Not only that but:
Code:
 With ActiveDocument.Sections(1)
 Dim CurPage As String
 Dim TtlPgs As String
 
 CurPage = Selection.Information(wdActiveEndPageNumber)
 TtlPgs = Selection.Information(wdNumberOfPagesInDocument)
actually can deal with two - DOES deal with two - different things.

The actions will indeed change Section(1). But this will only, ever, be accurate for you if the Selection is in Section 1. The Selection.Information will be taken from where ever the Selection is. If the Selection is not in Section 1, then the information will still be taken, but from where ever it is.

If you are doing it this way - still without any updating fields - then you may want to consider checking to see if the Selection IS where you want it to be.

BTW: using Selection is not efficient coding for most things.

For example, I just made a document with three Sections. When I ran your code, yes, it works...and put

Revised July 2006 DEQ Form #205-002 Page 17 of 17

in the footer of Page 1. The Selection was not on Page 1.

Your code will work, but it has flaws.

Why do you not want to use fields? Is this document NEVER going to change? The code I supplied will work with no errors. Just adjust it to the way you want. I would have done it but you did not actually state clearly what it was.
Code:
If CheckBox31 = True Then
  With r
   .Text = "Draft Copy  "
  End With
Else
  With r
   .Text = "Revised July 2006    DEQ Form #205-002      "
   .Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate. _
        AutoTextEntries("Page X of Y").Insert _
        Where:=r
    End With
End If

Again, though, using Selection often is not a good idea.

Gerry
My paintings and sculpture
 
Yeah I got to realizing that a little later. I actually wrote that code before I saw your post.
 
hmmmm, really.

Wrote...what code? The code that you say:
I finally ended up using this solution
The one that does not use fields, and uses Selection?

If you are using something different from the code you state you "ended up using", it would be polite to share with us.

What DID you end up using?

Gerry
My paintings and sculpture
 
The one that I quoted there where I said I finally ended up using this solution. It looked at first as if it would work, until I realized that all the pages were 1.

I gave your solution a whirl and it didn't work either, so I may be back to square one.
 
It is a bit annoying when someone says "it didn't work". I have no issue with it not working, if that is the case, but I DO have issue with someone not saying what "didn't work" means.

WHAT didn't work?

It not possible that the pages were all 1 - which you state happened with your last code. If the AutoText was inserted the pages will work.

So please post EXACTLY what code you used, and HOW you used it.

Gerry
My paintings and sculpture
 
This code

Sub CheckBox31_Click()
Dim r As Word.Range
' for footers 1 = wdHeaderFooterPrimary
Set r = ActiveDocument.Sections(1).Footers(1).Range
If CheckBox31 = True Then
With r
.Text = "Draft Copy "
.Collapse Direction:=wdCollapseEnd
End With
ActiveDocument.AttachedTemplate. _
AutoTextEntries("Page X of Y").Insert _
Where:=r
Else
With r
.Text = "Revised July 2006 DEQ Form #205-002 "
End With
End If
Set r = Nothing
End Sub

Didn't show a page number at all. Sorry that you took offense, I'm just trying to solve a problem here, and I've run through a plethora of different solutions in the last 4 days, none of which have worked 100%.
 
I am not offended. I am trying to help. It just would make it easier to help if you actually gave decent information.

"Didn't show a page number at all. "

OK, that is...some information. Man, this is like pulling teeth. May I suggest you read the FAQ here on posting good posts in order to get the best answers?

"Didn't show a page number at all. "

.......SO...if I may ask...what DID it show? It did nothing? There was no text at all? Which situation did you test - the checkbox true, or false? If it was True, did it insert the text "Draft Copy, but NOT the AutoText Page X of Y? If it was False, was there text inserted?

I am not offended, but neither can I look over your shoulder and see what happened. You have to tell us.

Gerry
My paintings and sculpture
 




blinocac,

Please realize that YOU are thoroughly familiar with your problem. All we know, is what YOU tell us.

It's kinda like going to the doctor, and saying, "Doc, I don't feel good. Make me better!"

What chance would your doctor have, with ONLY that information. At least he can SEE you, even if you uttered not another word. We can't see ANYTHING, but what you tell us.

Please be CLEAR, CONCISE & COMPLETE.

Skip,

[glasses] [red][/red]
[tongue]
 
Just to take the sting off of that, I want to reiterate that I really AM trying to help. I would like to help you solve your problem, as I definitely believe it is solvable. So we are not trying to trash you, or make you feel offended yourself - we are trying to tell you to give us what we need to assist.

It is in your interest to do so.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top