[英]Prevent termination of Word Application when user closes last document
I have a simple Win Forms application that consists of a Button
control, an OpenFileDialog
component and a Microsoft.Office.Interop.Word.Application
object: 我有一个简单的Win Forms应用程序,该应用程序由
Button
控件, OpenFileDialog
组件和Microsoft.Office.Interop.Word.Application
对象组成:
using System;
using System.Windows.Forms;
namespace WordInterop
{
public partial class Form1 : Form
{
// From Form1.Designer.cs:
//Button openButton;
//FileOpenDialog openFileDialog;
private Microsoft.Office.Interop.Word.Application _wordApp;
public Form1()
{
InitializeComponent();
_wordApp = new Microsoft.Office.Interop.Word.Application();
}
protected override void OnClosed(EventArgs e)
{
_wordApp.Quit();
base.OnClosed(e);
}
private void _openButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK) {
// Throws COMException if Word app is not longer running
_wordApp.Documents.Open(openFileDialog.FileName);
_wordApp.Visible = true;
}
}
}
}
When the user clicks the Open
button, they can selected a Word document via the OpenFileDialog
. 当用户单击“
Open
按钮时,他们可以通过OpenFileDialog
选择一个Word文档。 The filename of the selected document is used to open the Word Document using the Microsoft.Office.Interop.Word.Application
object. 所选文档的文件名用于使用
Microsoft.Office.Interop.Word.Application
对象打开Word文档。
It appears that the Word application is terminated when the user closes the last document: If the user clicks the Open
button, selects a document, closes the document, clicks the Open
button and selects a document a second time, a COMException
occurs with the message The RPC server is unavailable . 当用户关闭最后一个文档时,似乎Word应用程序终止了:如果用户单击“
Open
按钮,选择一个文档,关闭该文档,再单击“ Open
按钮,然后再次选择一个文档,则会出现COMException
消息RPC服务器不可用 。 If there is at least one document open, the user can open documents without a COMException
occurring. 如果打开了至少一个文档,则用户可以打开文档而不会发生
COMException
。
Is there a way I can prevent Word application from terminating when the user closes the last document? 有没有一种方法可以防止用户关闭最后一个文档时Word应用程序终止?
I tried creating a hidden blank document by changing the Form1
constructor to the following: 我尝试通过将
Form1
构造函数更改为以下内容来创建隐藏的空白文档:
public Form1()
{
InitializeComponent();
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.Documents.Add(Visible: false);
}
However the blank document becomes visible when the user opens a document meaning it is still possible for user terminate the Word application. 但是,当用户打开文档时,空白文档变得可见,这意味着用户仍然可以终止Word应用程序。
Create a new instance of Microsoft.Office.Interop.Word.Application() on the button click. 单击按钮上,创建一个Microsoft.Office.Interop.Word.Application()的新实例。
private void _openButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK) {
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.Documents.Open(openFileDialog.FileName);
_wordApp.Visible = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.