how to print via VBA

Jake975

Board Regular
Joined
Feb 9, 2020
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
I found this code to print a specific file with VBA but im not sure where to put the file name to be printed or if i just use t at a normal module
Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute _
        Lib "shell32.dll" Alias "ShellExecuteA" _
            (ByVal hwnd As LongPtr, _
             ByVal lpOperation As String, _
             ByVal lpFile As String, _
             ByVal lpParameters As String, _
             ByVal lpDirectory As String, _
             ByVal nShowCmd As Long) _
        As LongPtr
#Else
    Private Declare 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
#End If

Private Const SW_HIDE = 0

Sub FilePrint(ByVal strFilePath As String)

    Dim retVal As Long
    
        retVal = ShellExecute(0, "Print", strFilePath, 0, 0, SW_HIDE)

        If retVal < 32 Then
           '// there are Error codes for this..left out
            MsgBox "An Error occured...could not print"
        End If

End Sub
Any help would be great thank you for your time and support
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Jake

From what I can understand you have told the VBA to print "strFilePath", but you have not stated what or where "strFilePath" is.

If the file is static just add a line stating
VBA Code:
Dim strFilePath As String
then state where the file is located (I.e.
VBA Code:
strFilePath = "C:\Users\Jake\Desktop\file.xlsx"
)

If it is dynamic then someone better than me might need to help you,
 
Upvote 0
I can't seem to get it to print anything any ideas on how to externally Print several files of different types?
 
Upvote 0
Hi Jake975

If you look at the previous thread where you got that code (located here: Got a Macro to open a word document from Excel......but)
you will see the part you are missing to print a file. As I stated, you had not defined what or where "strFilePath" was, the code in the earlier thread had that info (posted by Ivan F Moala).

I hope this helps.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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