Home > Blockchain >  Using VBA, how do I create a custom field in Word that uses the current page number?
Using VBA, how do I create a custom field in Word that uses the current page number?

Time:12-04

I'm trying to add a custom field in Word (in the shape { CUSTOM_FIELD } ) that uses the current page number and outputs its text representation (12 => twelve), but in multiple exotic (not supported) languages, which is why the built-in English variant (page * cardtext) isn't sufficient.

The VBA code won't be a problem, but I need to know how to create a custom field.

The field would be added to the footer template, before 100s of pages would be added programmatically.

I tried using a custom DocProperty, but wasn't able to find a way to integrate the needed behavior. Another linked answer seems to be using the existing { PAGE } field, which wouldn't help, as I need to insert the new field (once only) into the footer template.

CodePudding user response:

This isn't the only possible way to do this, but if you have a reasonably small maximum page count then you could use a { DOCVARIABLE } field something like this, where "LANG" is just a piece of text that identifies the language you want to use:

{ DOCVARIABLE "LANG{ PAGE }" }

You would then need to use VBA to set up 1 Document variable to store the text verion of each page number. Document variables are in essence key-value pairs stored invisibly in the document.

e.g. Let's suppose you were wanted the text versions of numbers from one to three in English, French and German. So you could have document variables with the following names and values

EN1  One
EN2  Two
EN3  Three
FR1  Un
FR2  Deux
FR3  Trois
DE1  Ein
DE2  Zwei
DE3  Drei

Even if you need hundreds or thousands of these, the amount of text you can store in Document variables is very large. OTOH if you need to be able to generate the texts dynamically by building them using an algorothm (as \* Cardtext probably does) this won't work.

To set up one of these variables youjust need, e.g.

ActiveDocument.Variables("EN1").Value = "One"

The field you would need for the English results would be

{ DOCVARIABLE "EN{ PAGE }" }

As long as you only need to use one language in each document, you could just change the "EN" to "FR" to get the French version, etc. - after all, if you only have one footer layout, you would only need to make one change. Otherwise, you could consider storing the language code somewhere else in the document, e.g.

  • in a bookmark called LANG, in which case you might use

    { DOCVARIABLE "{ LANG }{ PAGE }" }

  • in a DOCVARIABLE called LANG, so you would use

    { DOCVARIABLE "{ DOCVARIABLE LANG }{ PAGE }" }

  • in a Custom document property called LANG, so you would use

    { DOCVARIABLE "{ DOCPROPERTY LANG }{ PAGE }" }

(The problem with using custom document properties for your numbers is that you can only have a small number of them).

If that general approach can't be made to fit what you're trying to achieve, I think you'll probably need to clarify your Question some more.

CodePudding user response:

The Cardtext switch will give numbering in different languages, assuming that the language is applied to the text as proofing language.

enter image description here

I would recommend saving a { Page * Cardtext } field as AutoText or another Building Block in your template and using code to insert it. Here is my writing on using vba to insert AutoText.

The following creates the field at the insertion point.

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="Page \* CardText"
  • Related