print any file using vba and excel

starpreast

New Member
Joined
Jun 9, 2013
Messages
12
I have a program that looks for 3 different types of file. A spreadsheet, a word document, and a .dwg file for autocad all with the same die number.

I need to have the program auto print them. I have a couple loops that find the correct paths etc. for them, but the problem is printing them.

http://www.mrexcel.com/forum/excel-...-open-word-document-excel-but.html#post422928

using the code in this post I can print them just fine, but the problem is the strFilepath is declared as a const in the code. Every time I try to redeclare it or anything I get an error. I tried just setting the file path as a string, and then passing along the correct path to it before calling the print command.

I need to know how I can change the file path to the another string programmatically.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hello starpreast,

This version of the macro will allow you supply the file path and name as an argument. The code has also been modified to work with 32 or 64 bit versions of Windows.
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
 
Upvote 0
I have vba 6 not 7. It refuses to acknowledge that part as being code.
Maybe I'm missing something, but I still haven't had any luck with this. When I try to declare strFilepath I'm not having any luck and it's throwing up an error "Argument not optional".

Can I declare it by simply ...

strFilePath = "S:\press\ ...."

and if so where do I need to put it in?

Sorry to be difficult.
 
Upvote 0
I am trying to use this function but I cannot see where you get to choose the printer. Basically I have a filepath and I want to print it but i want to select the printer to be save as pdf. Is this possible with shellexecute print command?
 
Upvote 0
where do I put the file path locations to be printed?
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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