2011-08-17 4 views
2

배경 : 편지를 Word 문서로 병합하고 하나의 고객에게 나가는 5 가지 다른 문자를 생성하는 Excel 템플릿을 만들었습니다.Word VBA를 병합 필드가있는 파일 이름 및 프롬프트로 병합

임무 : Word VBA 코드를 사용하려면 자동 편지 병합을 실행하고 편지 병합 필드에서 파생 된 파일 이름을 사용하여 특정 디렉터리에 저장 (또는 자동 저장)하라는 메시지를 표시합니다.

즉. 날짜 는 두 번째 문자 + 날짜 의 첫 글자 폴더

(고유 식별자) + 이름으로 저장되는 + 첫 글자의

(고유 식별자) + 이름

두 번째 편지 폴더에 저장하기

문제 : 디렉터리를 지정하는 방법이나 파일 이름의 일부로 편지 병합 필드를 삽입하는 방법을 알 수 없습니다.

다음

내가 다음 코드 디렉토리를 선택합니다

Sub MailMerge() 

With ActiveDocument.MailMerge 
    .Destination = wdSendToNewDocument 
    .SuppressBlankLines = True 

    With .DataSource 
     .FirstRecord = wdDefaultFirstRecord 
     .LastRecord = wdDefaultLastRecord 
    End With 

    .Execute Pause:=False 
End With 

With Dialogs(wdDialogFileSummaryInfo) 
    .Title = "Letter1Draft" & Format(Now(), "mmddyyyy") & ".doc" 
    .Execute 
End With 

' Then this! 
With Dialogs(wdDialogFileSaveAs) 
    .Show 
End With 

End Sub 

답변

0

이 그 코드입니다. 편지 병합 필드를 파일 이름으로 삽입 할 수 없습니다.

Sub AllSectionsToSubDoc() 

    Dim x    As Long 
    Dim Sections  As Long 
    Dim Doc    As Document 

    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    Set Doc = ActiveDocument 
    Sections = Doc.Sections.Count 
    For x = Sections - 1 To 1 Step -1 
     Doc.Sections(x).Range.Copy 
     Documents.Add 
     ActiveDocument.Range.Paste 
     ActiveDocument.SaveAs (Doc.Path & "\" & x & ".pdf") 
     ActiveDocument.Close False 
    Next x 

    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 

End Sub