PDA

View Full Version : Mapped CC not remapped when a node is deleted



Hawkansson
01-09-2017, 07:15 AM
Hi again,

I have written some macros to add or delete a table row. Each cell in the row contains a content control of some sort which automatically gets mapped to my custom xml. For example the structure could look like this in the example of a two row table:



<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
<Protocol xmlns='My_Xml'>"
<TableRow>
<Cell1></Cell1>
<Cell2></Cell2>
<Cell3></Cell3>
</TableRow>

<TableRow>
<Cell1></Cell1>
<Cell2></Cell2>
<Cell3></Cell3>
</TableRow>
</Protocol>


The resulting Xpaths of the CCs in above mentioned cells becomes like this:

/ns0:Protocol/ns0:TableRow[1]/ns0:Cell1[1]
/ns0:Protocol/ns0:TableRow[1]/ns0:Cell2[1]
/ns0:Protocol/ns0:TableRow[1]/ns0:Cell3[1]

/ns0:Protocol/ns0:TableRow[2]/ns0:Cell1[1]
/ns0:Protocol/ns0:TableRow[2]/ns0:Cell2[1]
/ns0:Protocol/ns0:TableRow[2]/ns0:Cell3[1]

Further on I have copied the mapped CCs to several occurances through out the document. Now let's say that I delete table row 1 and also the corresponding node /ns0:Protocol/ns0:TableRow[1] . Then the node which was previously /ns0:Protocol/ns0:TableRow[2] now automatically gets renumbered as /ns0:Protocol/ns0:TableRow[1] .

This messes up my document. The CCs mapped to /ns0:Protocol/ns0:TableRow[2] still points at that node, but it doesn't exist in the xml. The content controls mapped to /ns0:Protocol/ns0:TableRow[1] keeps that Xpath, but now points at the values that was originally in the second row.

I thought that if I deleted the node /ns0:Protocol/ns0:TableRow[1] , the CCs mapped to that node would loose its mapping, and all CCs mapped to /ns0:Protocol/ns0:TableRow[2] would get renumbered to /ns0:Protocol/ns0:TableRow[1] .

Do you have any suggestions on how I could solve this? Is there a better way in implementing my xml? Is there any procedures to remap the content controls in case of a node deletion?

Best regards,
David

gmaxey
01-09-2017, 08:33 AM
Perhaps you could monitor the XMLPart in the ThisDocument class and use the NodeAfterDelete event:


Option Explicit
Dim WithEvents oMonitor As CustomXMLPart
Sub InitializeMonitor()
On Error Resume Next
Set oMonitor = ThisDocument.CustomXMLParts(4)
End Sub

Private Sub oMonitor_NodeAfterDelete(ByVal OldNode As Office.CustomXMLNode, ByVal OldParentNode As Office.CustomXMLNode, ByVal OldNextSibling As Office.CustomXMLNode, ByVal InUndoRedo As Boolean)
Dim oCC As ContentControl
For Each oCC In ActiveDocument.ContentControls 'Won't get CCs outside the main text storyrange.
If oCC.XMLMapping.CustomXMLNode.XPath = OldNode.XPath Then
'Do whatever to set new mapping
End If
Next
End Sub

Hawkansson
01-17-2017, 05:52 AM
Thanks for the suggestion Greg. I ended up protecting the section so the user can only delete a row by a macro that also controls the mappings.