2013-02-01 4 views
1

MS Word 2010 사용 Mailmerge를 매크로와 함께 실행하여 각 레코드를 파일 이름으로 필드 중 하나를 사용하여 PDF 형식으로 개별 파일로 저장합니다. 이렇게하면 많은 시간을 절약 할 수 있습니다.MS Word에서 서식 손실 Wordmermer 매크로

내가 가진 문제는 형식이 완전히 손실되고 있다는 것입니다. 마치 텍스트를 복사하여 새 문서에 붙여 넣는 것뿐입니다. 형식이 없으면 무의미한 것처럼 형식을 보호 할 수있는 방법이 있습니까?

미리 감사드립니다.

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

저장 하시겠습니까?

이 샘플 코드는 메일 병합 문서의 각 편지 병합 항목을 반복하고 항목을 문자로 열어 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 년 오래된 Basic (예, Visual Basic이 아님)과 Fortran (펀치 카드는 아니지만 Fortan이지만 실제로는 닫습니다.)의 무언가로만 무장했습니다. Raduner 씨 매크로를 http://raduner.ch/blog/microsoft-word-mail-merge-into-single-documents에서 가져 왔고, Remou 매크로 코드는 위의 pdf를 생성하고, 몇몇 다른 사람 및 결합 된 다른 양상 및 프레스토 !!! 분명히 운이 좋았지 만 MS Word 2010에서 잘 작동합니다. 다른 모든 사람들에게도 도움이되기를 바랍니다. 개별 PDF 작성자와 개별 단어 파일 작성자를 모두로드하고 있습니다. Visual Basic을 알고있는 사람이이를 정리하고 다른 모든 사용자에게 더 친숙한 사용자가되기를 바랍니다.

개별적인 단어 파일 MACRO (엑셀 데이터 소스에서 "파일 이름"열이 있어야합니다 당신을주의) :

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