Word Automation in C#. Error while using SaveAs

3

I am getting the following error when trying to Save as Document Object while trying to implement a word automation in C#:

System.Runtime.InteropServices.COMException

(0x80020005): Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

   at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object&

FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks)

   at TestWordAutomation.Form1.SaveAs(String

fileName) in D:\dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:line 246

   at TestWordAutomation.Form1.button4_Click(Object

sender, EventArgs e) in D:\dotnet\WordAutomation\TestWordAutomation\TestWordAutomation\Form1.cs:line 557

I am calling the Save As method like so:

MySaveAs("Doc1.doc");
MySaveAs("Doc2.doc");
MySaveAs("Doc3.doc");

//I have a MySaveAs function
 public void  MySaveAs(string fileName)
        {
            object FileName = null, FileFormat = null, LockComments = null, _Password = null, AddToRecentFiles = null, _WritePassword = null, _ReadOnlyRecommended = null, _EmbedTrueTypeFonts = null, SaveNativePictureFormat = null, _SaveFormsData = null, SaveAsAOCELetter = null, Encoding = null, InsertLineBreaks = null, AllowSubstitutions = null, LineEnding = null, AddBiDiMarks = null;
            FileName = (object) fileName;
            oDoc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref _Password, ref AddToRecentFiles,
                        ref _WritePassword, ref _ReadOnlyRecommended, ref _EmbedTrueTypeFonts,
                        ref SaveNativePictureFormat, ref _SaveFormsData, ref SaveAsAOCELetter, ref Encoding,
                        ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
        }

Can any one help me to resolve this ?

c#
.net
ms-word
office-interop
asked on Stack Overflow Nov 18, 2008 by (unknown user) • edited Jul 30, 2016 by Deduplicator

2 Answers

8

'Tis painful. I remember getting that working with the Word/Excel Interop. Not to be flippant, but one of the parameters has an incorrect type. Hopefully the following helps, but I did a number of these types of definitions.

static object s_missing = System.Reflection.Missing.Value;
static object s_true = true;
static object s_false = false;
static object s_forcesave = Word.WdSaveOptions.wdSaveChanges;

static Word._Application s_app = null;
...
return s_app.Documents.Open ( ref filename,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing,
   ref s_missing );
answered on Stack Overflow Nov 18, 2008 by kenny
2

You cannot pass null into any of the interop function call - use Missing.Value instead (as suggested by Kenny)

answered on Stack Overflow Jul 12, 2009 by veljkoz

User contributions licensed under CC BY-SA 3.0