Surely this is a Bug

  • Follow


Microsoft Word developer's have proven once again their determination to 
make the goal of a simple and reliable means of validating content control 
contents seemingly impossible.

The OnExit event has been buggy since the release of Word and MS simply 
doesn't seem to care.

For the last several days I have been getting closer and closer to finding a 
reliable (far from simple) means.  At first I thought that the CustomXMLPart 
NodeAfterReplace event would fit the bill.  Unfortunately when a mapped CC 
starts out life with the placeholder text displayed then the corresponding 
node contains a null value.  When content is added to the CC this 
replacement of "null" with "something" is not detected by the event.  Only 
replacing "something" with "something else" triggers the event.  (bug or by 
design I am not certain?)

Next I experimented with NodeAfterInert.  Sure enough when I replace CC 
placeholder text with something else and tab to the next CC this event 
fires.  Almost ready to proclaim EUREKA, I encountered more bugging 
behavior!!

When the event first is inconsistent!!  To work as a CC validation event it 
must not fire until "after" the user enters the data an exists the CC. 
Unfortunately sometimes if fires after the data is entered and the user 
leaves the CC, but other times it fires immediately after the user enters 
the first character.  Thsi has to be a bug!! Why on earth should it fire on 
first character entry and the bigger issue is however it fires it should 
fire consistently.

You can observe this behavior using the following steps:

1.  Open a new Word Document.
2.  Paste the following code in the "ThisDocument" class module:

Option Explicit
Dim WithEvents oMonitor As CustomXMLPart
Sub SetMonitor()
On Error Resume Next
Set oMonitor = ThisDocument.CustomXMLParts(4)
End Sub
Private Sub oMonitor_NodeAfterInsert(ByVal NewNode As Office.CustomXMLNode, 
ByVal InUndoRedo As Boolean)
Main.CCValidaton NewNode
End Sub
Private Sub oMonitor_NodeAfterReplace(ByVal OldNode As Office.CustomXMLNode, 
ByVal NewNode As Office.CustomXMLNode, ByVal InUndoRedo As Boolean)
Main.CCValidaton NewNode
End Sub

3.  Paste the following code in a standard module and run AddCCsAndMap. 
This will add four mapped CCs to the document.

Option Explicit
Sub AddCCsAndMap()
Dim oCC As ContentControl
With ActiveDocument
  .Range.InsertAfter "Test 1: "
  .Bookmarks("\endofdoc").Select
  Set oCC = .ContentControls.Add(wdContentControlText)
  oCC.Title = "Test 1"
  .Range.InsertAfter vbCr + vbCr & "Test 2: "
  .Bookmarks("\endofdoc").Select
  Set oCC = .ContentControls.Add(wdContentControlText)
  oCC.Title = "Test 2"
 .Range.InsertAfter vbCr + vbCr & "Test 3: "
  .Bookmarks("\endofdoc").Select
  Set oCC = .ContentControls.Add(wdContentControlText)
  oCC.Title = "Test 3"
  .Range.InsertAfter vbCr + vbCr & "Test 4: "
  .Bookmarks("\endofdoc").Select
  Set oCC = .ContentControls.Add(wdContentControlText)
  oCC.Title = "Test 4"
End With
MapCCs
ThisDocument.SetMonitor
End Sub

Sub MapCCs()
Dim oCC As ContentControl
Dim pXML As String
Dim oCustXMLPart As CustomXMLPart
Dim XPath As String
pXML = "<?xml version='1.0' 
encoding='utf-8'?><Main><Test_1></Test_1><Test_2></Test_2><Test_3></Test_3>" 
_
       & "<Test_4></Test_4></Main>"
ClearXMLParts
Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add(pXML)
Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 1").Item(1)
XPath = "/Main/Test_1[1]"
oCC.XMLMapping.SetMapping XPath
Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 2").Item(1)
XPath = "/Main/Test_2[1]"
oCC.XMLMapping.SetMapping XPath
Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 3").Item(1)
XPath = "/Main/Test_3[1]"
oCC.XMLMapping.SetMapping XPath
Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 4").Item(1)
XPath = "/Main/Test_4[1]"
oCC.XMLMapping.SetMapping XPath
Set oCC = Nothing
Set oCustXMLPart = Nothing
End Sub
Sub ClearXMLParts()
Dim i As Long
For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1
  ActiveDocument.CustomXMLParts(i).Delete
Next i
End Sub

