C# Document.SaveAs2()给出了COMException(0x80020003):在同时安装Office 2003和Office 2010时未找到成员异常

C# Document.SaveAs2()给出了COMException(0x80020003):在同时安装Office 2003和Office 2010时未找到成员异常,c#,visual-studio-2010,office-interop,word-2010,word-2003,C#,Visual Studio 2010,Office Interop,Word 2010,Word 2003,我有一个VisualStudio项目,它读取Word文件,进行一些处理,然后将其保存为PDF文件。这段代码在我只安装了Office 2010的机器上运行得很好,但是当我在另一台同时安装了Office 2003和Office 2010的电脑上运行时,SaveAs2()抛出以下异常 System.Runtime.InteropServices. COMException (0x80020003): Member not found. (Exception from HRESULT: 0x800200

我有一个VisualStudio项目,它读取Word文件,进行一些处理,然后将其保存为PDF文件。这段代码在我只安装了Office 2010的机器上运行得很好,但是当我在另一台同时安装了Office 2003和Office 2010的电脑上运行时,SaveAs2()抛出以下异常

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)
下面是代码片段

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);
}
在一台机器上安装两个版本的Office是一种非常有问题的做法。尤其是自动化接口无法正常工作。Office的每个版本都使用相同的GUI作为核心接口,如应用程序和文档。但是COM接口的注册是全局的,在整个机器上一个接口只能有一个实现

这显然是这里的问题所在。您正在与Office 2003交谈,而不是2010。它没有Document.SaveAs2()方法


您需要通过卸载2003和2010并重新安装2010来解决此问题。或者避免使用2003年不可用的方法。

我见过当文件名格式包含斜杠(例如日期)等字符时会发生这种情况。在我的情况下不会发生这种情况,因为文件名是序列号(例如1234.docx)。还有一件事,当我使用以下代码检查版本时,结果是11。double version=double.Parse(oWord.version);感谢Hans的回复,问题是上次手动打开office的哪个版本,代码会自动获取该版本。即使在使用类型word2010ApplicationProgId=Type.GetTypeFromProgID(“Word.Application.14”);