Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

There are two methods I have found to achieve this goal, I will leave the second method, but I don't use it since it required a separate text file outside the document. Method 1 stores the number in a custom document property.

The idea is I have a single page document and each time it prints should have a unique incrementing number so I can track say, I have doc # 45 to employee John  Smith.

I would reccomend reading through Create Simple Greeting Macro - Word - Microsoft Office if you are new to Macros.

Also, you will want to have this automated once it works as expected via this post >> Run Macro Before Printing - Word - Microsoft Office

Here is my final product which is "portable" for you to explore:

Serial_Numbered_Doc_Template.docm

Method 1:

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

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)

	Cancel = True

	Call FilePrint
 
	ActiveDocument.Saved = False
	If ActiveDocument.Saved = False Then ActiveDocument.Save

End Sub

Method 2:

You need to create a module inside word and a txt file containing the current incremental number.

I like to call the txt file the document name and store in the same location.

I am working on automating this process (see Method 1!) (See https://stackoverflow.com/questions/50769735/improving-vba-macro-in-word-to-automatically-create-file-relating-to-document-na/50772002#50772002)

Module:

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

settings.txt

[MacroSettings]
SerialNumber=1

Inside Word Document where you want to place the Serial Number:

Insert >> Bookmark >> "SerialNumber" >> Add


Sources:

https://wordmvp.com/FAQs/MacrosVBA/NumberCopiesOf1Doc.htm
https://stackoverflow.com/questions/50769735/improving-vba-macro-in-word-to-automatically-create-file-relating-to-document-na/50772002#50772002
https://stackoverflow.com/questions/48909968/running-a-macro-before-printing-a-word-document#
https://superuser.com/questions/1329822/embed-macro-in-document-word?noredirect=1#comment1985687_1329822
https://superuser.com/questions/1329783/print-serial-number-or-icrementing-number-in-word-document-footer
https://stackoverflow.com/questions/48348049/vba-compile-error-expected-function-or-variable?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
http://www.vbaexpress.com/forum/showthread.php?62073-Runnig-a-macro-before-printing
http://forums.whirlpool.net.au/archive/2603917

 

  • No labels