2011-08-17 53 views
2

背景: 我已經創建了一個Excel模板郵件合併字段到Word文檔並生成5個不同的字母,將出去一個客戶。字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 
相關問題