簡體   English   中英

在 ThisWorkbook.Path 中將單元格范圍保存為 PDF

[英]Save range of cells to PDF at ThisWorkbook.Path

我有一個保存到定義路徑的宏,該路徑沒有用,因為多人將使用該文件。

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

您的標題顯示您想要 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

修改@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM