Word 2003 bug: creating XML markup when switching between document stories

C

Chango V.

Hello,

We ran into a pretty severe bug. If you switch between the main document
story and another one, for example footnotes, and depending on the current
view state of the document window, creating XML markup may not work
properly. In particular, the XMLNodes.Add method returns an element
different from the one it has created. The following macro demonstrates one
instance of the problem:

Sub DocStoriesXMLBug()
Dim doc As New Document
ns = "urn:xxx.org/schemas/StdMasterChapter-WxS"
' any namespace registered in Word's Schema Library
doc.XMLSchemaReferences.Add ns
Application.TaskPanes(wdTaskPaneXMLStructure).Visible = True
' This step is critical! Without it, the problem doesn't show up
right away.
Dim fn As Footnote
Set fn = doc.Footnotes.Add(doc.Range(), , "Footnote text")
fn.Range.Words(2).XMLNodes.Add "ep", ns
' Substitute "ep" with any element from the chosen schema
fn.Range.Words(1).XMLNodes.Add("ep", ns).Range.Select
'**The bug: The element created should be around the first footnote
word and its
' range should get selected. However, the Add method returns the
element created
' around the second word!
End Sub

Extensive experimentation shows that whether the XML Structure task pane is
visible and what it currently displays somehow triggers the problem. It can
happen even when trying to create markup in the main document story: If the
selection (insertion point) is within a footnote, say, and the XML Structure
is visible, any code trying to create markup in the main document story at
that point is likely to fail. The other required conditions seems to be that
there has to be another element following the new one created. (That's why
the macro above marks up the two footnote words in reverse order.) Further,
if you have the XML Structure view open while the selection is within one
document story and then close the XML Structure view and move the selection
to another document story and try to create markup there, the problem will
show up. Essentially, the XML Structure view has to be visible in order for
it to trigger some state change inside Word so that markup can be created
normally.

I hope someone will have a suggestion for an effective workaround and that
MS folks will look into the problem and find out the root cause. Any help
will be appreciated.

Thanks.

// Chango V.

NOTE: This post is from an anti-spam, non-existent email address. For
further communication, please reply to the Newsgroup and/or contact me
directly at (e-mail address removed).
 
P

Peter Huang

Hi Chango,

I can reproduce the problem with the code line below.
Application.TaskPanes(wdTaskPaneXMLStructure).Visible = True
Based on my test, the fn.Range.Words(1).XMLNodes.Add("ep", ns).Range.Text
will return "text" which is not our expected behavior.

So far it seems that if we do not run the code line first
Application.TaskPanes(wdTaskPaneXMLStructure).Visible = True
The code will work well.
So can you also provide a simple reproduce sample that will reproduce the
problem even if we do not call the
Application.TaskPanes(wdTaskPaneXMLStructure).Visible = True?
Please also speicify the detailed reproduce steps so that we can report the
problem to out product team.

Thank you for your understanding.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chango V.

Hello Peter,

Thanks for looking into the problem. There are many variants of the bug.
Sometimes you get a crash instead of just the wrong element. And you don't
have to try to create markup in footnotes. (One might dismiss such a problem
as obscure and irrelevant to most people.) For example, this variant just
tries to create two ordinary elements in the main document story, and it
crashes most of the time:

Sub DocStoriesXMLBug2()
Dim doc As New Document
ns = "urn:xxx.org/schemas/StdMasterChapter-WxS"
' any namespace registered in Word's Schema Library
doc.XMLSchemaReferences.Add ns
Application.TaskPanes(wdTaskPaneXMLStructure).Visible = True
doc.Content.Text = "Paragraph 1" & vbLf & "Paragraph 2"
doc.Footnotes.Add doc.Range(), , "Footnote text"
doc.ActiveWindow.View.Type = wdPrintPreview
doc.ActiveWindow.View.SeekView = wdSeekFootnotes
Dim elem As XMLNode
Set elem = doc.XMLNodes.Add("para", ns, doc.Paragraphs(2).Range)
Set elem = doc.XMLNodes.Add("para", ns, doc.Paragraphs(1).Range)
elem.Range.Select
End Sub

When it doesn't crash, you'd get the wrong element, just like in my first
example. The line that turns on the XML Structure tree is still there. To
see the problem without it (and without programmatically changing the
current view to footnotes as the macro does), follow these simple steps:
1. Create a blank document.
2. Insert a footnote (from the Insert menu). Type some text for the
footnote.
3. While the selection (insertion point) is within the footnote, open the
XML Structure task pane. [Keep the selection in the footnote.]
4. Now, optionally, you can close the task pane. (This little variant of the
script can give an insight into the problem.)
5. Run this macro, which just creates two document paragraph and tries to
enclose each of them in an element:

