Printing file to default printer, not to Application ActivePrinter in VBA

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi, I'm using the below code to print files to a different printer than the default printer.
But my file is always printing to the default printer, but not to the application.activeprinter

Please help me to sort out the issue.

VBA Code:
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _
   "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
   As String, ByVal lpFile As String, ByVal lpParameters _
   As String, ByVal lpDirectory As String, ByVal nShowCmd _
   As Long) As Long

Declare PtrSafe Function apiFindWindow Lib "User32" Alias "FindWindowA" _
   (ByVal lpclassname As Any, ByVal lpCaption As Any) As Long

Global Const SW_SHOWNORMAL = 1

Sub ShellExecuteExample()
''''''''''''''''''''''''''''''''''''''''
''https://support.microsoft.com/en-us/topic/wd2000-how-to-call-the-shellexecute-windows-api-function-80da207b-2fa3-ac60-e871-f0a63164bad7
'''''''''''''''''''''''''''''''''''''''
   Dim hwnd
   Dim StartDoc
   hwnd = apiFindWindow("OPUSAPP", "0")
   
      Dim mypath As String, myfile As String, strFilePath As String
        mypath = "C:\Users\User\Desktop\test\"
        myfile = Dir(mypath & "*.ai*")
        
        Dim my_printer As String, chkActivPrinter As String, chkDefultPrinter As String
        my_printer = Application.ActivePrinter
        Application.ActivePrinter = "PDFill PDF&Image Writer on Ne05:"
        chkActivPrinter = Application.ActivePrinter
        Do While myfile <> ""
               strFilePath = mypath & myfile
                StartDoc = ShellExecute(hwnd, "Print", strFilePath, "", "", SW_SHOWNORMAL)
                       'Application.Wait Now + TimeValue("00:00:07")
                myfile = Dir
        Loop
       Application.ActivePrinter = my_printer
       chkDefultPrinter = Application.ActivePrinter
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top