M
Michael Stauffer
I am trying to find a way to use Nested If expressions within a field code to
limit a document property that displays the document name from appearing only
on the last page of the last section of a document. The first If expression
cehcks the current page number against the n umber of pages within the
current section of the document. The second If expression checks the current
section number against the number of sections in the document. If both If
expressions are true, then the fild is supposed to display the document name,
otherwise it should display nothing.
I am able to get the result to come out correctly using the code shown below
to generate the field code. I had to create a custom document property named
SectionsCount in order to get the number of sections in the docuemnt to
display within the field:
Private Sub BuildIndentifierField()
On Error GoTo Err_Handler
With Selection
' Begin the First IF expression:
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages
.TypeText Text:=" "
' If the First IF expression is TRUE then use the Second (NESTED) IF
expression:
.TypeText Text:=Chr(34)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""SectionsCount"" "
.TypeText Text:=" "
' Add the result to display if the Second IF expression is TRUE:
.TypeText Text:=Chr(34)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""Identifier1"" "
.TypeText Text:=Chr(34)
' Add the result to display if the Second IF expression is FALSE:
.TypeText Text:=" " & Chr(34) & Chr(34)
.MoveRight Unit:=wdCharacter, Count:=2
.TypeText Text:=Chr(34)
' Add the result to display if the First IF expression is FALSE:
.TypeText Text:=" " & Chr(34) & Chr(34)
End With
Exit Sub
Err_Handler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error"
ActiveWindow.View.ShowFieldCodes = False
application.ScreenUpdating = True
I always get a message saying "Error! Missing test condition." within the
nested If expression of the field. For example, I get this to display
whenever the document is saved: { IF 1 = 6 "Error! Missing test condition."
"" }
If were to toggle the field code over the error message, I can see that my
second If expression is being read correctly by the code, as shown here: { IF
1 = 6 " { IF 1=2 "{ DOCPROPERTY "Identifier1" \* MERGEFORMAT }" "" }" "" }.
Thanks.
limit a document property that displays the document name from appearing only
on the last page of the last section of a document. The first If expression
cehcks the current page number against the n umber of pages within the
current section of the document. The second If expression checks the current
section number against the number of sections in the document. If both If
expressions are true, then the fild is supposed to display the document name,
otherwise it should display nothing.
I am able to get the result to come out correctly using the code shown below
to generate the field code. I had to create a custom document property named
SectionsCount in order to get the number of sections in the docuemnt to
display within the field:
Private Sub BuildIndentifierField()
On Error GoTo Err_Handler
With Selection
' Begin the First IF expression:
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages
.TypeText Text:=" "
' If the First IF expression is TRUE then use the Second (NESTED) IF
expression:
.TypeText Text:=Chr(34)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""SectionsCount"" "
.TypeText Text:=" "
' Add the result to display if the Second IF expression is TRUE:
.TypeText Text:=Chr(34)
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="DOCPROPERTY ""Identifier1"" "
.TypeText Text:=Chr(34)
' Add the result to display if the Second IF expression is FALSE:
.TypeText Text:=" " & Chr(34) & Chr(34)
.MoveRight Unit:=wdCharacter, Count:=2
.TypeText Text:=Chr(34)
' Add the result to display if the First IF expression is FALSE:
.TypeText Text:=" " & Chr(34) & Chr(34)
End With
Exit Sub
Err_Handler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Error"
ActiveWindow.View.ShowFieldCodes = False
application.ScreenUpdating = True
I always get a message saying "Error! Missing test condition." within the
nested If expression of the field. For example, I get this to display
whenever the document is saved: { IF 1 = 6 "Error! Missing test condition."
"" }
If were to toggle the field code over the error message, I can see that my
second If expression is being read correctly by the code, as shown here: { IF
1 = 6 " { IF 1=2 "{ DOCPROPERTY "Identifier1" \* MERGEFORMAT }" "" }" "" }.
Thanks.