Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

modified code print different extensions files

0

hi

I search way to  make  this  code include many  diffrent extensions  files  for  printing  PDF,JPG,DOCX,..... etc  . the   current  code  prints  only  EXCEL FILES    based on COL A(FILE NAME), COL B   (COPIES)  to print count copies .   but  I  want  print  any  file  without  exception 


Sub printem()
Dim LR As Long, i As Long, x As Long
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To LR
        x = Cells(i, 2).Value
        On Error GoTo NoFile
        Workbooks.Open Filename:="C:\Users\PC WORLD\Desktop\ff\" & Cells(i, 1).Value
        ActiveWorkbook.Sheets.Select
        ActiveWindow.SelectedSheets.PrintOut Copies:=x, Collate:=True
        ActiveWorkbook.Close
NoFile:
    If Err <> 0 Then
        MsgBox "C:\Users\PC WORLD\Desktop\ff\" & Cells(i, 1).Value & " is not in the folder", vbExclamation
        Err.Clear
    End If
    Next i
End Sub
Answer
Discuss

Answers

0

Hi Mussa and welcome to the Forum

Your desire to ".. print  any  file  without  exception" sounds problematic to me - there are hundreds of file extensions, printed from many apps. 

Suggest you search for solutions using the Windows API called hwnd - I believe that prints using the apps detailed in your file associations set under Windows (so it should print any file for which you have an existing app / set file association).  I don't have the time or inclination to investigate or test that however

In practice you will have  a limited set (say pdf,. doc*, .txt) so you could use the file extension in the filename to identify the file type and have a sub (per file type) to use the Windows Shell to launch the default app and print that file.

The more file types you have, the more coding you'll need (and the bigger the risk that you'll have errors and print many unnecessary pages). Good luck! 


Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login