0

Wordファイルを読み取り、処理を行ってからPDFファイルとして保存するVisualStudioプロジェクトがあります。コードは、Office 2010のみがインストールされている私のマシンでは完全に機能しましたが、Office2003とOffice2010の両方がインストールされている別のPCで実行すると、Document.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);
}
4

1 に答える 1

2

1 台のコンピューターに2 つのバージョンの Office をインストールすることは、非常に疑わしい方法です。特に適切に機能しなくなったのは、自動化インターフェースです。Office のすべてのバージョンは、アプリケーションやドキュメントなどのコア インターフェイスに同じ Guid を使用します。ただし、COM インターフェイスの登録はグローバルであり、マシン全体でインターフェイスの実装は 1 つしか存在できません。

ここで何が間違っているのかは明らかです。2010 ではなく、Office 2003 と話しているのです。Document.SaveAs2() メソッドはありません。

2003 と 2010 の両方をアンインストールし、2010 を再インストールして、この問題を解決する必要があります。または、2003 で利用できない方法を使用しないでください。

于 2012-08-31T11:52:20.863 に答える