.net 4.0FrameworkとOffice2010でc#を使用しています
私は現在、すでに開いているWord文書を閉じる方法を見つけようとしています。このコードでエラーが発生しています:
Microsoft.Office.Interop.Word.Application app = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
foreach (Microsoft.Office.Interop.Word.Document d in app.Documents)
{
if (d.FullName.ToLower() == "OOPOR".ToLower())
{
object saveOption = WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
d.Application.Documents.Close(ref saveOption, ref originalFormat, ref routeDocument);
}
}
現在、このコードを実行すると、foreachループに入ることができません。app.Documentsは「0」のカウントを示しています。そして、foreachループにブレークポイントを設定し、「app」変数を見ると、次のように表示されます。
ActiveDocument = '((Microsoft.Office.Interop.Word.ApplicationClass)(app)).ActiveDocument' threw an exception of type 'System.Runtime.InteropServices.COMException' >>
base {System.Runtime.InteropServices.ExternalException} = {"This command is not available because no document is open."}
これは、アクティブな単語ドキュメントがある場合でも発生します。