2015-09-30 3 views
0

J'essaie de créer un mot addin qui ajoute une création d'instruction IF complexe, à partir d'une liste de champs de fusion possibles.Champ IF imbriqué dans Word VBA

Complex est

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

Im essayant de faire tout cela en VBA, mais im avoir des problèmes avec mon complexe si, comme je ne peux pas obtenir le « } » pour mettre fin aux bons endroits. Si j'utilise la terminaison "Selection.EndKey Unit: = wdLine" dans tout autre endroit en dehors de la fin, cela crée un désordre et met tout le} sur cette ligne.

Voici mon 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 

Et ce que je reçois lors de la génération du "complexe si"

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

Mais il devrait être ceci:

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

Si quelqu'un pouvait faire la lumière sur cette question, où vais-je me tromper? Ou S'il y a un moyen de forcer l'emplacement de la fin}, ce serait une grande Je suis très nouveau pour VBA (je suis un programmeur C++)

Répondre

2

I Alors trouvé une solution à mon problème. J'ai utilisé le mot enregistreur de macro (Affichage> Macros> Enregistrer la macro ...) pour m'inscrire en entrant l'instruction complexe if, puis vu la macro, en appuyant sur Alt + F11 et en sélectionnant les macros, et j'ai simplement remplacé certaines chaînes avec mes variables. et qui a résolu les problèmes.

Espérons que cela sera utile pour quelqu'un d'autre, en leur épargnant des heures de grattage de la tête.

Je n'avais aucune idée que vous pourriez enregistrer ces actions à revoir plus tard. Vous apprenez quelque chose de nouveau chaque jour.

Voici à quoi ressemblait l'enregistrement.

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"