【问题标题】:Save the active open document as pdf file将活动打开的文档另存为 pdf 文件
【发布时间】:2015-12-16 07:44:24
【问题描述】:

我有以下代码,可以从 Excel 中复制范围单元格的值,并将其作为图片粘贴到新的 Word 文档中。 我想将活动文档保存为 pdf 文件,文件名作为单元格“A2”中的值。 如果您能帮我在下面的代码中添加相同的内容,那将是一个很大的帮助。

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

【问题讨论】:

    标签: vba excel pdf ms-word


    【解决方案1】:

    试试这个,

    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
    

    不要使用word转PDF,而是使用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
    

    【讨论】:

    • 谢谢您.. 效果很好。我现在面临的唯一问题是 word doc 和 pdf 文件仍然打开。它也应该关闭。
    • OpenAfterExport:=True 更改为 false,则 pdf 将无法打开。 .Close saveChanges:=False 行应该关闭单词 doc。
    • 您不能将Crng.CopyPicture 放到不同的工作表中,然后将该工作表以 PDF 格式保存吗?
    • .Close saveChanges:=False 只关闭活动文档而不是整个word doc。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多