Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsschenk/d5edd3db92b07e463945cf7e8ce6529c to your computer and use it in GitHub Desktop.
Save larsschenk/d5edd3db92b07e463945cf7e8ce6529c to your computer and use it in GitHub Desktop.
Word Macro (VBA) to add one barcode to each page containing a variable content and a page number.
Option Explicit
' Konzept - Barcode beim Öffnen eines Dokumentes hinzufügen.
'
' Ein oder mehrere Barcodes werden beim Öffnen eines Dokumentes bzw. beim Aufrufen von
' AddBarcodes unter bestimmten Bedinungen eingefügt.
' Auf jeder Seite wird unten rechtes jeweils ein Barcode eingefügt.
' Barcodes werden nicht als InlineShape eingefügt sondern als ein freei fließenes Shape.
' Damit wird die Positionierung des Barcodes flexibel ermöglicht.
' Als InlineShape kann der Barcode nicht frei im Dokument positioniert werden sondern muss
' sich Inline im Text befinden.
' Nachteil: Frei positionierbare Objekte sind nicht absolut stabil.
' Es kann Probleme geben wenn die frei positionierbaren Objekte in der Position verändert werden.
'
' Es wird getestet ob ein Barcode bereits eingefügt wurde.
' Wenn ein Barcode bereits existiert, werden keine weiteren Barcodes eingefügt.
' Aber alle Barcodes werden mit einem aktuellen Timestamp versehen.
'
' Wenn noch kein Barcode vorhanden ist soll der Barcode mit einem bestimmten Wert belegt werden.
' Dieser Wert ist dem Dokument zu entnehmen - z.b. immer bestimmte Werte aus einem Feld oder
' einem Bookmark. Derzeit wird zur Demo die aktuelle Zeit in den Barcode übernommen.
'
' Der Barcode soll beim Einfügen immer ans untere, rechte Ende einer jeden Seite eingefügt werden.
' Da die Dokumente unterschiedliche Formate haben können (z.B. Portrait und Landscape),
' kann keine statische Position verwendet werden. Diese muss ermittelt werden.
'
' Der naheliegende Ansatz Barcode im Header oder Footer zu verwenden, funktioniert hier nicht, da
' nur ein BarcodeObjekt in den Header/Footer verwendet werden kann der dann für alle Seiten identisch ist.
' Ändert man den Text in Barcode in Header/Footer so ändert sich der Barcode
' für alle Seiten was nicht gewünscht ist. Daher müssen Barcode je Seite eingefügt werden.
' Das ist eine komplexere Aufgabe und erfordert Beachtung wenn Seiten neu eingefügt oder gelöscht werden.
' Hierbei müssen dann die Seitenzahlen neu zugewiesen werden.
' Insofern stellt dieser Lösungsansatz nur eine rudimentäre Lösung für dieses komplexe Problem dar.
'
' (c) 2019-2020 www.activebarcode.de
' Lars Schenk, info@activebarcode.de
'
' Check if a barcode is already created
' Return TRUE if there is a barcode
Private Function DocumentHasABarcode() As Boolean
Dim result As Boolean
result = False
Dim shp As Shape
Dim sVersion As String
For Each shp In ActiveDocument.Shapes
If shp.Type = msoOLEControlObject Then
sVersion = shp.OLEFormat.Object.Version
If (VBA.Left(sVersion, 2) = "6.") Then
result = True
End If
End If
Next
DocumentHasABarcode = result
End Function
' This public sub cam be called from makros, too.
' It will also be called whenever a documen is opened.
Public Sub AddBarcodes()
Dim widthInPoints As Double
Dim heightInPoints As Double
Dim barcodeText As String
Dim shouldAddBarcode As Boolean
widthInPoints = 200
heightInPoints = 75
' Use timestamp as barcode text to show some usaful stuff in this concept
barcodeText = Time ' Can be used from bookmark or field
' Test if a barcode already exists to avoid adding multiple barcodes
' TODO add more condition here when not to add barcodes. I.e. if required datasource are not available.
shouldAddBarcode = Not DocumentHasABarcode()
If shouldAddBarcode Then
Dim aBarcode As Object
Dim pge As Page
Dim pgNumber As Integer
pgNumber = 0
For Each pge In ActiveDocument.ActiveWindow.Panes(1).Pages
pgNumber = pgNumber + 1
' pre ActiveBarcode Version 6.8.0 use:
' ClassType:="BARCODE.BarcodeCtrl.1"
' since ActiveBarcode Version 6.8.0:
Set aBarcode = ActiveDocument.Shapes.AddOLEControl(ClassType:="ACTIVEBARCODE.BarcodeCtrl.1", _
Anchor:=ActiveDocument.GoTo(What:=wdGoToPage, Count:=pgNumber))
' position barcode to the end of the page:
aBarcode.OLEFormat.Object.Text = barcodeText + " Page: " + CStr(pgNumber)
' Set type to Datamatrix24x24 (56)
aBarcode.OLEFormat.Object.Type = 56
aBarcode.top = ActiveDocument.PageSetup.PageHeight - _
ActiveDocument.PageSetup.BottomMargin - _
ActiveDocument.PageSetup.FooterDistance - _
heightInPoints
aBarcode.Left = ActiveDocument.PageSetup.PageWidth - _
ActiveDocument.PageSetup.RightMargin - _
widthInPoints
aBarcode.Width = widthInPoints
aBarcode.Height = heightInPoints
Next
Else
' Iterate through all barcodes and set new text.
Dim sVersion As String
Dim shp As Shape
For Each shp In ActiveDocument.Shapes
If shp.Type = msoOLEControlObject Then
sVersion = shp.OLEFormat.Object.Version
If (VBA.Left(sVersion, 2) = "6.") Then
shp.OLEFormat.Object.Text = barcodeText
End If
End If
Next
End If
End Sub
' Event Handler when opening a document.
Private Sub Document_Open()
' Enable this line if you wanto automatically add Barcodes when opening a document:
' AddBarcodes
' With this the line above commented out, you will have to start the macro "AddBarcodes" manually.
End Sub
@larsschenk
Copy link
Author

Screenshot when using his VBA macro on a document with two pages.
Content of the barcodes is a timestamp and the page number.

Screenshot 2020-03-26 16 56 55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment