首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel宏邮件合并-导出到pdf

Excel宏邮件合并-导出到pdf
EN

Stack Overflow用户
提问于 2015-10-30 13:34:44
回答 2查看 3.9K关注 0票数 1

我正在使用vba宏,它工作得很好,但我需要将文档保存为.pdf。

我在找小费,但我不知道如何找到它们。上一次我找到了这个解决方案:vba邮件合并保存为pdf,但我不知道将它应用到宏中。

这是我的代码:

代码语言:javascript
复制
Sub RunMerge()

Dim wd As Object
Dim wdocSource As Object

Dim strWorkbookName As String

On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdocSource = wd.Documents.Open(ThisWorkbook.Path & "\" & "ArtSpecDatabase.docx")

strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

wdocSource.MailMerge.MainDocumentType = wdFormLetters

wdocSource.MailMerge.OpenDataSource _
        Name:=strWorkbookName, _
        AddToRecentFiles:=False, _
        Revert:=False, _
        Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
        SQLStatement:="SELECT * FROM `Sheet2$`"

With wdocSource.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    With .DataSource
        .FirstRecord = 1
        .LastRecord = 1
    End With
    .Execute Pause:=False
End With

Dim PathToSave As String
PathToSave = ThisWorkbook.Path & "\" & "pdf" & "\" & Sheets("Sheet2").Range("B2").Value2 & ".docx"
If Dir(PathToSave, 0) <> vbNullString Then
    wd.FileDialog(FileDialogType:=msoFileDialogSaveAs).Show
Else
    wd.activedocument.SaveAs2 PathToSave, wdFormatDocumentDefault

End If

wd.Visible = True
wdocSource.Close savechanges:=False
wd.activedocument.Close savechanges:=False

Set wdocSource = Nothing
Set wd = Nothing


End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-30 13:52:53

要将Word文档导出为PDF格式,需要使用ExportAsFixedFormat方法。例如,可以将SaveAs2调用替换为:

代码语言:javascript
复制
wd.ActiveDocument.ExportAsFixedFormat PathToSave, 17 'The constant for wdExportFormatPDF

现在,您对FileDialog的调用是没有意义的,所以我建议更改整个Dir(.)如果-判决如下:

代码语言:javascript
复制
Dim PathToSave As String
PathToSave = ThisWorkbook.Path & "\" & "pdf" & "\" & Sheets("Sheet2").Range("B2").Value2 & ".pdf"
If Dir(PathToSave, 0) <> vbNullString Then
    With wd.FileDialog(FileDialogType:=msoFileDialogSaveAs)
        If .Show = True Then
            PathToSave = .SelectedItems(1)
        End If
    End With
End If

wd.ActiveDocument.ExportAsFixedFormat PathToSave, 17 'The constant for wdExportFormatPDF

编辑:忘记包括".pdf“扩展。

票数 2
EN

Stack Overflow用户

发布于 2015-10-30 14:22:02

使用下面的代码将excel导出到pdf。

代码语言:javascript
复制
Sub tst1()

Dim fFilename     As String

    fFilename = "C:\Documents and Settings\test.xlsx"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
     fFilename & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False


End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33437299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档