Skip to main content
Participating Frequently
May 23, 2018
Answered

Adding page numbers to PDF via (Excel) VBA

  • May 23, 2018
  • 1 reply
  • 5326 views

Dear community,

although this question was already asked a hundred times, I am stuck with it for two days now and hope somebody of you can help me.

I want to add page numbers to an existing PDF (at the bottom on the right side), beginning with number "1" on the second page of the document.

Through googling I found out that the "common" way to do this is adding, e.g., text fields and then flattening the document so that the text field becomes integrated in the PDF.

Unfortunatelly, I get various problems when I try doing that:

These are the relevant parts of my VBA code so far:

Dim AcroApp as Acrobat.CAcroApp

Dim jso as Object

Dim KurzGesamt as Acrobat.CAcroPDDoc

Dim i As Integer, intSeiten As Integer

Dim objTextfeld As Object

strPfadVerteilungEndlauf = ThisWorkbook.Path & "\Verteilung Endlauf\"

strNameKurzGesamt = "00 Kurzübersicht_komplett_" & Format(Date, "YYYY_MM_DD") & "_" & ThisWorkbook.Name & ".pdf"

Set AcroApp = CreateObject("AcroExch.App")

Set KurzGesamt = CreateObject("AcroExch.PDDoc")

KurzGesamt.Open (strPfadVerteilungEndlauf & strNameKurzGesamt)

Set jso = KurzGesamt.GetJSObject

intSeiten = KurzGesamt.GetNumPages

For i = 2 To intSeiten

    Set objTextfeld = jso.AddField("Textfeld", "text", i, Array(35, 470, 400, 440))

    objTextfeld.Value = Str(i)

    objTextfeld.textSize = 10

    objTextfeld.textFont = "Calibri"

Next i

jso.FlattenPages

Call KurzGesamt.Save(1, strPfadVerteilungEndlauf & strNameKurzGesamt)

Set jso = Nothing

Call AcroApp.CloseAllDocs

Set KurzGesamt = Nothing

Call AcroApp.Exit

Set AcroApp = Nothing

The "set objTextfeld" line leads to the "Object Required" error. Therefore, I first thought the declaration of the jso object does not work.

But when I omit the loop, the text field occurs on the first page of the PDF (with value 0).

When I then set the "i" in the "addfield" command to another page number, the field occurs on this page, but still with value 0.

So how can I

1. create a loop that inserts the page numbers on each page, from the second page on?

2. place the page number at the bottom on the right side? (What I tried concerning the array settings did not really lead to success.)

I also tried to solve the problem with watermarks what did not work either.

By the way, I have Acrobat 7.0 and MS Excel 2010 installed and the reference in Excel is set.

Thank you in advance for any helpful advice!

This topic has been closed for replies.
Correct answer Bernd Alheit

The loop still doesn't work. But if I select a certain page number in the addfield command now, apparently text fields with all page numbers are inserted on that page. Is that maybe a step in the right direction?


You must use i-1 as page number in the addfield command. The first page in the document is 0.

1 reply

Bernd Alheit
Community Expert
Community Expert
May 23, 2018

Form fields with the same name has always the same value. You must create fields with unique names.

Participating Frequently
May 23, 2018

Hello Bernd,

I already thought about that and tried "Textfeld(i)" and "Textfeld(str(i))" instead, but that makes no difference. Any ideas?

Bernd Alheit
Community Expert
Community Expert
May 23, 2018

Try this:

"Textfeld" & i