XMLAfterInsert XMLBeforeDelete and text boxes problem.

G

Gareth

It appears that xml nodes in text boxes do not recieve either of the
xmlafterinsert or xmlbeforedelete events - any ideas on how this could be
worked around?
 
P

Peter Huang

Hi Gareth,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you will insert a textbox into the
word document by clicking Insert/TextBox/Horizontal, and then insert a few
xml node into textbox or delete a few xml nodes, but the xmlafterinsert or
xmlbeforedelete events did not fired.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

The two events belongs to the Document object.

New Events
See AlsoSpecificsVisit the Office Developer Center on the Microsoft
Developer Network Web site for the latest information about programming
with Microsoft Office Word 2003, including product news, technical
articles, downloads, and samples.

The following table lists events added to the Office Word 2003 object model.

New Event Object
DocumentSync Application
EPostageInsertEx Application
Sync Document
XMLAfterInsert Document
XMLBeforeDelete Document
XMLSelectionChange Application
XMLValidationError Application



I can not reproduce the problem.
Here is my reproduce step.
1. Open an xml file in word.
2. Handle the two events in ThisDocument in the VBA editor as below.
Private Sub Document_XMLAfterInsert(ByVal NewXMLNode As XMLNode, ByVal
InUndoRedo As Boolean)
Debug.Print "Document_XMLAfterInsert"
End Sub

Private Sub Document_XMLBeforeDelete(ByVal DeletedRange As Range, ByVal
OldXMLNode As XMLNode, ByVal InUndoRedo As Boolean)
Debug.Print "Document_XMLBeforeDelete"
End Sub

3. Now if we delete or insert a new xml nodes(by cut and paste) the two
events will be fired.

4. Insert an textbox in the document by clicking Insert/TextBox/Horizontal

5. Copy some xml node into the textbox, the Document_XMLAfterInsert will be
fired.

6. Delete some nodes from the textbox, the Document_XMLBeforeDelete will
also be fired.

Please you may try my suggestion above and let me know if the problem
persists.

If no, can you tell me what do you handle these events, is it in an COM
addin, what is the addin written by? C#,VB.NET or else?

Can you post some simple reproduce code here for further troubleshooting?


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

Gareth

Appologies, it seems I posted a bit early, I miss diagnosed the problem I
was having.
The events do indeed get fired. The real problem I am having is that the
newXMLNode seems to point to the wrong node. Hence when I go to mark the
inserted node as bad, by setting an attribute, (for deletion at a later
time, since deleting immediately causes bugs when a queue of xmlafterinsert
events is being performed) a different node gets marked bad, and the
original node does not, hence my confusion.

I am writing a c# com plugin, loaded without using the managed office loader
(since that also causes problems), just normal com. I am using the word
2003 PIA.

Exact details of this issue.
add a text box using the drawing toolbar.
click a button which causes programatic insertion of a pair of xml tags, and
sets a few attributes on them.
copy an xml tag (as in ctrl-C).
click in text box, paste.
xmlafterinserts do occur, the newXMLNode variable has an attribute added
called Bad with value DEAD.
right click on new xmlnodes - choose attributes. Notice no attribute called
Bad set.
right click on the first xmlnode in document - choose attributes. Notice Bad
set with value DEAD.

if repeated - the first xmlnode in document ends up with Multiple Bad
attributes. This is despite the fact that the xmlafterinsert code checks
for an existing bad attribute before adding a new one, and will change the
existing one if it exists. If you copy/paste n xmlnodes, the first n nodes
in the document get their attributes set.

Other issues i've been having I might mention here.
If select multiple xmlnodes copy then paste
XMLAfterInsert is fired, but if the responce XMLAfterInsert event handler
takes is newXMLNode.Delete(), then only half (rounding upwards) of the nodes
produce the event.
If I select some nodes - copy then hold down ctrl-V. XMLAfterInsert marks
nodes bad, OnSelectionChange searches the document and removes nodes with
bad attribute. Result, only half (Rounding upwards) of the inserted nodes
(After the first paste) receive the event and get marked bad&removed. (I
previous reported that All of them didnt recieve events, but I must of been
mistaken.)

Another strange one is when draging multiple nodes to a later position in
the document. If xmlbeforedelete records their details as text (not the
nodes themselves) for reinsertion at the originating point, and
onselectionchange performs the reinsertion, then XMLAfterInsert is performed
on an incorrect set of nodes. (one example, dragging 2 nodes to the right,
past a 3rd node - results in the 3rd node recieving an xmlafterinsert event,
the two dragged nodes receiving 2 and 1 xmlafterinsert events respectively.
While the reinserted two nodes (to the left of node 3) correctly recieve no
events.

All in all XMLAfterInsert seems to be a bit confused.

Gareth
 
G

Gareth

Perhaps another related issue. XML nodes in textboxes do not appear to
correctly set up there sibling fields. Two nodes side by side in the same
textbox will both have all sibling parent and child fields set to null.
In addition i have found that the 'attributes go missing, turn up on nodes
in main story' issue also applies with headers/footers - and at a guess
would extrapolate it to being all non main-document storys.
I can add attributes to them manually, but not programatically.

Gareth
 
L

Lori Turner [MSFT]

Hi Gareth,

I've tried to reproduce the problem you've described but have not been
successful. I tried to complete your steps as follows but do not see the
behavior you see:

1) I created an XMLSchema and attached it to the document:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element1">
<xs:complexType>
<xs:sequence />
<xs:attribute name="attribute1" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>

