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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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