如何通过Word 2007以PDF格式打印单个文档(邮件合并)

时间:2013-01-08 18:51:11

标签: vba pdf printing ms-word mailmerge

我有一个有趣的问题,我找不到某个地方的答案。 我需要打印大约2000个字母,当然我会使用Mail Merge。问题:我需要以pdf格式打印出的每一个。我发现我必须在Word中使用VBA宏来获取单个字母(而不是只在一个文档中获取整个字母),这已经有效了。但我无法找到如何自动将它们转换为pdf。

有没有人有想法可以帮助我?我感谢您的帮助。

到目前为止我所做的事情(用邮件合并制作单个文件):

    Sub EinzelDatei() 

    Dim actpath As String, Dateiname As String 
    Dim fs As Object 
    Dim LetzterRec As Long 


    Const path As String = "D:\Test\" 

    On Error GoTo 0 

    Application.ScreenUpdating = False 
    Application.Visible = False 

    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord 
    LetzterRec = Word.ActiveDocument.MailMerge.DataSource.ActiveRecord 

    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord 

    With ActiveDocument.MailMerge 
        .DataSource.ActiveRecord = wdFirstRecord 
        Do 
            If .DataSource.ActiveRecord > 0 Then 
                .Destination = wdSendToNewDocument 
                .SuppressBlankLines = True 

               With .DataSource 

                    actpath = path & "\"          'Der aktuelle Pfad wird zusammengesetzt 

                    Set fs = CreateObject("Scripting.FileSystemObject") 
                    If fs.folderexists(actpath) = False Then MkDir (actpath) 'Wenn der Pfad         noch nicht existiert wird er jetzt erstellt 


                    .FirstRecord = .ActiveRecord 
                    .LastRecord = .ActiveRecord 

                Dateiname = actpath & _ 
                    .DataFields("No").Value & "-" & _ 
                    .DataFields("Surname").Value & "," & _ 
                    .DataFields("First_Name").Value & ".docx"                  'Dateiname =                 Name, Vorname.doc 
                End With 
                .Execute Pause:=False 

                ActiveDocument.SaveAs FileName:=Dateiname                   '
                ActiveDocument.Close False 
            End If 

        If .DataSource.ActiveRecord < LetzterRec Then 
             .DataSource.ActiveRecord = wdNextRecord 
          Else 
             Exit Do 
          End If 
       Loop 


    End With 


    MsgBox ("Erledigt") 
    Application.Visible = True 
    Application.ScreenUpdating = True 

    End Sub

提前谢谢!!!

3 个答案:

答案 0 :(得分:1)

使用

ActiveDocument.SaveAs FileName:= Dateiname,FileFormat:= wdsaveformat.wdFormatPDF

或者

ActiveDocument.SaveAs2 FileName:= Dateiname,FileFormat:= wdsaveformat.wdFormatPDF

但它只适用于Word 2007 SP2(我认为)及以后。

答案 1 :(得分:1)

在Word 2007中,导出功能可以将文件另存为pdf:

alter procedure NewUserTableCreation(@Username varchar(50))
as
    declare @CreateUserTable NVARCHAR(MAX)
    declare @AddRecord NVARCHAR(MAX)
    declare @VisitedClothesKids varchar(50)='VisitedClothesKids'
    declare @InitialCount varchar(20)='0'
BEGIN
    --Building query for creating a user table
    SET @CreateUserTable = 'create table ' + @Username +
                           '_Table(UserActivityData varchar(50),DataValue varchar(20))'
    EXEC(@CreateUserTable);

    --Adding Records in the user table
    SET @AddRecord = 'insert into ' + @Username + '_Table(UserActivityData, DataValue)
                      values(' + @VisitedClothesKids + ','  + @InitialCount + ')'
    EXEC(@AddRecord);
END
GO

答案 2 :(得分:0)

有点简单,但您可以将默认打印机设置为PDF创建者,然后使用打印功能。2010年,您完成合并后将获得打印或编辑选项。