I am trying to ExportAsFixedFormat any excel files from a specific folder if they match from my range B3 to lr. (range is the filename minus the extension)
This is what i have so far. Currently it tries to open each file (not all are excel files, some are autocad) and export the .xlsx as a pdf. It PDFs the current page (which i do not want) and it PDFs one of the .xlsx files from my list.
I do not want to print all excel files from that folder only the ones from the range.
Thanks for any feedback.
Sub ExcelPrint()
Dim myFile As String, myFolder As String
Dim LoginName As String, destFolder As String
myFolder = Range("B1").Value
lr = Cells(Rows.Count, "B").End(xlUp).Row
LoginName = UCase(GetUserID)
destFolder = "C:\Users\" & LoginName & "\Desktop\_PDF\_Temp\"
If Len(Dir("C:\Users\" & LoginName & "\Desktop\_PDF\", vbDirectory)) = 0 Then
MkDir "C:\Users\" & LoginName & "\Desktop\_PDF\"
End If
If Len(Dir("C:\Users\" & LoginName & "\Desktop\_PDF\_Temp\", vbDirectory)) = 0 Then
MkDir "C:\Users\" & LoginName & "\Desktop\_PDF\_Temp\"
End If
For i = 3 To lr
myFile = Cells(i, 2).Value & ".xlsx"
On Error Resume Next
Workbooks.Open _
FileName:=myFolder & myFile, _
ReadOnly:=True
With ActiveSheet.PageSetup 'How to print only the .xlsx files from 3 to lr?
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=destFolder & ActiveSheet.Name, _ 'How to save as Workbook name?
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
ActiveSheet.Close
Next i
End Sub
FileSystemObject
orDIR
to check file type before attempting to open the file – Zacon error resume next
– Matt TaylorResume Next
statement! Not something I use often. You don't need that. Like I said, you can use something likeFSO
to check if file exists prior to opening it – ZacC:\Users\<username>
to be the home of the user docs. Useenviron("Userprofile")
instead. And personally, I don't like it to have anything stored on the desktop... – FunThomasenviron
tip. Desktop is a temporary location that the user will then print from and move the files to the final location inside of ProjectWise. – Matt Taylor