2014-11-05 57 views
0

如何從郵件合併而不是由Microsoft Office中的郵件合併功能輸出的一個大文件創建個人文件?從郵件合併中導出單個文檔

我希望能夠保存每個字母不是作爲合併域之一的名字被創建,但我一直沒能找到到目前爲止,以直觀的方式...

+0

我加入了VBA代碼的問題,但請注意,如果你想做到這一點沒有字(例如,使用C#),你可以這樣做使用docx4j.NET – JasonPlutext 2014-11-17 23:18:58

+0

謝謝@JasonPlutext! – 2014-11-18 13:10:57

回答

1

爲我的經驗,沒有選擇保存單個文件,而是可以使用宏來吐出文件並將其單獨保存爲您想要的特定名稱。我已經嘗試過並且想要成功。希望下面的代碼可以幫助你實現你的目標。

Sub BreakOnSection() 
    'Used to set criteria for moving through the document by section. 
    Application.Browser.Target = wdBrowseSection 

    'A mailmerge document ends with a section break next page. 
    'Subtracting one from the section count stop error message. 
    For i = 1 To ((ActiveDocument.Sections.Count) - 1) 

     'Select and copy the section text to the clipboard 
     ActiveDocument.Bookmarks("\Section").Range.Copy 

     'Create a new document to paste text from clipboard. 
     Documents.Add 
     'To save your document with the original formatting' 
     Selection.PasteAndFormat (wdFormatOriginalFormatting) 

     'Removes the break that is copied at the end of the section, if any. 
     Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend 
     Selection.Delete Unit:=wdCharacter, Count:=1 

     ChangeFileOpenDirectory "C:\" 
     DocNum = DocNum + 1 
     ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc" 
     ActiveDocument.Close 
     'Move the selection to the next section in the document 
     Application.Browser.Next 
    Next i 
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges 
End Sub 

請恢復我的任何澄清。

+0

我在第一次處理這個問題時嘗試了這個腳本,但是我一直得到一個運行時錯誤,例如「6078 - 找不到範圍」。這可能與大學設置的方式有關(我必須做一些技巧才能讓他們首先使用宏)。我試圖爲我們的辦公室管理員設置這一點,所以我想盡可能簡化,這可能是不可能的。 – 2014-11-18 13:16:26

+0

你在哪裏指出每份文件的結尾。換句話說,你在哪裏告訴宏停止,並且當我保存爲PDF時,它不會打開。謝謝 – John 2017-12-01 15:28:26

3

最近我遇到過類似的情況,我想以pdf格式保存單個文件,而不是保存郵件合併功能創建的一個大文件。我已經寫下這個小函數來保存PDF格式的單個文件。

Sub SaveAsPDF() 
Dim CouncilName As String 
    With ActiveDocument.MailMerge 
     .Destination = wdSendToNewDocument 
     .SuppressBlankLines = True 
     For SectionCount = 1 To .DataSource.RecordCount 
      With .DataSource 
       'FirstRecord and LastRecords defines how many data records needs to be merge in one document. 
       'createing pdf file for each data record so in this case they are both pointing to ActiveRecord. 
       .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord 
       .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord 

       'get the council name from data source 
       CouncilName = .DataFields("Council").Value 

       'move to next datasource record. 
       If .ActiveRecord <> .RecordCount Then 
        .ActiveRecord = wdNextRecord 
       End If 
      End With 

      'Get path and file name 
      PDFPathAndName = ActiveDocument.Path & Application.PathSeparator & "FINAL - " & CouncilName & ".pdf" 

      ' Merge the document 
      .Execute Pause:=False 

      ' Save resulting document. 
      Set PDFFile = ActiveDocument 
      PDFFile.ExportAsFixedFormat PDFPathAndName, wdExportFormatPDF 
      PDFFile.Close 0 
     Next 
    End With 
End Sub 
+0

我會試試這個,但它會保存文檔中的安全功能,即可編輯字段嗎? – 2014-11-18 13:18:08

0

試試這個 - 它爲我工作 - 出口的單詞和pdf

http://www.gmayor.com/individual_merge_letters.htm

+1

一個潛在解決方案的鏈接總是受歡迎的,但請[在鏈接附近添加上下文](http://meta.stackoverflow.com/a/8259/169503),以便您的同行用戶可以瞭解它是什麼以及爲什麼在那。如果目標網站無法訪問或永久離線,請始終引用重要鏈接中最相關的部分。考慮到*僅僅是一個外部網站的鏈接*是一個可能的原因[爲什麼和如何刪除一些答案?](http://stackoverflow.com/help/deleted-answers)。 – zuazo 2016-12-14 02:48:25

0

我修改Parth的答案,因爲它沒有爲我工作。

Sub SaveAsFileName() 
Dim FileName As String 
With ActiveDocument.MailMerge 
    .Destination = wdSendToNewDocument 
    .SuppressBlankLines = True 

    For SectionCount = 1 To .DataSource.RecordCount 
     With .DataSource 
      ActiveDocument.MailMerge.DataSource.ActiveRecord = SectionCount 
      ActiveDocument.MailMerge.DataSource.FirstRecord = SectionCount 
      ActiveDocument.MailMerge.DataSource.LastRecord = SectionCount 

      ' replace Filename with the column heading that you want to use - can't have certain symbols in the name 
      FileName = .DataFields("Filename").Value 
     End With 

     'Get path and file name 
     FullPathAndName = ActiveDocument.Path & Application.PathSeparator & FileName & ".docx" 

     ' Merge the document 
     .Execute Pause:=False 

     ' Save resulting document. 
     ActiveDocument.SaveAs (FullPathAndName) 
     ActiveDocument.Close False 
    Next 
End With 
End Sub