C
Carl
Hi
I've tried lots of different ways to get this example to work, but nothing
seems to happen.
I have a Dataset in a winform. I take one record in the dataset and convert
it to Xml with the WriteXml method. This works fine.
Then I export it to Word, do a minor change to it (using the Xml-tools for
Word), and hook up the BeforeSave event. This also works fine.
When the code in the event starts to execute, I get the Xml-code back, I can
see that it is changed when I inspect it. Good.
Now, to get it into a dataset, and update it to the backend db seems
virtually impossible. In the code below, two of all of my trials are present
(one is commented out). Exceptions are never raised, it just does not do
anything! I would be very happy if someone could point out what I'm doing
wrong!
Generate the Xml to send to Word:
private void GenerateXML_Click(object sender, EventArgs e)
{
//testDataSet tds = new testDataSet();
DataTable dt = documentTableAdapter.GetDocumentById(1);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.DiffGram);
xmlDoc = sw.ToString();
if (! File.Exists(xmlFileName))
{
ds.WriteXml(xmlDoc, XmlWriteMode.DiffGram);
}
if (!File.Exists(xmlSchemaName))
{
ds.WriteXmlSchema(xmlSchemaName);
}
}
Send it to Word:
private void btSendToWord_Click(object sender, EventArgs e)
{
Word.ApplicationClass wordApp = new Word.ApplicationClass();
wordApp.Visible = true;
Word.Document doc = wordApp.Documents.Add(ref missing, ref
missing, ref missing, ref missing);
Object aliasObj = "XmlTestSchema";
Object namespaceUriObj = "http://www.w3.org/2001/XMLSchema";
Word.XMLNamespace schema =
wordApp.XMLNamespaces.Add("c:\\Temp\\XmlTest\\XmlTestSchema.xsd", ref
namespaceUriObj, ref aliasObj, false);
Object obj = doc;
schema.AttachToDocument(ref obj);
Word.Range range = doc.Range(ref missing, ref missing);
range.InsertXML(xmlDoc, ref missing);
wordApp.DocumentBeforeSave += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(wordApp_DocumentBeforeSave);
}
Try to get it back, this is probably where the problem is:
void
wordApp_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref
bool SaveAsUI, ref bool Cancel)
{
string xmlReturn = Doc.Range(ref missing, ref
missing).get_XML(true);
System.Diagnostics.Debug.Write(xmlReturn);
//DataSet ds = new DataSet();
//ds.ReadXml(new StringReader(xmlReturn),
XmlReadMode.InferSchema);
testDataSet.ReadXml(new StringReader(xmlReturn),
XmlReadMode.DiffGram);
//testDataSet.Merge(ds, true, MissingSchemaAction.AddWithKey);
documentTableAdapter.Update(testDataSet);
}
regards
Carl
I've tried lots of different ways to get this example to work, but nothing
seems to happen.
I have a Dataset in a winform. I take one record in the dataset and convert
it to Xml with the WriteXml method. This works fine.
Then I export it to Word, do a minor change to it (using the Xml-tools for
Word), and hook up the BeforeSave event. This also works fine.
When the code in the event starts to execute, I get the Xml-code back, I can
see that it is changed when I inspect it. Good.
Now, to get it into a dataset, and update it to the backend db seems
virtually impossible. In the code below, two of all of my trials are present
(one is commented out). Exceptions are never raised, it just does not do
anything! I would be very happy if someone could point out what I'm doing
wrong!
Generate the Xml to send to Word:
private void GenerateXML_Click(object sender, EventArgs e)
{
//testDataSet tds = new testDataSet();
DataTable dt = documentTableAdapter.GetDocumentById(1);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.DiffGram);
xmlDoc = sw.ToString();
if (! File.Exists(xmlFileName))
{
ds.WriteXml(xmlDoc, XmlWriteMode.DiffGram);
}
if (!File.Exists(xmlSchemaName))
{
ds.WriteXmlSchema(xmlSchemaName);
}
}
Send it to Word:
private void btSendToWord_Click(object sender, EventArgs e)
{
Word.ApplicationClass wordApp = new Word.ApplicationClass();
wordApp.Visible = true;
Word.Document doc = wordApp.Documents.Add(ref missing, ref
missing, ref missing, ref missing);
Object aliasObj = "XmlTestSchema";
Object namespaceUriObj = "http://www.w3.org/2001/XMLSchema";
Word.XMLNamespace schema =
wordApp.XMLNamespaces.Add("c:\\Temp\\XmlTest\\XmlTestSchema.xsd", ref
namespaceUriObj, ref aliasObj, false);
Object obj = doc;
schema.AttachToDocument(ref obj);
Word.Range range = doc.Range(ref missing, ref missing);
range.InsertXML(xmlDoc, ref missing);
wordApp.DocumentBeforeSave += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(wordApp_DocumentBeforeSave);
}
Try to get it back, this is probably where the problem is:
void
wordApp_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref
bool SaveAsUI, ref bool Cancel)
{
string xmlReturn = Doc.Range(ref missing, ref
missing).get_XML(true);
System.Diagnostics.Debug.Write(xmlReturn);
//DataSet ds = new DataSet();
//ds.ReadXml(new StringReader(xmlReturn),
XmlReadMode.InferSchema);
testDataSet.ReadXml(new StringReader(xmlReturn),
XmlReadMode.DiffGram);
//testDataSet.Merge(ds, true, MissingSchemaAction.AddWithKey);
documentTableAdapter.Update(testDataSet);
}
regards
Carl