从邮件生成txt文件与Title合并

时间:2014-01-20 17:10:32

标签: vba word-vba

我正在尝试通过邮件合并并为每个条目生成一个.txt。 .txt文件应该具有与邮件合并中的某个字段对应的标题(尝试使用ActiveDocument.MailMerge.Fields(1))获取它。目前我的脚本不运行但不会产生错误。

如果有人有建议,我们将不胜感激。感谢。

当前代码:

Option Explicit   

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
    Call ActiveDocument.SaveAs(Doc.Path & "\" & ActiveDocument.MailMerge.Fields(1) & ".txt", wdFormatText)
    ActiveDocument.Close False
  Next x

  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Call ActiveDocument.SaveAs(Doc.Path & "\" & ActiveDocument.Fields(1).Result & ".txt", wdFormatText)

Call ActiveDocument.SaveAs(Doc.Path & "\" & ActiveDocument.Sections(x).Fields(1).Result & ".txt", wdFormatText)

或/并根据需要更改字段数。