我在下面有一個代碼,可以將當前作業表保存為 PDF 檔案。它使用輸入到范圍的資訊("Customer_Name")來生成PDF的部分檔案名。我遇到的一個問題是用戶使用檔案名中禁止的特殊字符(& " ? < > # { } % ~ / \ )。
'Save Copy of Order Form in PDF Format
Dim path As String
Dim fname As String
fname = Sheets("ORDER FORM").Range("Company_Name").Value & " - " & Format(Now(), "dd-mm-yyyy hhmmss")
path = Application.ActiveWorkbook.path
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path & "\" & fname & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Application.ScreenUpdating = True
'Completion MsgBox
MsgBox "Order has saved in PDF Format and stored in file:" & vbNewLine & path, vbInformation vbOKOnly, "File Saved"
On Error GoTo ErrorHandler
ErrorHandler:
MsgBox "Please check that the company name does not include any of the characters highlighted in the popup box", vbCritical
Resume
如果公司名稱中沒有使用特殊字符,則此代碼可以正常作業。
我要做的是創建一個錯誤處理程式,向用戶發出msgbox提示他們檢查公司名稱不包含任何特殊字符的問題。
我已經撰寫了上述錯誤處理程式,然后故意使用了其中一個特殊字符,但仍然得到除錯訊息。請問有人可以幫忙嗎?
uj5u.com熱心網友回復:
- 你
On Error GoTo ErrorHandler需要在你的 sub 的頂部,或者至少在錯誤發生之前。 - 我建議只進行類似于下面的子字串檢查而不是錯誤處理程式,因為錯誤處理程式會捕獲可能發生的任何型別的錯誤。
If InStr(1, path, "/", vbTextCompare) = 0 Then
MsgBox "Contains illegal characters"
End Sub
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448710.html
