簡體   English   中英

Word VBA中的嵌套IF字段

[英]Nested IF field in Word VBA

我正在嘗試從可能的mergefields列表中創建添加復雜的IF語句創建的單詞插件。

復雜的是

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

我試圖在VBA中完成所有操作,但是如果我無法在正確的位置結束“}”,則我的Complex會出現問題。

如果我在結尾以外的任何其他位置使用終止符“ Selection.EndKey Unit:= wdLine”,則會造成混亂,並將所有}推入該行。

這是我的代碼:

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

這就是生成“如果...時復雜”的結果

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

但是應該是這樣的:

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

如果有人可以闡明這件事,那我錯了。 或者,如果有一種強制結束}位置的方法,那太好了,我是VBA的新手(我是C ++程序員)

因此,我找到了解決問題的方法。

我使用了宏記錄器一詞(“查看”>“宏”>“記錄宏...”)來記錄我輸入復雜的if語句,然后通過按Alt + F11並選擇宏來查看宏,然后我只是用變量替換了一些字符串。 這樣就解決了問題。

希望這對其他人有幫助,從而節省了他們長時間的頭部抓撓。

我不知道您可以記錄這些操作以供以后檢查。 你每天學習新的東西。

這就是錄音的樣子。

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