Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

vba - How to convert Excel to Adobe PDF

I am trying to find a code that works to convert my excel into PDF (Adobe). So far my research to find the perfect code doesn't work.

So here is how my company manually converts and save at their own file ===>

Click "File" -> Click "Print" -> (configure the Set Up to their own preferences like portrait or landscape etc) -> Click "Print" button -> Select "CutePDFWriter" -> (configure the Properties to their own preferences) -> Click "OK" button -> Click "Save" (to whichever file they want to save in)

Please do help me if you have any sort of knowledge in this. Thank you very very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Option Explicit
Sub SaveAsPDF()
    Dim FSO As Object
    Dim s(1) As String
    Dim FilePath As String

    Set FSO = CreateObject("Scripting.FileSystemObject")
    s(0) = ThisWorkbook.FullName

    If FSO.FileExists(s(0)) Then
        '// Change Excel Extension to PDF extension in FilePath
        s(1) = FSO.GetExtensionName(s(0))
        If s(1) <> "" Then
            s(1) = "." & s(1)
            FilePath = Replace(s(0), s(1), ".pdf")

            '// Export to PDF with new File Path
            ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=FilePath, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
        End If
    Else
        '// Error: file path not found
        MsgBox "Error: This workbook may be unsaved.  Please save and try again."
    End If

    Set FSO = Nothing
End Sub

To Export the workbook Try changing ActiveSheet To ActiveWorkbook

To Export only multiple sheets selections try using Sheets(Array("Sheet4", "Sheet5"))

Example:

        ThisWorkbook.Sheets(Array("Sheet2", "Sheet3")).Select
        Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=FilePath, _

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...