Word – Print Serial Number or Incrementing Number in Word Document Footer

microsoft wordmicrosoft-word-2013printingserial numbervba

I feel this is likely a dup, but I can't find good answers and "Question's that may already have your answer" only shows an answer for excel.

I'm looking to track documents printed by having a serial number aka an incrementing number on each document printed.

The goal is that we have one document that is the master and then logs.

I want each log to have an incremental/unique number and then people can write that unique number on the master document for easy reference to the corresponding log.

I have been finding some complicated VBA, but I'm unfamiliar with adding VBA code to Word and that seems complicated, I figured word has a built in function for this that I am missing.

I want the field to look like this:

Doc #: 1

Next time we print it should show in the footer

Doc #: 2

etc etc.

Thank you,

PS: Office 2013 is currently what we use.

https://stackoverflow.com/questions/50769735/improving-vba-macro-in-word-to-automatically-create-file-relating-to-document-na

Embed Macro in Document Word

Print Serial Number or Icrementing Number in Word Document Footer

https://stackoverflow.com/questions/48909968/running-a-macro-before-printing-a-word-document

Best Answer

Seem's VBA is the way to go, I have only found one solution that fits my problem and works.

This was my final blog post --> https://www.freesoftwareservers.com/display/FREES/Adding+Serial+Number+or+Incremental+Number+to+Word+Document+at+Print+Time

Method 1: (This method makes the document portable, but doesn't have unique numbers for each document)

Serial_Numbered_Doc_Template.docm

  • First create a "Custom Document Property" named "Counter" and set the initial value to 0 (or whatever).

  • Insert into document where you want it to be when printed (CTRL + F9)

  • This module will increment the number by one each time the module is called:

    Sub FilePrint()
    Dim i As Long, j As Long
    With ActiveDocument
      j = CLng(InputBox("How many copies to print?", "Print Copies"))
      For i = 1 To j
    With .CustomDocumentProperties("Counter")
      .Value = .Value + 1
    End With
     .Fields.Update
        ActiveDocument.PrintOut Copies:=1
    Next
       .Save
    End With
    End Sub
    
  • Now I modified my "EventClassModule" to automatically call this sub when printing:

    Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    ' MsgBox "Before Print"
    ' Call Greeting
    ' Call SerialNumber
     Cancel = True
     Call FilePrint
    End Sub
    

Method 2: (This method uses a file to track the serial number which has the advantage of have multiple word docs share the same settings/update log and each word doc should have a unique number)

I followed this guide found here -->

https://wordmvp.com/FAQs/MacrosVBA/NumberCopiesOf1Doc.htm

Of course the code posted on the site is incorrect...

The problem is it needs to have this code snippet:

' Display message, title, and default value.
Dim SerialNumber As String
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\settings.txt", "MacroSettings", "SerialNumber")

Note the line Dim SerialNumber As String which is NOT on the website. Without it, you get the error found in this question >>

https://stackoverflow.com/questions/48348049/vba-compile-error-expected-function-or-variable

For posterity, I will copy the guide here:

Also note, I made one other change which is that the site shows two places for "settings.txt", but one is "Settings.Txt" and the other is "Settings.txt". I switched it to "settings.txt" as I'm a Linux guy and know how caps and spaces etc can mess you up in the future.

------------------------------------------------`

Create a bookmark named SerialNumber in the document where you want the Serial Number to appear. It can be in the header or footer if that is where you want the number. Then create a macro containing the following commands to print the document.

It will ask for the number of copies that you want to make and sequentially number each copy.The first time this macro runs, the first copy will be numbered 1 and when it finishes running, it will store in aSettings.Txt file the number that is one more that the number on the last copy.The next time the macro is run, it will start numbering the copies from that number. If when you first start, you want the numbers to start at some number other than 1, run the macro, entering 1 as the number of copies and then open Settings.Txt file and replace the number in the file with the number that you want as the first in the series.At any time thereafter, if you want the series to start at a particular number, you can open that file and replace the number in it with the number that you want to be the first in the series.

Sub SerialNumber()
'
' SerialNumber Macro
'
'
Dim Message  As String, Title  As String, Default  As String, NumCopies  As Long
 Dim Rng1   As Range

 ' Set prompt.
Message = "Enter the number of copies that you want to print"
 ' Set title.
Title = "Print"
 ' Set default.
Default = "1"

' Display message, title, and default value.
Dim SerialNumber As String
NumCopies = Val(InputBox(Message, Title, Default))
 SerialNumber = System.PrivateProfileString("W:\settings.txt", _
 "MacroSettings", "SerialNumber")

 If SerialNumber = "" Then
     SerialNumber = 1
 End If

 Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
 Counter = 0

 While Counter < NumCopies
     Rng1.Delete
     Rng1.Text = SerialNumber
     ActiveDocument.PrintOut
     SerialNumber = SerialNumber + 1
     Counter = Counter + 1
 Wend

'Save the next number back to the Settings.txt file ready for the next use.
System.PrivateProfileString("W:\settings.txt", "MacroSettings", _
         "SerialNumber") = SerialNumber

'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
     .Add Name:="SerialNumber", Range:=Rng1
 End With
End Sub

Note: You can/should probably change location of "settings.txt" to your LAN preferred location!

settings.txt

[MacroSettings]
SerialNumber=1
Related Question