如何消除对话框:标题记录定界符

时间:2019-03-19 12:53:29

标签: vba ms-word word-vba

我想在Word中进行自动邮寄,可以在其中更改f.ex。 <<value1>>上的“ Ania”。因此,当我尝试连接到数据库时,它将显示一个消除对话框:标题记录分隔符。我们的项目要求这是完全自动化的,因此我不希望出现此对话框。如何自动消除它?

这是我的数据库:

"value1","value2";
"Anna2","Marek1";

这是我的VB:

Sub TestMacro()

ActiveDocument.MailMerge.OpenDataSource Name:= _
   "D:\Users\anna\Desktop\test2.txt", ConfirmConversions:=False, _
    ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
    WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
    Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
    wdMergeSubTypeOther
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    , Text:="""value1"""
End Sub

enter image description here

2 个答案:

答案 0 :(得分:0)

我记得它应该通过复制第一行并使用字段名称来工作:

"value1","value2"
"value1","value2"
"Anna2","Marek1"

请注意,对于Word,您还应该使用换行符作为记录分隔符,而不是分号或其他任何内容。

答案 1 :(得分:0)

您可以通过将数据源编辑为Word将自动识别的格式来完全避免此问题。试试:

Sub Demo()
Application.ScreenUpdating = False
Const StrSrc As String = "D:\Users\anna\Desktop\test2.txt"
Documents.Open StrSrc, False, False, False
With ActiveDocument
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = False
    .Text = ""","""
    .Replacement.Text = Chr(34) & vbTab & Chr(34)
    .Execute Replace:=wdReplaceAll
    .Text = ";^p"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  .Close True
End With
With ActiveDocument
  .MailMerge.OpenDataSource Name:=StrSrc, ConfirmConversions:=False, _
    ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
    WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
    Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
    wdMergeSubTypeOther
  .Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:="""value1"""
End With
Application.ScreenUpdating = True
End Sub