I have two working macros that I'd like to combine into one. The reason for this is because while they both print out the Word document, they do it differently.
Macro One pulls the Mail Merge data into the Word document correctly but will only print to the selected / default printer.
Macro Two allows for a selection of a printer by an IP address and the document to print to the defined printer. It does not process the mail merge.
The goal is a user to start macro 1 to update the mail merge data and macro 2 to send it to a printer based on the IP. How can I join these two together?
Thanks for the help!
Macro 1
Macro 2
Macro One pulls the Mail Merge data into the Word document correctly but will only print to the selected / default printer.
Macro Two allows for a selection of a printer by an IP address and the document to print to the defined printer. It does not process the mail merge.
The goal is a user to start macro 1 to update the mail merge data and macro 2 to send it to a printer based on the IP. How can I join these two together?
Thanks for the help!
Macro 1
Code:
Sub label_query_to_print(data_query_to_print, excelPath, printer_id_value, ip_address_value)
Debug.Print "Data Query: " & data_query_to_print
Debug.Print "Excel Pathway: " & excelPath
Debug.Print "IP Address: " & ip_address_value
Debug.Print "Printer ID: " & printer_id_value
ActiveDocument.MailMerge.OpenDataSource Name:= _
excelPath _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source" & excelPath & ";Mode=Read;Extended Properties=""HDR=YES" _
, SQLStatement:=data_query_to_print, SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToPrinter
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End Sub
Macro 2
Code:
Sub PrintToPrinterByIP()
Dim strPrinterIP As String
Dim objWMIService As Object
Dim colPrinters As Object
Dim objPrinter As Object
Dim foundPrinter As Boolean
' Define the IP address of the printer (you can change this)
strPrinterIP = "172.16.209.36" ' Replace with your desired printer IP address
' Flag to check if the printer is found
foundPrinter = False
' Get the WMI service to query printers
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' Query to get all installed printers
Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
' Loop through all printers found
For Each objPrinter In colPrinters
' Check if the printer's port contains the desired IP address
If InStr(objPrinter.PortName, strPrinterIP) > 0 Then
' If found, set the active printer and print
Application.ActivePrinter = objPrinter.Name
ActiveDocument.PrintOut
MsgBox "Document sent to printer at IP address: " & strPrinterIP
foundPrinter = True
Exit For
End If
Next objPrinter
' If no printer was found
If Not foundPrinter Then
MsgBox "No printer found with IP address: " & strPrinterIP
End If
End Sub