Using Nested If expressions in field codes

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.
 
J

Jezebel

Since the last page of the last section of the document is necessarily the
last page of the document, why not just use

{ IF {PAGE} = {NUMPAGES} ... }

?
 
P

Peter Jamieson

If you actually have a comparison 1=2 with no spaces around the "=" then I
would expect an error. Although your code always inserts " = ", not "=" I
don't think it is that, but worth checking. I can't see another problem
right now, but it may be worth trying an alternative set of field codes,
e.g. the following (which should work as long as Identifier1 does not
contain stuff such as single quotes):

{ ={ SECTION }-{ DOCPROPERTY "SectionsCount" }+{ PAGE }-{ SECTIONPAGES }
\#";;'{ DOCPROPERTY Identifier }'" }

BTW,
a. there are circumstances in which this code won't work, e.g. you have
continuous numbering, or if your fields are in a footer, you have a
continuous section break on the last page and you can't adjust the value of
SectionsCount.
b. I suggest you add PreserveFormatting:=False to all your field insertion
calls? It makes the resulting field much easier to read.
 
M

Michael Stauffer

I had tried this in the beginning. The reason that I cannot use { IF {PAGE}
= {NUMPAGES} ... } is because the page numbering is supposed to restart in
each new section, therefor I would never have a page number that equals that
valuer of the {NUMPAGES} field and the if expression would never be true.

Thanks.
 
M

Michael Stauffer

Peter,

Thanks for the response.

I have checked my fields and there are spaces around the = in each of the
expressions, so that may not be the cause of the error that I am getting.

I have also added ", PreserveFormatting:=False" to the end of each field
insertion call and I will try substituting the code as you had suggested.

Thanks again.
 
G

Greg

Michael,

I haven't really thought about what you are trying to do, but I think
your problem is that you didn't have field codes displayed as you
attempted to write your nested fields into the footer.

Try:
Private Sub BuildIndentifierField()
ActiveDocument.CustomDocumentProperties("SectionsCount").Value =
ActiveDocument.Sections.Count
ActiveWindow.View.ShowFieldCodes = True
With Selection
'Begin the First IF expression:
.Fields.Add Selection.Range, wdFieldEmpty, "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 Selection.Range, wdFieldEmpty, Text:="IF ",
PreserveFormatting:=False
.MoveRight Unit:=wdCharacter, Count:=5
.Fields.Add Range:=Selection.Range, Type:=wdFieldSection
.TypeText Text:=" = "
.Fields.Add Selection.Range, wdFieldEmpty, "DOCPROPERTY
""SectionsCount"" "
.TypeText Text:=" "
'Add the result to display if the Second IF expression is TRUE:
.TypeText Text:=Chr(34)
.Fields.Add 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
ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Fields.Update
Exit Sub
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top