View Full Version : Header Page Number replacement
dan300
03-14-2013, 09:16 AM
I have a page number field in my header. I want to replace the numbers with a string that comes from a DLL. For simplicity, assume sending a '1' to the DLL returns 'A'. How do I replace the page value with the returned string?
Replacing is not necessary, the returned string could go in another field or bookmark.
dan300
03-14-2013, 05:56 PM
Some code to better explain
Sub NumToAlpha()
Dim N2A As MyDLLClass
Set N2A = New MyDLLClass
Dim pagenum As String
pagenum = SomeFunctionToGetThePageNumFromTheHeaderPageField 'most tries at this gives a '1' in the header field of all pages
Dim pagealpha As String
pagealpha = N2A.GetAlpha(pagenum) 'my DLL function to get 'A' from '1' etc.
ActiveDocument.Bookmarks("AlphaPage").Range.Text = pagealpha
End Sub
fumei
03-14-2013, 08:02 PM
Function SomeFunctionToGetThePageNumFromTheHeaderPageField() As String
SomeFunctionToGetThePageNumFromTheHeaderPageField = _
ActiveDocument.Sections(1).Headers(1).Range.Fields(1).Result
End Function
This gives the pagenumber result of the page the Selection is on.
Selection on page 17, this returns 17
Selection on page 6, this returns 6
dan300
03-15-2013, 04:52 AM
I think I had tried that. That returns the page number that the cursor is currently on. I need it to be dynamic like the page number field is. Maybe I'm doing something wrong.
macropod
03-15-2013, 06:03 AM
I need it to be dynamic like the page number field is. Maybe I'm doing something wrong.
Maybe you have the wrong expectations. Page number fields update for each page because that's how they're designed. A few other fields function in like manner (eg StyleRef). However, any static text you put into a header is just that: static.
fumei
03-15-2013, 11:23 AM
I think you definitely have wrong expectations. You state you want "it to be dynamic like the field number field is".
Well...it IS dynamic. It is completely dynamic, based on where the cursor is. WHAT would you want it to be dynamically based on.
dan300
03-15-2013, 04:24 PM
Doesn't seem like it should be that hard to make pages be A, B, C instead of 1, 2, 3.
macropod
03-15-2013, 04:31 PM
Doesn't seem like it should be that hard to make pages be A, B, C instead of 1, 2, 3.
No, that isn't at all hard. It can be done via a field switch. But that is an entirely different matter to pasting content into the header. Anything you paste into the header on one page will be reflected on every page using that header.
To get different header content on each page, you need to rely on what's available via field coding or use a different header for every page. And, since a given Section can have at most 3 headers, hard-coding the header content can mean inserting Next Page Section breaks at least that often - which rather compromises subsequent editing.
dan300
03-15-2013, 04:34 PM
Can you give some guidance on doing a field switch?
Or point me to some reading material?
macropod
03-15-2013, 04:49 PM
See: http://office.microsoft.com/en-us/word-help/insert-and-format-field-codes-in-word-2010-HA101830917.aspx?CTT=5&origin=HA102110133
Of course, if you use Word's page numbering tools (available from the Ribbon's "Header & Footer Tools" tab when you're in the header/footer), it can even be done without a switch ...
dan300
03-15-2013, 05:20 PM
OK, I probably made my ABC illustration too simple, since that's included in the standard format switches.
If I wanted 1x1, 2x2, 3x3, to show instead of the page numbers 1, 2, 3, how would I do it? The vba code would do s = s & "x" & s to get the page number to be shown.
fumei
03-15-2013, 07:44 PM
I really think you need to be clear in your own mind what you want. So you now do NOT want alpha (A, B, C...), but a repeated page number. Strange.
macropod
03-17-2013, 01:33 AM
If I wanted 1x1, 2x2, 3x3, to show instead of the page numbers 1, 2, 3, how would I do it? The vba code would do s = s & "x" & s to get the page number to be shown.
Why do you suppose you need vba code for this? All you need is two PAGE fields, with an 'x' between them.
dan300
03-17-2013, 06:54 AM
Again, I've made my illustration too simple. I need to take the page number that would normally appear in the header page field and give it to a dll that will produce a proprietary barcode that has the page number embedded. And then put that object into the header, maybe in the the form of a image or a string.
I have everything covered except for the page data to hand to my dll.
fumei
03-17-2013, 10:33 AM
"maybe in the the form of a image or a string"
maybe? maybe?
Two rather different things, and you do not know which one is going to be used. Hmmmmm.
"I have everything covered except for the page data to hand to my dll"
It does not sound like you have everything covered, if you do not know if it is going to be an image or a string.
In any case, if what you are saying is that you want a DIFFERENT image or string on each page, then you can NOT do so unless you make each page different vis-a-vis sections. You need to start there. PLUS, again, you can not get the dynamic result of the field except using each page via selection.
macropod
03-17-2013, 02:22 PM
Dan300: When you decide what it is you really want to do and to stop giving us examples to solve that aren't what you want to do, then ask for help after actually spending a little bit of your time learning how Word works, maybe (only maybe) I might try to help you. So far, you've done nothing but waste everyone's time.
dan300
03-17-2013, 03:05 PM
I think my question is very simple and straight forward. I need to pass the page number to my dll (already written and debug) which can pass back either a image or a string. Seems like everybody wants to over complicate the whole thing. It seems like not knowing the answer here is an excuse to criticize the person asking the question. I've wasted a lot of bandwidth here myself.
fumei
03-17-2013, 04:50 PM
Except for this...you HAVE the answer to your question.
I know the answer to your question. Go to each page, using Selection, run the function to get the current result of the page number field.
Voila. The page number.
However, if you actually would read the thread, you will understand that you can NOT put different images (or string) on each page. There you go. The answer, at least as you have stated it. If you want page 10 (say) to have a different image or string from page 11 then you MUST have them in different sections. Section can have three different headers, no more.
This is a fact of Word. You will have to deal with it.
As for passing the page number to a procedure in the DLL, say something like:
Function A_Number() As Long
A_Number = _
ActiveDocument.Sections(1).Headers(1).Range.Fields(1).Result
End Function
Sub DLL_Procedure(A_Number As Long)
whatever it is you doing...using hmmm a string. an image
You have, as you have stated all the rest of it covered.
You will have to use a page by page movement of the Selection to get each number, but then...you STILL will not be able to use the output of whatever happens in the DLL unless each page is properly set up with the Sections.
fumei
03-17-2013, 04:56 PM
Oh, and:
"I think my question is very simple and straight forward."
"Seems like everybody wants to over complicate the whole thing."
You may think that, but you would be wrong. We are asking things because it seems that you do not fully understand the complications of headers in Word. And, your question is answered. Use the function given in the early post.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.