Windows 10环境中VBA打印到pdf错误代码的问题

时间:2019-06-25 13:03:59

标签: excel vba

我有在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

我得到对象“ Range”的运行时错误方法“ ExportAsFixedFormat”失败。但是,当在较早的Windows环境中运行某人的代码运行良好时,我便创建,保存并打开了pdf文件,供用户插入其他文档。

让我精神错乱,我无法弄清楚为什么这样做会失败-偶尔也会出现。

1 个答案:

答案 0 :(得分:0)

我使用下面的方法将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