Sub DocStoriesXMLBug3()
Dim doc As Document
Set doc = ActiveDocument
ns = "urn:xxx.org/schemas/StdMasterChapter-WxS"
' any namespace registered in Word's Schema Library
doc.XMLSchemaReferences.Add ns
doc.Content.InsertBefore "Paragraph 1" & vbLf & "Paragraph 2"
Dim elem As XMLNode
Set elem = doc.XMLNodes.Add("para", ns, doc.Paragraphs(2).Range)
Set elem = doc.XMLNodes.Add("para", ns, doc.Paragraphs(1).Range)
elem.Range.Select
End Sub

As in the previous versions, if there is no crash, the returned element from
the second XMLNodes.Add() call will be the wrong one.

My insight into the root cause of the problem is this: Word apparently keeps
separate XML node sets (and element structure trees) for different document
stories. It also must have some internal pointer to the "current" element
tree. If the XML Structure task pane is visible, this pointer gets updated
when you move the selection between document stories. The problem is that if
you try to programmatically create markup in a document story other than the
"current" one, Word apparently gets confused and either returns the wrong
element or crashes. This problem can affect just about any ordinary custom
application trying to manipulate even very simple XML markup in a document.
All it takes for the bug to bite is to have the selection (insertion point)
in a footnote, comment, etc, and run code that tries to create markup in the
main document story. I think the problem is pretty severe (if affects us
badly now), and the reason I am spending so much time on investigating it
and reporting it is that I am hoping someone from the Word team will pay
attention and, hopefully, provide a fix in a downloadable Office update
soon. My workarounds for the problem so far have limited applicability and
some undesirable side effects.

// Chango V.
 
P

Peter Huang

Hi Chango,

Thank you for you reponse.
Now I can reproduce the problem on my side and I think this may be an issue
of our product, now I have reported the problem to our product team for
confirm and ask for if there is any new information for you. I know that
the problem now has affected you severely, I will get back here once I get
any new information. If the problem has been confirmed as a bug in our
problem, you can contact our Microsoft PSS for hotfix for this problem, and
that case instance is free.

Thank you for your understanding.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi,

Now our product team is working on the issue,please pay more patience.
Thank you for your understanding.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chango V.

Thanks for the update. I can be patient as long as there is hope...

Here is another nasty problem I encountered, possibly related to the first
one: If you have a saved document with XML markup in footnotes, the first
attempt after opening the document to access the XMLNode objects or their
properties can fail, with generic/meaningless error messages. It's not every
document, but when the "right conditions" are there (which I don't know what
they are), the error is very persistent. It won't go away until you manually
switch to the first footnote and view the XML markup or programmatically
access some other, unrelated property on the XMLNode object. In particular,
I found that most properties can throw exceptions, and somehow accessing
ChildNodeSuggestions brings Word back to its senses and everything starts to
work normally from that point on. (So, this is my workaround now, but it's
not pretty, and I'm not sure whether I am fully protected.)

//
 
P

Peter Huang

Hi Chango,

Now we have confirmed that this is a problem in our product, now we are
trying to find a workaround for you.
As for the new problem you have reported seems to be concern about the
problem, and it seems that your workaround does work.
Here is my reproduce code.

Sub Test()
Dim doc As Document
Set doc = Documents.Open("c:\test.doc") ¡®test.doc has a xmlnodes in the
footnote
Dim oNode As XMLNode
Set oNode = doc.Footnotes(1).Range.XMLNodes(1)
Debug.Print oNode.Range.Text ¡®The code line will work
¡®If we comment out the code line, the next line will report the error
message
¡® ¡± Run-time error '-2147467259 (80004005)': Method 'Text' of object
'XMLNode' failed¡±
Debug.Print oNode.ChildNodeSuggestions.Count
Debug.Print oNode.Text
doc.Close False
End Sub

If I have any misunderstanding, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi Chango,

We have already involved product group and it is confirmed as one of
product issues. We have entered it in the database and the product group
will look into it.
If you want a hotfix for it, you may send a request to Microsoft PSS. And
the request should be a free incident for you.
If you still have any concern on this issue, please feel free to let me
know and I will be glad to be of assistance.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chango V.

Thanks for the legwork, Peter. I'm not sure what a hotfix might look like
and whether it would be obtainable at all, but I will use your suggestion
for contacting PSS. I suppose it will be enough to (re)describe the problem
to them and/or reference our discussion thread here.
Thanks much for the help.

// Chango
 
P

Peter Huang

Hi Chango,

A hotfix is similar with a patch, but it is more specified to the problem
in your environment.
Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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