Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / December 2005

Tip: Looking for answers? Try searching our database.

Export to Word Code - Franck

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BCLivell - 10 Dec 2005 15:03 GMT
Franck-
 
      I saw your previous post on some code that allowed you to export to
Word.  Can you help me with that code.  I followed the link that you posted.  
I inserted the code into a button and when I went to preview it, I got the
following error:

InfoPath cannot open the selected form because of an error in the form's code.
The following error occurred:

Expected ';'
File:script.js
Line:23
{using System;

Below is the code as I copied it from the link you posted.  Thank you for
your help.

function CTRL225_5::OnClick(eventObj)
{
using System;

using Microsoft.Office.Interop.InfoPath.SemiTrust;

// Add .NET reference: System.Xml

using System.Xml;

using System.Xml.Xsl;

using System.Security.Cryptography;

using System.IO;

// Add COM reference: Microsoft Word 11.0 Object Library

using Word = Microsoft.Office.Interop.Word;



// Office integration attribute. Identifies the startup class for the form.
Do not

// modify.

[assembly: System.ComponentModel.DescriptionAttribute("InfoPathStartupClass,
Version=1.0, Class=StatusReport.StatusReport")]

namespace StatusReport

{

// The namespace prefixes defined in this attribute must remain synchronized
with

// those in the form definition file (.xsf).

   
[InfoPathNamespace("xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"
xmlns:xdUtil=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Util\"
xmlns:xdXDocument=\"http://schemas.microsoft.com/office/infopath/2003/xslt/xDocument\"
xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\"
xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\"
xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"
xmlns:xd=\"http://schemas.microsoft.com/office/infopath/2003\"
xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\"
xmlns:tns=\"http://tempuri.org/\"
xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"
xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\"
xmlns:xdMath=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Math\"
xmlns:xsf=\"http://schemas.microsoft.com/office/infopath/2003/solutionDefinition\"
xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"
xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"
xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-05-24T18:10:14\"
xmlns:xdDate=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Date\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")]

   public class StatusReport

   {

       private XDocument thisXDocument;

       private Application thisApplication;

       // For encryption/decryption

       private Rijndael key = new RijndaelManaged();

       public void _Startup(Application app, XDocument doc)

       {

           thisXDocument = doc;

           thisApplication = app;

           // You can add additional initialization code here.

       }

       public void _Shutdown()

       {

       }

       // The following function handler is created by Microsoft Office
InfoPath. Do not

       // modify the type or number of arguments.

       [InfoPathEventHandler(EventType = InfoPathEventType.OnLoad)]

       public void OnLoad(DocReturnEvent e)

       {

           // Set the username

           IXMLDOMNode userName =

               thisXDocument.DOM.selectSingleNode("/my:status/my:name");

           if (userName.text == String.Empty)

           {

               userName.text = Environment.UserName;

           }

           // Load key information

           IXMLDOMNode keyNode =
thisXDocument.DOM.selectSingleNode("/my:status/my:key");

           IXMLDOMNode ivNode =
thisXDocument.DOM.selectSingleNode("/my:status/my:iv");

           byte[] keyBytes = Convert.FromBase64String(keyNode.text);

           byte[] ivBytes = Convert.FromBase64String(ivNode.text);

           // Decrypting the project name

           MemoryStream ms = new MemoryStream();

           CryptoStream csRijndael =

               new CryptoStream(ms, key.CreateDecryptor(keyBytes, ivBytes),
CryptoStreamMode.Write);

           IXMLDOMNode projectNode =

               
thisXDocument.DOM.selectSingleNode("/my:status/my:project/my:name");

           byte[] projectBytes = Convert.FromBase64String(projectNode.text);

           csRijndael.Write(projectBytes, 0, (int)projectBytes.Length);

           csRijndael.FlushFinalBlock();

           string projectName =

               System.Text.Encoding.Unicode.GetString(ms.GetBuffer(), 0,
(int)ms.Length);

           projectNode.text = projectName;

       }

       // The following function handler is created by Microsoft Office
InfoPath. Do not

       // modify the type or number of arguments.

       [InfoPathEventHandler(MatchPath = "/my:status/my:date", EventType =
InfoPathEventType.OnValidate)]

       public void date_OnValidate(DataDOMEvent e)

       {

           // Custom data validation because InfoPath does not have
functions for date arithmetic

           if (e.Operation == "Insert")

           {

               try

               {

                   DateTime date = DateTime.Parse(e.NewValue.ToString());

                   if (date.CompareTo(DateTime.Today.AddDays(5)) > 0)

                   {

                       e.ReportError(e.Site, "Date cannot be more than 5
days away.",

                                                    false, null, 1, "modal");

                   }

               }

               catch (FormatException)

               {

                   // Incorrectly formatted dates are handled automatically
by InfoPath.

               }

           }

       }

       // The following function handler is created by Microsoft Office
InfoPath. Do not

       // modify the type or number of arguments.

       [InfoPathEventHandler(EventType = InfoPathEventType.OnSubmitRequest)]

       public void OnSubmitRequest(DocReturnEvent e)

       {

           // If Online, submit using data adapter

           if (thisApplication.MachineOnlineState ==
XdMachineOnlineState.xdOnline)

           {

               WebServiceAdapter2 webServiceAdapter =

                   (WebServiceAdapter2)thisXDocument.DataAdapters["Submit"];

               webServiceAdapter.Submit();

           }

           // If Offline, save to cache

           else

           {

               XmlDocument xmlDoc = new XmlDocument();

               string now = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.ff");

               xmlDoc.PreserveWhitespace = true;

               xmlDoc.LoadXml(thisXDocument.DOM.xml);

               xmlDoc.Save("C:\\TechEd2005\\Submit\\Form " + now + ".xml");

           }

           e.ReturnStatus = true;

       }

       // The following function handler is created by Microsoft Office
InfoPath. Do not

       // modify the type or number of arguments.

       [InfoPathEventHandler(EventType = InfoPathEventType.OnSaveRequest)]

       public void OnSaveRequest(SaveEvent e)

       {

           // Set up encryption streams

           MemoryStream ms = new MemoryStream();

           CryptoStream csBase64 =

               new CryptoStream(ms, new ToBase64Transform(),
CryptoStreamMode.Write);

           CryptoStream csRijndael =

               new CryptoStream(csBase64, key.CreateEncryptor(),
CryptoStreamMode.Write);

           // Encrypt project name

           IXMLDOMNode projectNode =

               
thisXDocument.DOM.selectSingleNode("/my:status/my:project/my:name");

           byte[] projectBytes =

               System.Text.Encoding.Unicode.GetBytes(projectNode.text);

           csRijndael.Write(projectBytes, 0, (int)projectBytes.Length);

           csRijndael.FlushFinalBlock();

           string projectEncrypted =

               System.Text.Encoding.ASCII.GetString(ms.GetBuffer(), 0,
(int)ms.Length);

           // Save key information

           IXMLDOMNode keyNode =
thisXDocument.DOM.selectSingleNode("/my:status/my:key");

           IXMLDOMNode ivNode =
thisXDocument.DOM.selectSingleNode("/my:status/my:iv");

           keyNode.text = Convert.ToBase64String(key.Key);

           ivNode.text = Convert.ToBase64String(key.IV);

           // Save encrypted project name, then decrypt in view

           string projectOriginal = projectNode.text;

           projectNode.text = projectEncrypted;

           e.IsCancelled = e.PerformSaveOperation();

           projectNode.text = projectOriginal;

           thisXDocument.SetDirty(false);

           e.ReturnStatus = true;

       }

       // The following function handler is created by Microsoft Office
InfoPath. Do not

       // modify the type or number of arguments.

       [InfoPathEventHandler(MatchPath = "ButtonGenerate", EventType =
InfoPathEventType.OnClick)]

       public void ButtonGenerate_OnClick(DocActionEvent e)

       {

           // This button converts the XML Form to a Word Document

           

           // Load the XSLT file from resources

           IXMLDOMDocument domDocument = thisXDocument.CreateDOM();

           domDocument.load("StatusReport.xsl");

           XmlDocument xsltDocument = new XmlDocument();

           xsltDocument.LoadXml(domDocument.xml);

           // Load the XML DOM into System.Xml

           XmlDocument infoPathDocument = new XmlDocument();

           infoPathDocument.LoadXml(thisXDocument.DOM.xml);

           // Apply the XSLT to the XML DOM

           XslCompiledTransform xslt = new XslCompiledTransform();

           xslt.Load(xsltDocument);

           XmlDocument outputDocument = new XmlDocument();

           System.Xml.XPath.XPathNavigator outputNavigator =
outputDocument.CreateNavigator();

           using (XmlWriter writer = outputNavigator.AppendChild())

           {

               xslt.Transform(infoPathDocument, writer);

           }

           // Instantiate Word with the new document

           object missing = System.Reflection.Missing.Value;

           Word.Application wordApplication = new Word.ApplicationClass();

           Word.Document oDoc = new Word.DocumentClass();

           oDoc = wordApplication.Documents.Add(ref missing, ref missing,
ref missing, ref missing);

           
wordApplication.Selection.Range.InsertXML(outputDocument.OuterXml, ref
missing);

           wordApplication.Visible = true;

       }

 
    // Write your code here
}
Franck Dauché - 10 Dec 2005 17:25 GMT
Hi,

The code I pointed you to is Managed Code, not JScript.  You need Visual
Studio installed on your machine.

Regards,

Franck Dauché

> Franck-
>  
[quoted text clipped - 296 lines]
>
>             IXMLDOMNode projectNode =
Ben walters - 13 Dec 2005 02:12 GMT
BC
Depending on your solution it is possible to export an InfoPath form
directly to a MHT file this file can then be opened in word and saved off
again as a word document. I have developed some code that takes advantage of
this automatically however your form would need to have a full trust level to
run (e.g. either signed or regiseterd on the users machine as full trust)
If you would like a sample of this code let me know and I'll shoot it through

cheers
Ben

> Franck-
>  
[quoted text clipped - 296 lines]
>
>             IXMLDOMNode projectNode =
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.