tags:

views:

36

answers:

3

Hi All,

I have some code I hope someone may be able to help me with. What I'm trying to achieve is to convert an Infopaths XML based form into a Word 2007 document using an embedded XSLT file to do the transformation.

Code:

XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldProject = nav.SelectSingleNode("//my:Project", NamespaceManager).Value;
string fieldQuote = nav.SelectSingleNode("//my:QuoteNumber", NamespaceManager).Value;
string fieldDate = nav.SelectSingleNode("//my:Date", NamespaceManager).Value;


// Define variables for the word template to use and file to create
string wordTemplateFilePath = @"\\2003server\common\OIF Proposals\TemplateFile\Interiors Letter Template.docx";
string wordPrintFilePath = @"\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - " + fieldProject + " - " + fieldQuote + " - " + fieldDate + ".docx";

// Copy the template to create a new docx file
File.Copy(wordTemplateFilePath, wordPrintFilePath, true);

// Crack open the package
Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite);

// Retrieve the document.xml part of the new docx file
PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));

// Retrieve the xsl to use to transform the InfoPath form into document.xml
XslCompiledTransform trans = new XslCompiledTransform();
Stream xslTemplate = this.Template.OpenFileFromPackage("transform.xsl");
XmlReader xslTemplateReader = XmlReader.Create(xslTemplate);
trans.Load(xslTemplateReader);

// Create a StreamWriter to be able to write to the stream of the part
using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
{
    // Transform the InfoPath form and save the XML into the stream for the part
    trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);

    // Close the stream of the part
    partStream.Close();
}

// Write changes to the package
packWordPrint.Flush();

// Close the package
packWordPrint.Close();

And I'm getting the following error about 1 in 8 times:

System.IO.IOException
The process cannot access the file '\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - ABC123 - 123 - 2010-08-19.docx' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync)
   at MS.Internal.IO.Zip.ZipArchive.OpenOnFile(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.ZipPackage..ctor(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess)
   at OIF_Order_Images.FormCode.CTRL31_5_Clicked(Object sender, ClickedEventArgs e)
   at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
   at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

There must be a sensible way to handle or prevent the error, just can't get my head around it.

I guess I must not be closing a stream somewhere properly?

Any pointers / changes to the code greatly appreciated.

Many thanks

Rich

A: 

It's best to use using to make sure that the package is closed:

using (Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite))
{
    ...
}

and remove the packWordPrint.Close(); statement.

0xA3
Brilliant, thanks the suggestion
Richard
A: 

Refactoring your code to be a little more efficient and more deterministic on file closures. This should help. note: I re-factored in the SO editor so there could be typos, missing braces etc..

Also, I wasn't sure what else you were using packWordPrint for but does it really need to be opened ReadWrite? It looks like you are just Reading from it so opening it ReadOnly would be smarter and you wouldn't need to call to Flush at the end.

XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldProject = nav.SelectSingleNode("//my:Project", NamespaceManager).Value;
string fieldQuote = nav.SelectSingleNode("//my:QuoteNumber", NamespaceManager).Value;
string fieldDate = nav.SelectSingleNode("//my:Date", NamespaceManager).Value;

// Retrieve the xsl to use to transform the InfoPath form into document.xml
Stream xslTemplate = this.Template.OpenFileFromPackage("transform.xsl");
XmlReader xslTemplateReader = XmlReader.Create(xslTemplate);

XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(xslTemplateReader);

// Define variables for the word template to use and file to create
string wordTemplateFilePath = @"\\2003server\common\OIF Proposals\TemplateFile\Interiors Letter Template.docx";
string wordPrintFilePath = string.Format(@"\\2003server\common\OIF Proposals\Ben Johnson Ltd Furniture Proposal - {0} - {1} - {2}.docx",fieldProject, fieldQuote,fieldDate);

// Copy the template to create a new docx file
File.Copy(wordTemplateFilePath, wordPrintFilePath, true);

// Crack open the package
using (Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite))
{

  // Retrieve the document.xml part of the new docx file
  PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));

  // Create a StreamWriter to be able to write to the stream of the part
  using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
  {
      // Transform the InfoPath form and save the XML into the stream for the part
      trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);
  }

  // Write changes to the package
  packWordPrint.Flush();

}
Foovanadil
Thanks Foovanadil, no spelling mistakes, I need the readwrite as thats the file I'm performing the transform on. I define the variable from the form for naming purposes, define the path and file name for the new doc from the variables, open the xsl doc from the form resources which I'll use to do the transform, copy a word 2007 doc that I'm using as the template, unpack it to find the document.xml in it which is the file I'm going to transform, and write the changes to it. Without write permission I get an error as I can't write to the document.xml. Thanks though, other suggestions implemented
Richard
A: 

I think the issue was actually AV. I still got an IOException error when using the solutions above, both of which I'm going to keep implemented as their correct in their own rights but as soon as I put the write directory in the exceptions list in the AV scanner I stopped getting errors.

Richard