Document.SaveAs2() gives COMException (0x80020003): Member not found exception when both Office 2003 and Office 2010 are installed

0

I have a visual studio project that read a Word file, does some processing and then saves it as a PDF file. The code worked perfectly on my machine which only had Office 2010 installed, but when I ran it on another PC which had both Office 2003 and Office 2010 installed the Document.SaveAs2() throws the following exception.

System.Runtime.InteropServices.
COMException (0x80020003): Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
at Microsoft.Office.Interop.Word.DocumentClass.SaveAs2(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, Object& CompatibilityMode)

The code snippet is below

object oMissing = System.Reflection.Missing.Value;

//Creates the needed objects (the application and the document)
Word._Application oWord = null;
Word._Document oDoc = null;

//Checks to see if the file does not exist (which would throw an error)
if (!System.IO.File.Exists(templatePath))
{
    _log.DebugFormat("The template file {0} does not exist on the path specified.", templatePath);
    throw new FileNotFoundException("The template file does not exist on the path specified.", templatePath);
}

try
{
    //Start up Microsoft Word
    oWord = new Word.Application();

    //If set to false, all work will be done in the background
    //Set this to true if you want to see what is going on in
    //the system - great for debugging.
    oWord.Visible = false;

    //Opens the Word Document
    //Parameters:
    //  templatePath = Document Name
    //  false = Don't convert conversions
    //  true = Open in Read-only mode
    //This may return null on Windows Server 2008 or Windows 7, 
    //to resolve  create a folder by the name of Desktop in the directory
    //C:\Windows\SysWOW64\config\systemprofile\Desktop, or
    //C:\Windows\System32\config\systemprofile\Desktop
    //depending on whether you have 64-bit Windows.
    oDoc = oWord.Documents.Open(templatePath, false, true);

    //Do some processing

    //Export the document to a PDF file, this function requires a default printer to be installed in the system so commenting it out
    //oDoc.ExportAsFixedFormat(pdfDocumentPath, Word.WdExportFormat.wdExportFormatPDF);

    //Save the word document as a PDF file
    oDoc.SaveAs2(pdfDocumentPath, Word.WdSaveFormat.wdFormatPDF);
}
c#
visual-studio-2010
office-interop
word-2010
word-2003
asked on Stack Overflow Aug 31, 2012 by Wasae Shoaib • edited Jan 6, 2014 by John Saunders

1 Answer

2

Installing two versions of Office on one machine is a very questionable practice. What in particular cannot work properly anymore is the automation interface. Every version of Office uses the same Guids for core interfaces like Application and Document. But registration of COM interfaces is global, there can be only one implementation of an interface on the entire machine.

Which is clearly what's going wrong here. You are talking to Office 2003, not 2010. It doesn't have a Document.SaveAs2() method.

You'll need to fix this problem by uninstalling both 2003 and 2010 and re-installing 2010. Or avoid using methods that are not available in 2003.

answered on Stack Overflow Aug 31, 2012 by Hans Passant

User contributions licensed under CC BY-SA 3.0