簡體   English   中英

MS Word Mailmerge宏丟失格式

[英]Losing format with MS Word Mailmerge Macro

使用MS Word 2010,我希望Mailmerge與宏一起運行,使用字段之一作為文件名將每個記錄保存為單獨的PDF格式文件。 這將節省我很多時間。

我遇到的問題是該格式已完全丟失,就好像它只是復制文本並將其粘貼到新文檔中一樣。 我有什么辦法可以保護格式,因為沒有格式它是徒勞的...

提前致謝。

Sub splitter()

Dim i As Integer
Dim Source As Document
Dim Target As Document
Dim Letter As Range
Dim oField As Field
Dim FileNum As String

Set Source = ActiveDocument

ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord

For i = 1 To ActiveDocument.MailMerge.DataSource.ActiveRecord
    ActiveDocument.MailMerge.DataSource.ActiveRecord = i
    Set Letter = Source.Range
        For Each oField In Letter.Fields
        If oField.Type = wdFieldMergeField Then
            If InStr(oField.Code.Text, "INV_ID") > 0 Then
            FileNum = oField.Result
            End If
        End If
        Next oField
    Set Target = Documents.Add
    Target.Range = Letter
    Target.SaveAs2 "C:\BACS\INVOICING\INVOICES\Word Export\" & FileNum, 17
    Target.Close
    Next i
End Sub

如何使用保存?

此示例代碼循環遍歷mailmerge文檔中的每個mailmerge項,以字母形式打開該項,然后使用DataSource中的字段作為文件名將其保存為PDF。 沒有錯誤編碼,也沒有嘗試檢查重復的文件名。 這是一個片段。

Dim iRec As Integer
Dim docMail As Document
Dim docLetters As Document


Set docMail = ActiveDocument

''There is a problem with the recordcount property returning -1
''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx
docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
iRec = docMail.MailMerge.DataSource.ActiveRecord

docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord

For i = 1 To iRec
    With docMail.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = i
            .LastRecord = i
            '' This will be the file name
            '' the test data source had unique surnames
            '' in a field (column) called Surname
            sFName = .DataFields("Surname").Value
        End With
        .Execute Pause:=False
        Set docLetters = ActiveDocument
    End With
    docLetters.ExportAsFixedFormat OutputFileName:= _
        "Z:\docs\" & sFName & ".pdf", ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
    docLetters.Close False

    docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next

首先,讓我在應該歸功的地方給予功勞,因為我完全不知道編寫宏。 實際上,這是我第一次嘗試使用宏,更不用說修改代碼了。 我只具備24年的Basic基礎知識(是原始知識,不是Visual Basic)和Fortran(不是Fortan打孔卡,但真的很親密),我從http://raduner.ch/blog/microsoft-上帶了Raduner先生宏將單詞郵件合並到單個文檔中 ,上面用於生成pdf的Remou宏代碼,以及一些其他代碼,並結合了不同方面和PRESTO! 我顯然很幸運,但是它可以在MS Word 2010中使用。希望它也適用於其他所有人。 我正在加載單個pdf創建者和單個word文件創建者。 我希望知道Visual Basic的人能夠解決此問題,並使它對其他所有人更加用戶友好。

單個單詞文件宏(請注意,您在Excel數據源中必須具有“ FileName”列):

Sub SaveIndividualWordFiles()
Dim iRec As Integer
Dim docMail As Document
Dim docLetters As Document
Dim savePath As String

Set docMail = ActiveDocument
''There is a problem with the recordcount property returning -1
''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx

savePath = ActiveDocument.Path & "\"

docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
iRec = docMail.MailMerge.DataSource.ActiveRecord
docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord

For i = 1 To iRec
 With docMail.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    With .DataSource
        .FirstRecord = i
        .LastRecord = i
        '' This will be the file name
        '' the test data source had unique surnames
        '' in a field (column) called FileName
        sFName = .DataFields("FileName").Value
    End With
    .Execute Pause:=False
    Set docLetters = ActiveDocument
  End With

' Save generated document and close it after saving
        docLetters.SaveAs FileName:=savePath & sFName
        docLetters.Close False

  docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM