0

I'm receiving the below error message when saving the mail merge document using C#, but while using office 2007. The save method works on versions of office newer than 2007. I receive that error message on the line with this command:

_wordApp.ActiveDocument.SaveAs2(string.Format(path + "\\temp\\" + @"MailMerge.docx"));

Any assistance would be greatly appreciated.

Error Message: The server threw an exception (Exception from HRESULT: 0X90010105 (RPC_E_SERVERFAULT))

Code:

 try
 {
     statusLabelStrip.Text = "Currently cleaning up data.  Please wait.";

     _wordApp.Visible = false;
     _wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

     oDoc.MailMerge.MainDocumentType = Microsoft.Office.Interop.Word.WdMailMergeMainDocType.wdMailingLabels;
     oDoc.MailMerge.OpenDataSource(csvFileName, false, false, true, false);


     oDoc.MailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
     oDoc.MailMerge.Execute(false);
     oDoc.MailMerge.DataSource.Close();
     oDoc.MailMerge.MainDocumentType = Microsoft.Office.Interop.Word.WdMailMergeMainDocType.wdNotAMergeDocument;
     oDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);// this will save the template if changed
    // oDoc.Save(); // this will save the template
    // oDoc = null;
 }

 catch (Exception ex)
 {
     MessageBox.Show("Office Version: " + sVersion + "\r\n" + "Process Error 3: " + 
ex.Message + "\r\n" + ex.StackTrace, "Mail Merge Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
 finally
 {
     try 
     {
         if (_wordApp != null)
         {
             //_wordApp.Documents.Save(true); //this will prompt the SaveAs box
             //_wordApp.ActiveDocument.MailMerge.Execute();

             _wordApp.ActiveDocument.SaveAs2(string.Format(path + "\\temp\\" + @"MailMerge.docx"));
             _wordApp.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges);
             System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_wordApp);
stuartd
  • 70,509
  • 14
  • 132
  • 163
Jelani
  • 705
  • 6
  • 25
  • 1
    [Msdn](http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas2.aspx) - "This method [SaveAs2] appears in IntelliSense in Word 2007 projects that target the .NET Framework 4. However, this property cannot be used in Word 2007 projects" – stuartd Mar 12 '16 at 17:43
  • @stuartd Thank you :) More on what stuartd said - You have to use SaveAs("filename.docx") method for office 2007, even though Intellisense doesn't show that method as available. – Jelani Mar 12 '16 at 19:07

0 Answers0