2) I then ran the following macro a couple of times to insert nodes into
the document:

Public Sub AddNode()
Dim n As XMLNode
Set n = ActiveDocument.XMLNodes.Add("element1",
"http://tempuri.org/XMLSchema.xsd", Selection)
n.Attributes.Add("attribute1", "").NodeValue = "test"
End Sub

3) I copied the nodes, inserted a text box in the document and pasted the
nodes into the text box. I checked the attributes for each node and they
all appear correct -- each node had just the one attribute "attribute1" set
to "test".

4) I repeated steps 2 and 3 several times but the attributes still looked
ok.

Is there anything I'm missing in the steps above that would enable me to
reproduce the problem you're experiencing?

Regards,
Lori Turner
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
G

Gareth

Hi,
See below for comments on where you have not followed the required steps for
reproduction.
Hi Gareth,

I've tried to reproduce the problem you've described but have not been
successful. I tried to complete your steps as follows but do not see the
behavior you see:

1) I created an XMLSchema and attached it to the document:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element1">
<xs:complexType>
<xs:sequence />
<xs:attribute name="attribute1" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>

2) I then ran the following macro a couple of times to insert nodes into
the document:

Public Sub AddNode()
Dim n As XMLNode
Set n = ActiveDocument.XMLNodes.Add("element1",
"http://tempuri.org/XMLSchema.xsd", Selection)
n.Attributes.Add("attribute1", "").NodeValue = "test"
End Sub

3) I copied the nodes, inserted a text box in the document and pasted the
nodes into the text box. I checked the attributes for each node and they
all appear correct -- each node had just the one attribute "attribute1" set
to "test".

As stated in my previous message. An event handler handling the
XMLAfterInsert document event is needed to trigger the bug. The event
handler should attempt to add attributes to the new XMLNode which is passed
as the second parameter of the event handler. It is in this case that the
attributes break, when paste is used in a text box or header/footer and
possibly other cases.
XMLAfterInsert is the problem child, without it there is no problem with the
attributes.

There are other problems, like nodes in textboxes not having valid
NextSibling/PreviousSibling properties despite there definitely being
siblings, but the attribute issue is an XMLAfterInsert event handler
problem.

Gareth
 
L

Lori Turner [MSFT]

Hi Gareth,

Thank you for the additional information; I was able to reproduce the
problems you described and have filed bug reports on both (the reports I
submitted are pasted below). Unfortunately, I do not see a workaround;
however, if I learn of one from the bug report resolution, I will certainly
post that information here as a reply to this message.

=======================

Title: Word 11: Attribute added in XMLAfterInsert event is added to wrong
node & crashes Word

Overview of the problem:
When you use the XMLAfterInsert event to add an attribute to an XML node,
the attribute is added to the wrong node when the new node resides in a
text box or in the header/footer.

1. Start a new document (Doc1).
2. Attach the following schema to Doc1:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element1">
<xs:complexType>
<xs:sequence />
<xs:attribute name="attribute1" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>

3. Add the following code to the ThisDocument code module of Doc1:

Private Sub Document_XMLAfterInsert(ByVal NewXMLNode As XMLNode, ByVal
InUndoRedo As Boolean)
Static i As Integer
NewXMLNode.Attributes.Add("attribute1", "").NodeValue = i
i = i + 1
End Sub

4. Insert an element1 node; verify the attribute1=0.
5. Insert a second element1 node; verify the attribute1 of the first
element is still 0 and the attribute1 of the second element is 1.
6. Activate the header/footer.
7. Insert a third element1 node into the header.

Now observe that the third element1 has no attribute1 but the attributes in
the first element1 have incorrectly changed.

================
Title: Word 11: Cannot access NextSibling and PreviousSibling properties
for XMLNodes in header/footer

Overview of the problem:
Elements in header/footer and textboxes cannot be accessed via NextSibling
and PreviousSibling.

1. Start a new document (Doc1).
2. Attach a schema to Doc1. For example:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element1">
<xs:complexType>
<xs:sequence />
<xs:attribute name="attribute1" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
3. Add this macro to Doc1:
Public Sub test()
Dim n As XMLNode
Set n = Selection.XMLNodes(1).NextSibling
If n Is Nothing Then
MsgBox "No next sibling"
Else
MsgBox n.BaseName
End If
End Sub
4. Insert two elements in the body of the document, select both elements
and run the macro test -- notice that the sibling is found.
5. Insert two elements in the header of the document, select both elements
and run the macro test -- notice that NO sibling is found.
6. Insert two elements into a text box in the document, select both
elements and run the macro test -- notice that NO sibling is found.

Regards,
Lori Turner
Microsoft Developer Support

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