Save the active open document as pdf file

↘锁芯ラ 提交于 2020-01-03 00:54:12

问题


I have the below code that copy range cell values from excel and paste as picture in a new word document. I want to save the active document as pdf file with name of file as value in cell "A2". If you can help me in adding the same in the below code it would be a great help.

Sub Picture()
Dim objWord, objDoc As Object
ActiveWindow.View = xlNormalView
Range("A2:K25").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Selection.Paste
objWord.Selection.TypeParagraph

End Sub

回答1:


Try this,

Sub SaveAsPDF()
    Dim objWord, objDoc As Object
    Dim A2 As String
    Dim Crng As Range

    A2 = Range("A2")
    Set Crng = Range("A2:K25")

    Crng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    objWord.Selection.Paste
    objWord.Selection.TypeParagraph

    With objDoc
        .ExportAsFixedFormat OutputFileName:= _
                             "C:\Users\Dave\Downloads\" & A2 & ".pdf", ExportFormat:=17, _
                             OpenAfterExport:=True, OptimizeFor:=0, Range:= _
                             0, From:=1, To:=1, Item:=0, _
                             IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
                             0, DocStructureTags:=True, BitmapMissingFonts:= _
                             True, UseISO19005_1:=False
        .Close saveChanges:=False
    End With
    objWord.Quit
    Set objWord = Nothing

End Sub

Instead of using word to PDF use excel

Sub SaveAsPDFxlStyle()
    Dim objWord, objDoc As Object
    Dim A2 As String

    A2 = Range("A2")
    ActiveSheet.PageSetup.PrintArea = "$A$2:$K$25"

    With ActiveSheet.PageSetup
        .PrintGridlines = True
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                    "C:\Users\Dave\Downloads\" & A2 & ".pdf", Quality:= _
                                    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=0

End Sub


来源:https://stackoverflow.com/questions/32664207/save-the-active-open-document-as-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!