Sub CCValidaton(oNode As Office.CustomXMLNode)
Dim pStr As String
pStr = oNode.Text
With ThisDocument
  Select Case oNode.ParentNode.BaseName
    Case "Test_1"
      If Len(pStr) < 5 Then
        MsgBox "Invalid entry"
        .SelectContentControlsByTitle("Test 
1").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
      Else
        .SelectContentControlsByTitle("Test 
1").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
      End If
    Case "Test_2"
      If Len(pStr) < 5 Then
        MsgBox "Invalid entry"
        .SelectContentControlsByTitle("Test 
2").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
      Else
        .SelectContentControlsByTitle("Test 
2").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
      End If
    Case "Test_3"
      If Len(pStr) < 5 Then
        MsgBox "Invalid entry"
        .SelectContentControlsByTitle("Test 
3").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
      Else
        .SelectContentControlsByTitle("Test 
3").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
      End If
    Case "Test_4"
      If Len(pStr) < 5 Then
        MsgBox "Invalid entry"
        .SelectContentControlsByTitle("Test 
4").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
      Else
        .SelectContentControlsByTitle("Test 
4").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
      End If
  End Select
End With
End Sub

4.  Click in the Test 1 CC and enter 1 to 4 characters then tab.  This will 
show you the expected behavior.

5.  The focus is now in Test 2 CC.  Type a single character.  This will show 
you the unexpected behavior and the bug!

6.  The focus is now in Test 3 CC.  This one behaves as expected.  Test 4 is 
unexpected.

For whatever reason every other CC behaves unexpectantly.  If you start with 
Test 2 then is is OK but Test 3 then fails.  Start with 4 OK but Test 1 
fails.

I have repeated this behavior consistently over 50 times.

Appreciate any response confirming this behavior and earnestly hoping that 
fixed in Word2010.

-- 
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)



0
Reply Greg 11/12/2009 4:58:00 PM

This gets even weirder.

If you add a CC between each of the four mapped CCs then cycle through the 
CCs each performs as expected (i.e., the event doesn't fire in the second 
and fourth control until after the user completes the text entry and exits 
the CC.  This the odd behavior also does occur if you enter the text in 
CC1>tab to CC2>click out of CC2>click back into CC2.

It seems that the event is somehow hanging and doesn't reset until you cycle 
in and out of the CC.  Weird.

"Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message 
news:eRAJJl7YKHA.1596@TK2MSFTNGP06.phx.gbl...
> Microsoft Word developer's have proven once again their determination to 
> make the goal of a simple and reliable means of validating content control 
> contents seemingly impossible.
>
> The OnExit event has been buggy since the release of Word and MS simply 
> doesn't seem to care.
>
> For the last several days I have been getting closer and closer to finding 
> a reliable (far from simple) means.  At first I thought that the 
> CustomXMLPart NodeAfterReplace event would fit the bill.  Unfortunately 
> when a mapped CC starts out life with the placeholder text displayed then 
> the corresponding node contains a null value.  When content is added to 
> the CC this replacement of "null" with "something" is not detected by the 
> event.  Only replacing "something" with "something else" triggers the 
> event.  (bug or by design I am not certain?)
>
> Next I experimented with NodeAfterInert.  Sure enough when I replace CC 
> placeholder text with something else and tab to the next CC this event 
> fires.  Almost ready to proclaim EUREKA, I encountered more bugging 
> behavior!!
>
> When the event first is inconsistent!!  To work as a CC validation event 
> it must not fire until "after" the user enters the data an exists the CC. 
> Unfortunately sometimes if fires after the data is entered and the user 
> leaves the CC, but other times it fires immediately after the user enters 
> the first character.  Thsi has to be a bug!! Why on earth should it fire 
> on first character entry and the bigger issue is however it fires it 
> should fire consistently.
>
> You can observe this behavior using the following steps:
>
> 1.  Open a new Word Document.
> 2.  Paste the following code in the "ThisDocument" class module:
>
> Option Explicit
> Dim WithEvents oMonitor As CustomXMLPart
> Sub SetMonitor()
> On Error Resume Next
> Set oMonitor = ThisDocument.CustomXMLParts(4)
> End Sub
> Private Sub oMonitor_NodeAfterInsert(ByVal NewNode As 
> Office.CustomXMLNode, ByVal InUndoRedo As Boolean)
> Main.CCValidaton NewNode
> End Sub
> Private Sub oMonitor_NodeAfterReplace(ByVal OldNode As 
> Office.CustomXMLNode, ByVal NewNode As Office.CustomXMLNode, ByVal 
> InUndoRedo As Boolean)
> Main.CCValidaton NewNode
> End Sub
>
> 3.  Paste the following code in a standard module and run AddCCsAndMap. 
> This will add four mapped CCs to the document.
>
> Option Explicit
> Sub AddCCsAndMap()
> Dim oCC As ContentControl
> With ActiveDocument
>  .Range.InsertAfter "Test 1: "
>  .Bookmarks("\endofdoc").Select
>  Set oCC = .ContentControls.Add(wdContentControlText)
>  oCC.Title = "Test 1"
>  .Range.InsertAfter vbCr + vbCr & "Test 2: "
>  .Bookmarks("\endofdoc").Select
>  Set oCC = .ContentControls.Add(wdContentControlText)
>  oCC.Title = "Test 2"
> .Range.InsertAfter vbCr + vbCr & "Test 3: "
>  .Bookmarks("\endofdoc").Select
>  Set oCC = .ContentControls.Add(wdContentControlText)
>  oCC.Title = "Test 3"
>  .Range.InsertAfter vbCr + vbCr & "Test 4: "
>  .Bookmarks("\endofdoc").Select
>  Set oCC = .ContentControls.Add(wdContentControlText)
>  oCC.Title = "Test 4"
> End With
> MapCCs
> ThisDocument.SetMonitor
> End Sub
>
> Sub MapCCs()
> Dim oCC As ContentControl
> Dim pXML As String
> Dim oCustXMLPart As CustomXMLPart
> Dim XPath As String
> pXML = "<?xml version='1.0' 
> encoding='utf-8'?><Main><Test_1></Test_1><Test_2></Test_2><Test_3></Test_3>" 
> _
>       & "<Test_4></Test_4></Main>"
> ClearXMLParts
> Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add(pXML)
> Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 1").Item(1)
> XPath = "/Main/Test_1[1]"
> oCC.XMLMapping.SetMapping XPath
> Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 2").Item(1)
> XPath = "/Main/Test_2[1]"
> oCC.XMLMapping.SetMapping XPath
> Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 3").Item(1)
> XPath = "/Main/Test_3[1]"
> oCC.XMLMapping.SetMapping XPath
> Set oCC = ActiveDocument.SelectContentControlsByTitle("Test 4").Item(1)
> XPath = "/Main/Test_4[1]"
> oCC.XMLMapping.SetMapping XPath
> Set oCC = Nothing
> Set oCustXMLPart = Nothing
> End Sub
> Sub ClearXMLParts()
> Dim i As Long
> For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1
>  ActiveDocument.CustomXMLParts(i).Delete
> Next i
> End Sub
>
> Sub CCValidaton(oNode As Office.CustomXMLNode)
> Dim pStr As String
> pStr = oNode.Text
> With ThisDocument
>  Select Case oNode.ParentNode.BaseName
>    Case "Test_1"
>      If Len(pStr) < 5 Then
>        MsgBox "Invalid entry"
>        .SelectContentControlsByTitle("Test 
> 1").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
>      Else
>        .SelectContentControlsByTitle("Test 
> 1").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
>      End If
>    Case "Test_2"
>      If Len(pStr) < 5 Then
>        MsgBox "Invalid entry"
>        .SelectContentControlsByTitle("Test 
> 2").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
>      Else
>        .SelectContentControlsByTitle("Test 
> 2").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
>      End If
>    Case "Test_3"
>      If Len(pStr) < 5 Then
>        MsgBox "Invalid entry"
>        .SelectContentControlsByTitle("Test 
> 3").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
>      Else
>        .SelectContentControlsByTitle("Test 
> 3").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
>      End If
>    Case "Test_4"
>      If Len(pStr) < 5 Then
>        MsgBox "Invalid entry"
>        .SelectContentControlsByTitle("Test 
> 4").Item(1).Range.Shading.BackgroundPatternColor = wdColorRose
>      Else
>        .SelectContentControlsByTitle("Test 
> 4").Item(1).Range.Shading.BackgroundPatternColor = wdColorAutomatic
>      End If
>  End Select
> End With
> End Sub
>
> 4.  Click in the Test 1 CC and enter 1 to 4 characters then tab.  This 
> will show you the expected behavior.
>
> 5.  The focus is now in Test 2 CC.  Type a single character.  This will 
> show you the unexpected behavior and the bug!
>
> 6.  The focus is now in Test 3 CC.  This one behaves as expected.  Test 4 
> is unexpected.
>
> For whatever reason every other CC behaves unexpectantly.  If you start 
> with Test 2 then is is OK but Test 3 then fails.  Start with 4 OK but Test 
> 1 fails.
>
> I have repeated this behavior consistently over 50 times.
>
> Appreciate any response confirming this behavior and earnestly hoping that 
> fixed in Word2010.
>
> -- 
> Greg Maxey
>
> See my web site http://gregmaxey.mvps.org
> for an eclectic collection of Word Tips.
>
> Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)
>
>
> 


0
Reply Greg 11/12/2009 9:15:12 PM

1 Replies
525 Views

(page loaded in 0.075 seconds)


Reply: