A
Angelos Petropoulos
Hi,
Consider a Word Template project, with the template containing a few lines
of text and an XML tag around the whole document. During document startup you
add a button in the Actions Pane and you listen to the Click event.
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
UserControl1 userControl = new UserControl1();
userControl.button1.Click += new EventHandler(button1_Click);
this.ActionsPane.Controls.Add(userControl);
}
In the event handler for the Click event you add an XML tag around the
current selection.
void button1_Click(object sender, EventArgs e)
{
this.Application.Selection.XMLNodes.Add(
"ChildElement", "http://tempuri.org/VSTOAddXmlNodeSchema.xsd", ref
missing);
}
The first time you click on the button you will get a COMException exception
with the message “This XML element cannot be applied here†(you can find
complete exception information at the bottom of this message). From then on,
every time the button is clicked no exception will be thrown and the code
will work.
Is there a specific reason behind this behaviour or is it a bug? If it is a
bug, is it safe to simply call it the first time expecting it to fail and
call it again or will this de-stabilize the environment? For those kind
enough to look into this problem, I have created a simple project that
reproduces the problem. You can download it here. (my usual file host is
down, so I am using an upload service; if the link stops working please do
let me know)
Thank you in advance
Angelos Petropoulos
--
http://blogs.wdevs.com/angelos
“This XML element cannot be applied hereâ€
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A17DD): This XML element
cannot be applied here.
at VSTOAddXmlNode.ThisDocument.AddXmlTag() in C:\Documents and
Settings\angelosp\My Documents\Visual Studio
2005\Projects\VSTOAddXmlNode\VSTOAddXmlNode\ThisDocument.cs:line 56
at VSTOAddXmlNode.ThisDocument.button1_Click(Object sender, EventArgs e)
in C:\Documents and Settings\angelosp\My Documents\Visual Studio
2005\Projects\VSTOAddXmlNode\VSTOAddXmlNode\ThisDocument.cs:line 26
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
Consider a Word Template project, with the template containing a few lines
of text and an XML tag around the whole document. During document startup you
add a button in the Actions Pane and you listen to the Click event.
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
UserControl1 userControl = new UserControl1();
userControl.button1.Click += new EventHandler(button1_Click);
this.ActionsPane.Controls.Add(userControl);
}
In the event handler for the Click event you add an XML tag around the
current selection.
void button1_Click(object sender, EventArgs e)
{
this.Application.Selection.XMLNodes.Add(
"ChildElement", "http://tempuri.org/VSTOAddXmlNodeSchema.xsd", ref
missing);
}
The first time you click on the button you will get a COMException exception
with the message “This XML element cannot be applied here†(you can find
complete exception information at the bottom of this message). From then on,
every time the button is clicked no exception will be thrown and the code
will work.
Is there a specific reason behind this behaviour or is it a bug? If it is a
bug, is it safe to simply call it the first time expecting it to fail and
call it again or will this de-stabilize the environment? For those kind
enough to look into this problem, I have created a simple project that
reproduces the problem. You can download it here. (my usual file host is
down, so I am using an upload service; if the link stops working please do
let me know)
Thank you in advance
Angelos Petropoulos
--
http://blogs.wdevs.com/angelos
“This XML element cannot be applied hereâ€
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A17DD): This XML element
cannot be applied here.
at VSTOAddXmlNode.ThisDocument.AddXmlTag() in C:\Documents and
Settings\angelosp\My Documents\Visual Studio
2005\Projects\VSTOAddXmlNode\VSTOAddXmlNode\ThisDocument.cs:line 56
at VSTOAddXmlNode.ThisDocument.button1_Click(Object sender, EventArgs e)
in C:\Documents and Settings\angelosp\My Documents\Visual Studio
2005\Projects\VSTOAddXmlNode\VSTOAddXmlNode\ThisDocument.cs:line 26
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************