Share via

Error! Bookmark not defined. on nested IF Fields with Content Controls

Anonymous
2020-10-30T05:54:22+00:00

Hi,

I am using combination of Content Controls and IF fields in my document.

I am getting "Error!  Bookmark not defined." error when I have nested IF Fields with content controls.

 

The inner  and outer IF are both comparing the value of content control  to a string. The content controls are bound to custom xml.

Please help me understand what causes this error.

When I update the IF field I am getting **"Error!  Bookmark not defined."**with closing field "}":

Thank you,

Rainier

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2020-10-31T12:40:47+00:00

Rainier,

  Word never ceases to perplex.  I'm not smart enough to say definitively that it is a bug, but it seems that way.

  Regardless, if you put the CCs in a Quote field the error no longer occurs.

However, your code doesn't seem to return the desired result (not sure why you care). So changing it as follows does:

Private Sub Document_ContentControlBeforeDelete(ByVal OldContentControl As ContentControl, ByVal InUndoRedo As Boolean)

Dim isInFieldResult As Boolean

Dim oFld As Field

 MsgBox OldContentControl.Range.Information(wdInFieldResult) & " - which doesn't seem to be a true statement."

 For Each oFld In ActiveDocument.Fields

   If OldContentControl.Range.InRange(oFld.Result) Then

     isInFieldResult = True

     MsgBox isInFieldResult & " - which seem more accurate."

     Exit For

   End If

 Next oFld

 isInFieldResult = OldContentControl.Range.Information(wdInFieldResult)

lbl_Exit:

  Exit Sub

End Sub

An interesting side note.  Look what happens when the CCs are mapped and the node values changed (to the four possible values) then updating fields:

Private Sub Document_ContentControlBeforeDelete(ByVal OldContentControl As ContentControl, ByVal InUndoRedo As Boolean)

Dim isInFieldResult As Boolean

Dim oFld As Field

 For Each oFld In ActiveDocument.Fields

   If OldContentControl.Range.InRange(oFld.Result) Then

     isInFieldResult = True

     MsgBox isInFieldResult

     Exit For

   End If

 Next oFld

lbl_Exit:

  Exit Sub

End Sub

Sub ScratchMacro1()

'A basic Word macro coded by Greg Maxey

Dim oCC1 As ContentControl, oCC As ContentControl

  Set oCC = ActiveDocument.ContentControls(1)

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rainier Lugue"

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubject"

  ActiveDocument.Fields.Update

lbl_Exit:

  Exit Sub

End Sub

Sub ScratchMacro2()

'A basic Word macro coded by Greg Maxey

Dim oCC1 As ContentControl, oCC As ContentControl

  Set oCC = ActiveDocument.ContentControls(1)

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rainier LugueX"

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubject"

  ActiveDocument.Fields.Update

lbl_Exit:

  Exit Sub

End Sub

Sub ScratchMacro3()

'A basic Word macro coded by Greg Maxey

Dim oCC1 As ContentControl, oCC As ContentControl

  Set oCC = ActiveDocument.ContentControls(1)

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rainier Lugue"

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubjectX"

  ActiveDocument.Fields.Update

lbl_Exit:

  Exit Sub

End Sub

Sub ScratchMacro4()

'A basic Word macro coded by Greg Maxey

Dim oCC1 As ContentControl, oCC As ContentControl

  Set oCC = ActiveDocument.ContentControls(1)

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rainier Lugue"

  oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubjectX"

  ActiveDocument.Fields.Update

lbl_Exit:

  Exit Sub

End Sub

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-10-31T01:08:14+00:00

    Thanks Greg and Doug,

    I tried to replicate this without my add-in. It's only happening when my add-in is managing the document. I think the add-in is doing something that corrupts the document. I will look on the code to see what causes this.

    Thanks guys for taking the time to help me.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-10-30T20:11:28+00:00

    In testing here, I created the nested fields and mapped the CCs to a customXMLPart.  Running this code repeatedly, it seemed to update fine with no errors:

    Sub ScratchMacro()

    'A basic Word macro coded by Greg Maxey

    Dim oCC1 As ContentControl, oCC As ContentControl

      Set oCC = ActiveDocument.ContentControls(1)

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rain"

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubject"

      ActiveDocument.Fields.Update

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "RainX"

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubject"

      ActiveDocument.Fields.Update

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "Rain"

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubjectX"

      ActiveDocument.Fields.Update

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(1).Text = "RainX"

      oCC.XMLMapping.CustomXMLPart.DocumentElement.ChildNodes(2).Text = "MySubjectX"

      ActiveDocument.Fields.Update

    lbl_Exit:

      Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-10-30T08:30:50+00:00

    Hi Doug,

    Thank you for the reply. Yes I also get that result, but eventually after several update fields it will result to "Error! Bookmark no defined". Then the error will not go away unless I rewrite the nested IF fields. 

    We usually update the values of the content controls through VSTO add-in by replacing the custom xml where the content controls are bound, then we call document.fields.update() to update the IF field result.

    Thanks,

    Rainier

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2020-10-30T07:56:14+00:00

    With this

    I get

    Not a very satisfactory arrangement however as to alter the entries into the content controls, it is necessary to toggle on the display of the content controls.  It would probably be better to use the Document_ContentControlOnExit event to evaluate the data entered into the content controls and then set the value of a document variable so the the appropriate text would be displayed in a DOCVARIABLE field.

    Was this answer helpful?

    0 comments No comments