Sunday, January 17, 2010

How to use Office 2007 Automation in Windows 2008 service

I’ve lost approximately 3 days about this topic. I read tens of posts by finally I’ve understood…

I’m working on a solution including a file conversion from a document in MS Office 2003 format (doc, xls, ppt) to PDF.

To do this I’ve seen several products base on a windows service which utilize MS Office 2007 or Open Office capabilities to export your documents in PDF format.

I’m not very satisfy to accomplish the requirements with a product, so I started to investigate different examples to create my custom solution.

I started with the following projects hosted on “The Code Project” portal.

While the first example is pretty easy to try (the only trouble is to find the correct version of Open Office to install :-)), the second hides unpredictable scenarios. In fact, after the test of the Office automation using the simple application form in the example, I try to export the code in a windows service process. The code is the same you can find in this MSDN article:

   1: try
   2: {
   3:     // Open the source document.
   4:     wordDocument = wordApplication.Documents.Open(
   5:         ref paramSourceDocPath, ref paramMissing, ref paramMissing,
   6:         ref paramMissing, ref paramMissing, ref paramMissing,
   7:         ref paramMissing, ref paramMissing, ref paramMissing,
   8:         ref paramMissing, ref paramMissing, ref paramMissing,
   9:         ref paramMissing, ref paramMissing, ref paramMissing,
  10:         ref paramMissing);
  11:  
  12:     // Export it in the specified format.
  13:     if (wordDocument != null)
  14:         wordDocument.ExportAsFixedFormat(paramExportFilePath,
  15:             paramExportFormat, paramOpenAfterExport, 
  16:             paramExportOptimizeFor, paramExportRange, paramStartPage,
  17:             paramEndPage, paramExportItem, paramIncludeDocProps, 
  18:             paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 
  19:             paramBitmapMissingFonts, paramUseISO19005_1,
  20:             ref paramMissing);
  21: }
  22: catch (Exception ex)
  23: {
  24:     // Respond to the error
  25: }
  26: finally
  27: {
  28:     // Close and release the Document object.
  29:     if (wordDocument != null)
  30:     {
  31:         wordDocument.Close(ref paramMissing, ref paramMissing,
  32:             ref paramMissing);
  33:         wordDocument = null;
  34:     }
  35:  
  36:     // Quit Word and release the ApplicationClass object.
  37:     if (wordApplication != null)
  38:     {
  39:         wordApplication.Quit(ref paramMissing, ref paramMissing,
  40:             ref paramMissing);
  41:         wordApplication = null;
  42:     }
  43:     
  44:     GC.Collect();
  45:     GC.WaitForPendingFinalizers();
  46:     GC.Collect();
  47:     GC.WaitForPendingFinalizers();
  48: }

I need to tell you that my dev environment is based on Windows Server 2008 (x86) with the latest SP. When I tried to convert the first doc file using the service, I receive the bitter newness. Despite the code were the same, convert process failed. Debugging, I found out that Word could not open the file and returned a null object (see row 13 in above code block).
Google searching response, further the “Microsoft does not support Office server side automation (for version till 2007 – see here)” notice, says that other guys experienced in same problem using Windows 2008 services (see this post). So, I tried the same custom service in a virtual machine with Windows Server 2003 and all went well. With an in deep search I’ve found other very usefully posts regarding Windows 2008 Server family lack about a particular folders. In detail:
  • for x64 system is necessary to create the following folder:
    C:\Windows\SysWOW64\config\systemprofile\Desktop
  • for x86 system:
    C:\Windows\System32\config\systemprofile\Desktop

After this little tip, also Windows Server 2008 service resumed to work well providing the Office automation desired.

References:
kick it on DotNetKicks.com
Save to delicious 0 saves

6 comments:

  1. Hi,

    This solved big problem for me!!

    Thanks

    ReplyDelete
  2. Great post: Problem solved -> Thank you :-)

    ReplyDelete
  3. Wow, many many thanks :-)
    This post is actually a few years old, but it saved me !

    ReplyDelete
  4. Acetech has many years of experience in custom software development. Find out more about custom software development at http://www.acetechindia.com

    ReplyDelete