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: }
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:- Generate PDF Using C#
- Convert Word-Documents to PDF on an ASP.NET Server
- Saving Word 2007 Documents to PDF and XPS Formats
- Considerations for server-side Automation of Office
- Word Automation through Windows service (Windows Server 2008) - Problem
- Excel 2007 automation on top of a Windows Server 2008 x64
- Word Interop Not Working In A Scheduled Task