2013-02-01 86 views
1

使用MS Word 2010我想要一個Mailmerge與一個宏一起運行,將每個記錄以PDF格式保存爲一個單獨的文件,並使用其中一個字段作爲文件名。這將節省我的時間。使用MS Word Mailmerge Macro丟失格式

我得到的問題是格式完全丟失,好像它只是複製文本並將其粘貼到新文檔中。有沒有什麼辦法可以保護格式化,因爲沒有它,這是非常無果的...

在此先感謝。

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 

回答

0

如何使用保存?

此示例代碼循環遍歷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 
+0

哇哇哇。你絕對是超級明星!格拉西亞斯 – user2032006

0

首先,讓我在信用到期的地方給出信用,因爲我完全不知道寫入宏。事實上,這是我第一次嘗試使用宏,更不用說修改代碼了。武裝只有24年的基本知識(是原始的,不是Visual Basic)和Fortran(沒有不打孔卡Fortan,但非常接近),我從http://raduner.ch/blog/microsoft-word-mail-merge-into-single-documents拿到Raduner先生的宏,Remou生成pdf的上面的宏代碼,以及幾個其他人,並結合不同的方面和PRESTO!我顯然很幸運,但它在MS Word 2010中工作。希望它也適用於其他人。我正在加載個人pdf創建者和個人文件創建者。我希望知道Visual Basic的人能夠清理這個問題,並且使其他人都更加友好。

單詞FILE MACRO(注意,您必須有一個「文件名」列在Excel數據源):

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