使用excel VBA代码将Word文档转换为PDF

时间:2016-04-18 19:45:51

标签: excel excel-vba word-vba vba

我写下面的代码来截取Excel文档的截图并粘贴到word文档。它工作正常但是我无法将此Word文档转换为PDF并且错误显示“对象不支持此属性或方法”。似乎我将Variable(Objword as variant)定义为不正确。请任何人都可以提供帮助。

Sub CopyToWordPicture()

Dim WDApp As Word.Application
Dim WDDoc As Word.Document

Dim objword As variant


Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open "C:\Automation\BH Report\Daily&BH RAN KPI report_ok.docx"
Workbooks.Open Filename:="C:\Automation\BH Report\Daily_Hourly KPI Template.xlsx"
Sheets("BH_Graphs").Select
Range("A1:k50").CopyPicture xlPrinter
    With objword
        '.Documents.Add
        .Selection.Paste
        .Visible = True

    End With

    'WDApp.Visible = True
   ' WDApp.Selection.GoToNext wdGoToPage

    Windows("Daily_Hourly KPI Template.xlsx").Activate
    Sheets("Daily_Graphs").Select
    Range("A1:J50").CopyPicture xlPrinter

    With objword
        '.Documents.Add
        .Selection.Paste
        .Visible = True
    End With


    'export as PDF

  objword.ExportAsFixedFormat OutputFileName:="C:\Automation\BH Report\Daily&BH RAN KPI report.pdf", _
     ExportFormat:=wdExportFormatPDF


    end sub

0 个答案:

没有答案