Some people were using digital cameras to capture the source code from our "InfoPath: Developing Forms with Managed Code" sessions, but we want to make it easier than that!
So here's the code Ned Friend used in the TechEd Europe version of the presentation (David Gerhardt used similar code in his USA version of the talk in Orlando):
DISCLAIMER: This code is designed to be short and sweet for demo purposes only. IT IS NOT BEST PRACTICES NOR READY FOR PRODUCTION.
using
// Add .NET reference: System.Xml
// Add COM reference: Microsoft Word 11.0 Object Library
// Office integration attribute. Identifies the startup class for the form. Do not
// modify.
[assembly: System.ComponentModel.DescriptionAttribute(
namespace
{
[InfoPathNamespace(
thisXDocument = doc;
thisApplication = app;
}
[InfoPathEventHandler(EventType = InfoPathEventType.OnLoad)]
IXMLDOMNode userName =
thisXDocument.DOM.selectSingleNode(
userName.text = Environment.UserName;
IXMLDOMNode keyNode = thisXDocument.DOM.selectSingleNode(
IXMLDOMNode ivNode = thisXDocument.DOM.selectSingleNode(
MemoryStream ms =
CryptoStream csRijndael =
IXMLDOMNode projectNode =
csRijndael.Write(projectBytes, 0, (
csRijndael.FlushFinalBlock();
System.Text.Encoding.Unicode.GetString(ms.GetBuffer(), 0, (
projectNode.text = projectName;
[InfoPathEventHandler(MatchPath =
DateTime date = DateTime.Parse(e.NewValue.ToString());
e.ReportError(e.Site,
[InfoPathEventHandler(EventType = InfoPathEventType.OnSubmitRequest)]
WebServiceAdapter2 webServiceAdapter =
(WebServiceAdapter2)thisXDocument.DataAdapters[
webServiceAdapter.Submit();
XmlDocument xmlDoc =
xmlDoc.PreserveWhitespace =
xmlDoc.LoadXml(thisXDocument.DOM.xml);
xmlDoc.Save(
e.ReturnStatus =
[InfoPathEventHandler(EventType = InfoPathEventType.OnSaveRequest)]
CryptoStream csBase64 =
System.Text.Encoding.Unicode.GetBytes(projectNode.text);
System.Text.Encoding.ASCII.GetString(ms.GetBuffer(), 0, (
keyNode.text = Convert.ToBase64String(key.Key);
ivNode.text = Convert.ToBase64String(key.IV);
projectNode.text = projectEncrypted;
e.IsCancelled = e.PerformSaveOperation();
projectNode.text = projectOriginal;
thisXDocument.SetDirty(
IXMLDOMDocument domDocument = thisXDocument.CreateDOM();
domDocument.load(
XmlDocument xsltDocument =
xsltDocument.LoadXml(domDocument.xml);
XmlDocument infoPathDocument =
infoPathDocument.LoadXml(thisXDocument.DOM.xml);
XslCompiledTransform xslt =
xslt.Load(xsltDocument);
XmlDocument outputDocument =
System.Xml.XPath.XPathNavigator outputNavigator = outputDocument.CreateNavigator();
xslt.Transform(infoPathDocument, writer);
Word.Application wordApplication =
Word.Document oDoc =
oDoc = wordApplication.Documents.Add(
wordApplication.Selection.Range.InsertXML(outputDocument.OuterXml,
wordApplication.Visible =