VBScript Syntax: MS Word Merge Field

我的未来我决定 提交于 2021-01-29 05:25:10

问题


I need a macro that places a conditional merge field with nested IF statements. Most users just copy the merge fields from one document and paste them into their final draft. However, there are numerous conditional merge fields to choose from. Using a command button that inputs the merge field instead of copying and pasting will illuminate user error and speed up this process. Any help is much appreciated.

  • A macro that places a multi-nested conditional IF statement into a document.
  • Below is the code I created. Where it reads, “truetext:=”True,” is where I need to insert another merge field.

Thank you for reading and for your interest!

Dim doc As Word.Document
Dim mName As String
Dim dField As Word.MailMergeDataField

Set doc = ActiveDocument

doc.MailMerge.Fields.Add Range:=Selection.Range, Name:="""Client_FirstName"""
    Selection.TypeText Text:=" "

doc.MailMerge.Fields.AddIf Range:=Selection.Range, _
mergefield:="""Client_MiddleName""", Comparison:=wdMergeIfIsNotBlank, _
truetext:="True", _
falsetext:="False"
Selection.TypeText Text:=" "
    
doc.MailMerge.Fields.Add Range:=Selection.Range, _
    Name:="""Client_LastName"""

doc.MailMerge.Fields.AddIf Range:=Selection.Range, _
mergefield:="""Client_NameSuffix""", Comparison:=wdMergeIfIsNotBlank, _
truetext:="""Client_NameSuffix""", _
falsetext:=""
    Selection.TypeText Text:=" "

来源:https://stackoverflow.com/questions/65877962/vbscript-syntax-ms-word-merge-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!