ドキュメントをマージするときに、結果を個別のファイルに分割し、それぞれのファイルにマージ フィールド「FileNumber」に基づく名前を付けたいと考えています。
私が現在持っているコードは次のとおりです。
Sub splitter()
' Based on a macro by Doug Robbins to save each letter created by a mailmerge as a separate file.
' With help from http://www.productivitytalk.com/forums/topic/3927-visual-basic-question-for-merge-fields/
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
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End = Letter.End - 1
For Each oField In Letter.Fields
If oField.Type = wdFieldMergeField Then
If InStr(oField.Code.Text, "FileNumber") > 0 Then
'get the result and store it the FileNum variable
FileNum = oField.Result
End If
End If
Next oField
Set Target = Documents.Add
Target.Range = Letter
Target.SaveAs FileName:="C:\Temp\Letter" & FileNum
Target.Close
Next i
End Sub
問題は、「新しいドキュメントにマージ」すると「FileNumber」フィールドが存在しないため、それを取得できないことですが、「結果のプレビュー」に移動してマクロを実行すると、現在プレビューされているレコードのみが保存され、残りの文字ではありません。
コードを次のように変更する必要があると想定しています
For i = 1 To Source.MergedRecord.Count
Set Letter = Source.MergedRecord(i).Range
しかし、正しい構文を理解できません。
http://www.gmayor.com/individual_merge_letters.htmは知っていますが、ダイアログ ボックスは必要ありません。ワンクリック ボタンが必要です。