[英]Problem with VBA print to pdf error code in windows 10 environment
我有在Windows 7和其他Windows版本環境中正常工作的代碼,但是當某些用戶已升級到Windows 10(包括我自己)時
這是一個啟用宏的工作表已經工作了3年,據我所知,唯一的變化是Windows 10的“升級”!
這是一些似乎失敗的代碼:
'saveas function for pdf
ws.range("A1:K69").ExportAsFixedFormat Type:=xlTypePDf, _
filename:=path & fname, _
Quality:-xlqualityStandard, _
IncludeDocProperties:=True' _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
我得到一個運行時錯誤方法'對象'范圍'的ExportAsFixedFormat'失敗。 但是當早期Windows環境中的某個人運行代碼時,它運行得很完美,我會創建,保存和打開pdf,以便用戶插入其他文檔。
讓我精神振奮,我無法弄清楚為什么會失敗 - 而且偶爾也是如此。
我使用下面的方法將Excel工作表自動保存為PDF,應該嘗試將其保存到Excel文件位置並在選項卡名稱后面命名。
如果您首先在工作表上設置要在PDF中查看的范圍的打印區域,它應該工作:)
希望能幫助到你
Sub Export_Summary()
'
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'user can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.