2017-09-01 992 views
0

我想做一個函數檢查.pdf文件是否存在。如果存在,我想打印文件。在Excel宏中使用VBA打印和現有的PDF

Function PrintPDF(PartNum As String) 
    Dim DirFile As String 
    DirFile = "\\SERVER5\hpfiles\Company\Drawings\PDF-SL8\" & PartNum & ".pdf" 
     If Dir(DirFile) = "" Then 
    Exit Function 
    Else 
    DirFile.PrintOut 

    End If 
    End Function 

我得到一個說錯誤無效的編譯錯誤。我認爲它是因爲DirFile是一個字符串。

如何使用此字符串作爲打印目標文件?

回答

0

正確,您無法打印字符串。一種方法是使用shell命令和打印這樣的文件:

Option Explicit 

Declare Function apiShellExecute 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 

Public Sub PrintFile(ByVal strPathAndFilename As String) 
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0) 
End Sub 

Function PrintPDF(PartNum As String) 
    Dim DirFile As String 
    DirFile = "\\SERVER5\hpfiles\Company\Drawings\PDF-SL8\" & PartNum & ".pdf" 

    If Dir(DirFile) = "" Then Exit Function 

    PrintFile (DirFile) 
End Function 

this post

+0

真棒改編。非常感謝你! – drewc2333

+0

太棒了!假設這是爲你做的,請標記爲已回答。乾杯 –