I'm trying to use BarcodeTools' data matrix ActiveX object to be able to create a data matrix code on my excel sheet. I want the data to be encoded in the barcode to be through my user messages probing for the data. This is because I'm lookin to have it printed for it to be scanned later. For whatever reason though I just can't get excel to verify that I have the ActiveX object on my worksheet.
I'm able to place it in the worksheet. But when I step through the code it seems that the checks I have in place display there is nothing there.
I'm not sure if there is something I'm missing from using this ActiveX tool. Especially since when the macro runs, I am still getting a barcode to print. But it's not encoded with the string that the user would create. I know that Barcode Tools has their barcodes set as added ActiveX objects. An I can see when I right click on the barcode its properties. I'm just not sure why the barcode isn't taking the code when I have its setting for linked cell A1, nothing happens. With nothing changing when I try to change both that property and the data encoded property.
Public Sub CreateAndPrintLabel()
Dim createLabel As VbMsgBoxResult
Dim labelInfo As String
Dim sPath As String
Dim ObjDoc As Object
' Step 1: Ask the user if they wish to create a label
createLabel = MsgBox("Do you wish to create a label?", vbYesNo)
If createLabel = vbYes Then
' Step 2: Prompt for each parameter
licensePlate = InputBox("Enter License Plate:")
partNumber = InputBox("Enter Part Number:")
Description = InputBox("Enter Description:")
ExpirationDate = InputBox("Enter Expiration Date:")
' Step 3: Validate and display entered information
labelInfo = licensePlate & " | " & partNumber & " | " & Description & " | " & ExpirationDate
confirmMsg = "Is the following information correct?" & vbCrLf & vbCrLf & labelInfo
confirmed = MsgBox(confirmMsg, vbYesNo) = vbYes
' Step 4: Allow correction or confirmation
If Not confirmed Then
' Provide an option to go through the steps again if the information was incorrect
Call CreateAndPrintLabel
Exit Sub
End If
' Store labelInfo in cell A1 on Sheet1
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = labelInfo
' Check if Data Matrix data matches the content in cell A1
Dim dataMatrixControl1 As OLEObject
On Error Resume Next
Set dataMatrixControl1 = ThisWorkbook.Sheets("Sheet1").OLEObjects("DataMatrixCtrl.1")
On Error GoTo 0
If Not dataMatrixControl1 Is Nothing Then
If dataMatrixControl1.Object.DataToEncode = labelInfo Then
' Step 5: Print the label using Brother printer
Set ObjDoc = CreateObject("bpac.Document")
sPath = "C:\Users\giovanni.fontanetta\Documents\My Labels\Matrix.lbx"
' Open lbx file
If ObjDoc.Open(sPath) Then
' Set text for the entire label
ObjDoc.SetText 0, dataMatrixContent
' Print the label
ObjDoc.StartPrint "", bpoDefault
ObjDoc.PrintOut 1, bpoDefault
ObjDoc.EndPrint
' Close lbx file
ObjDoc.Close
Else
MsgBox "Failed to open label file."
End If
Else
MsgBox "Data Matrix content does not match A1."
End If
Else
MsgBox "ActiveX Data Matrix control not found on the sheet."
End If
End If
End Sub
Sub Print_Label()
Dim bRet As Boolean
Dim sPath As String
Dim ObjDoc As bpac.Document
Set ObjDoc = CreateObject("bpac.Document")
sPath = "C:\Users\giovanni.fontanetta\Documents\My Labels\Matrix.lbx"
'Open lbx file
bRet = ObjDoc.Open(sPath)
If (bRet <> False) Then
' Start Print-Setting
ObjDoc.StartPrint "", bpoDefault
ObjDoc.PrintOut 1, bpoDefault
' Finish Print-Setting.iStart the printing.j
ObjDoc.EndPrint
' Close lbx file
ObjDoc.Close
End If
End Sub