SaMuTa
SaMuTa

Reputation: 11

Office Interop Word error with Sharepoint 2013

I developed a webpart that creates a Word document and exports it to PDF format using ExportAsFixedFormat. It worked in my development Sharepoint server. When I deployed it to the production server, I'm getting this error:

[COMException (0x80004005): Word has encountered a problem.]

Microsoft.Office.Interop.Word.DocumentClass.ExportAsFixedFormat(String OutputFileName, WdExportFormat ExportFormat, Boolean OpenAfterExport, WdExportOptimizeFor OptimizeFor, WdExportRange Range, Int32 From, Int32 To, WdExportItem Item, Boolean IncludeDocProps, Boolean KeepIRM, WdExportCreateBookmarks CreateBookmarks, Boolean DocStructureTags, Boolean BitmapMissingFonts, Boolean UseISO19005_1, Object& FixedFormatExtClassPtr) +0
test.PdfExporter.WebParts.PrintPDF.<>c__DisplayClass4_0.b__0() +10785
Microsoft.SharePoint.<>c__DisplayClass5.b__3() +349
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +197
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +423
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +95
test.PdfExporter.WebParts.PrintPDF.PrintPDFUserControl.generateWodDoc(ServiceConfermationList serviceConfermationList) +158
test.PdfExporter.WebParts.PrintPDF.PrintPDFUserControl.<btnPrint_Click>b__3_0() +2075
Microsoft.SharePoint.<>c__DisplayClass5.b__3() +349
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +197
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +423
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +95
test.PdfExporter.WebParts.PrintPDF.PrintPDFUserControl.btnPrint_Click(Object sender, EventArgs e) +94
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +99
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2749

This is an example of the code used:

using Microsoft.Office.Interop.Word;

// Create a new Word application
Application wordApp = new Application();

// Create a new document
Document doc = wordApp.Documents.Add();

// Insert content into the document
Paragraph para = doc.Content.Paragraphs.Add();
para.Range.Text = "Hello, World!";

// Export the document as PDF
string pdfFileName = "example.pdf";
doc.ExportAsFixedFormat(pdfFileName, WdExportFormat.wdExportFormatPDF);

// Clean up
doc.Close();
wordApp.Quit();

Notes:

  1. I used the wwwroot path as the destination for the saved file
  2. I use Office 2013.

Can you help me solve the issue that I got?

Upvotes: 0

Views: 120

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

If you need to deal with open XML documents only you may consider using the Open XML SDK instead, see Welcome to the Open XML SDK 2.5 for Office for more information. Otherwise, consider using any third-party components designed for the server-side execution.

Upvotes: 0

Related Questions