通过ThisWorkbook.Path将单元格范围保存为PDF

时间:2018-08-23 20:05:57

标签: excel vba excel-vba-mac

我有一个保存到已定义路径的宏,该路径无用,因为将有多个人使用该文件。

Sub SavetoPdf()
'
' SavetoPdf Macro
' To Save to PDF Details
'
' Keyboard Shortcut: Ctrl+s
'
    Range("E31:I54").Select
    Application.CutCopyMode = False
    ChDir "/Users/Me/Desktop/"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
        "/Users/Me/Desktop/" & Range("G41").Value & ".pdf", Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
End Sub

我想保存到ThisWorkbook.Path并修改上面的内容。

Sub SavetoPdf()
    ' Saves active sheet as PDF file.

    Dim Name As String

    Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"

    Range("E31:I54").Select
    Application.CutCopyMode = False
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
        Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

我在Mac上。

我明白了

  

打印错误

,调试器突出显示以下代码:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
  Quality:=xlQualityMinimum, _ 
  IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

2 个答案:

答案 0 :(得分:0)

您的标题显示您要PDF单元格的“范围”。那对你不起作用吗?

Sub SavetoPdf()
' Saves active sheet as PDF file.

    Dim Name As String

    Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"

    Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name, _
                                         Quality:=xlQualityMinimum, _
                                         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

答案 1 :(得分:0)

已修改的@Davesexel代码:已对模拟数据进行了测试,并且没有任何问题。

Dim rng As Range   
Dim Name As String

Set rng = Worksheets("Sheet1").Range("G41") 'Change worksheet to your needs

Name = rng.Value
Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=ThisWorkbook.Path & "/" & Name & ".pdf", _
    Quality:=xlQualityMinimum, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False