web-dev-qa-db-ja.com

Wordドキュメントを開いて変更する

「Microsoft.Office.Interop.Word」を使用して、サーバーに保存されたWordファイルを開きたい。これは私のコードです:

    object missing = System.Reflection.Missing.Value;
    object readOnly = false;
    object isVisible = true;
    object fileName = "http://localhost:52099/modelloBusta/prova.dotx";
    Microsoft.Office.Interop.Word.ApplicationClass applicationWord = new Microsoft.Office.Interop.Word.ApplicationClass();
    Microsoft.Office.Interop.Word.Document modelloBusta = new  Microsoft.Office.Interop.Word.Document();

    try
    {

        modelloBusta = applicationWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref  missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing, ref missing, ref missing, ref missing);
        modelloBusta.Activate();



    }
    catch (COMException eccezione){
        Console.Write(eccezione);
        modelloBusta.Application.Quit(ref missing, ref missing, ref missing);

    }

Windowsタスクマネージャーにはプロセスは存在しますが、「Word文書」は表示されません(アプリケーションは起動しません)。何が問題ですか?前もって感謝します。

18
ilamaiolo

そのようなWordを自動化するときは、Wordアプリケーションウィンドウが実際に表示されるようにする必要があります。

var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
21
Dirk Vollmar

最初にリソースに直接追加してoffice.interopのdllを追加してから、これを使用してディレクティブを追加します。

using Microsoft.Office.Interop.Word;

次のコードを使用します

Application ap = new Application();
Document document = ap.Documents.Open(@"C:\invoice.docx");;
7
sulman

http://support.Microsoft.com/kb/257757

Officeは不安定な動作を示したり、/この環境でOfficeを実行すると、デッドロックが発生します。

http://freeword.codeplex.com/

Document document = new Document();
document.LoadFromFile("test.doct");
5
PeterHapen