简体   繁体   English

Word VBA中的嵌套IF字段

[英]Nested IF field in Word VBA

I'm trying to create a word addin that adds complex IF Statement Creation, from a list of possible mergefields. 我正在尝试从可能的mergefields列表中创建添加复杂的IF语句创建的单词插件。

Complex is 复杂的是

{ IF { = OR ( { COMPARE { MERGEFIELD Field_1 } <= "Value" }, { COMPARE { MERGEFIELD Field_2 } >= "Value" } ) } = 1 "True Instructions" "False Instructions" }

Im trying to do this all in VBA, but im having issues with my Complex if, as I cant get the "}" to end in the right locations. 我试图在VBA中完成所有操作,但是如果我无法在正确的位置结束“}”,则我的Complex会出现问题。

If I use the terminator "Selection.EndKey Unit:=wdLine" in any other location besides the end, it creates a mess and putts all the } at that line. 如果我在结尾以外的任何其他位置使用终止符“ Selection.EndKey Unit:= wdLine”,则会造成混乱,并将所有}推入该行。

Here is my Code: 这是我的代码:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="IF "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:=" = " & JointOperator1 & " ( "

'FIRST ARG
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:="COMPARE "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="MERGEFIELD " & FirstArg1

    Selection.TypeText Text:=" "
    Selection.TypeText Text:=ComparisonType1
    Selection.TypeText Text:=" "
    Selection.TypeText Text:=Chr(34) & SecondArg1 & Chr(34)


Selection.TypeText Text:=", "


'SECOND ARG
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:="COMPARE "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="MERGEFIELD " & FirstArg2

    Selection.TypeText Text:=" "
    Selection.TypeText Text:=ComparisonType2
    Selection.TypeText Text:=" "
    Selection.TypeText Text:=Chr(34) & SecondArg2 & Chr(34)


Selection.TypeText Text:=" ) "

Selection.TypeText Text:=" = 1 "

Selection.TypeText Text:=vbCrLf & " " & Chr(34)


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=strTempIfTrue


Selection.TypeText Text:=Chr(34) & " " & vbCrLf
Selection.TypeText Text:=" " & Chr(34)

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=strTempIfFalse


Selection.TypeText Text:=Chr(34)
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph

And this is what I get when generating the "Complex if" 这就是生成“如果...时复杂”的结果

{IF { = AND ( {COMPARE{MERGEFIELD FHB} = "T", { COMPARE {MERGEFIELD BLAH} = "F") = 1 "If True text" "If False Text"}}}}

But it should be this: 但是应该是这样的:

{IF { = AND ( {COMPARE{MERGEFIELD FHB} = "T" **}** , { COMPARE {MERGEFIELD BLAH} = "F" **}** ) **}**  = 1 "If True text" "If False Text"}

If someone could shed some light on this matter, where am I going wrong. 如果有人可以阐明这件事,那我错了。 Or If there is a way of forcing the location of the Ending }, that would be great I'm very new to VBA (I'm a C++ Programmer) 或者,如果有一种强制结束}位置的方法,那太好了,我是VBA的新手(我是C ++程序员)

So I found a solution to my problem. 因此,我找到了解决问题的方法。

I used the word macro recorder(View>Macros>Record Macro...) to record me entering the complex if statement, then viewed the macro, by pressing Alt + F11 and selecting macros, and I simply replaced some strings with my variables. 我使用了宏记录器一词(“查看”>“宏”>“记录宏...”)来记录我输入复杂的if语句,然后通过按Alt + F11并选择宏来查看宏,然后我只是用变量替换了一些字符串。 and that has fixed the issues. 这样就解决了问题。

Hopefully this will be helpful for someone else, saving them hours of head scratching. 希望这对其他人有帮助,从而节省了他们长时间的头部抓挠。

I had no Idea that you could record these actions to be reviewed later. 我不知道您可以记录这些操作以供以后检查。 You learn something new every day. 你每天学习新的东西。

This is what the recording looked like. 这就是录音的样子。

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="IF "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=" = AND ("
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=" COMPARE "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="MERGEFIELD FHB"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="T"
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.TypeText Text:=", "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="COMPARE "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="MERGEFIELD Other"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" <> """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="T"
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.TypeText Text:=")"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = 1 """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="IfTrue"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="IfFalse"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM