单击开始 -> Microsoft word 2010 将新的空白文档附加到现有实例
Click on Start -> Microsoft word 2010 attaches a new blank document to existing instance
我正在 WPF 应用程序中自动化 MSWord。一切正常,但是
a 单击开始 -> Microsoft word 2010 将一个新的空白文档附加到我的实例,该实例已由 Wpf application.How 限制此行为?
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
_oApp = new Word.Application();
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
Microsoft 将此列为错误并在以下知识库文章中说明了此问题的解决方法
错误:手动启动 Word 使用与自动化相同的实例 -
https://support.microsoft.com/en-us/kb/188546
using System.Windows;
using Word = Microsoft.Office.Interop.Word;
namespace WordAutomationTestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
//To restrict the automation instance sharing with the user's documents.
//Create a temporary app instance and quit the same after the automation instance is created.
//Ref :: Go through the following work around.
//https://support.microsoft.com/en-us/kb/188546
Word.Application temp = new Word.Application(); //Create temporary instance.
_oApp = new Word.Application(); //Create automation instance.
temp.Quit(oFalse,oMissing,oMissing); //Close the temporary instance.
temp = null;
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
}
衷心感谢@Cindy Meister 帮助解决了这个问题。
我正在 WPF 应用程序中自动化 MSWord。一切正常,但是 a 单击开始 -> Microsoft word 2010 将一个新的空白文档附加到我的实例,该实例已由 Wpf application.How 限制此行为?
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
_oApp = new Word.Application();
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
Microsoft 将此列为错误并在以下知识库文章中说明了此问题的解决方法
错误:手动启动 Word 使用与自动化相同的实例 - https://support.microsoft.com/en-us/kb/188546
using System.Windows;
using Word = Microsoft.Office.Interop.Word;
namespace WordAutomationTestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
//To restrict the automation instance sharing with the user's documents.
//Create a temporary app instance and quit the same after the automation instance is created.
//Ref :: Go through the following work around.
//https://support.microsoft.com/en-us/kb/188546
Word.Application temp = new Word.Application(); //Create temporary instance.
_oApp = new Word.Application(); //Create automation instance.
temp.Quit(oFalse,oMissing,oMissing); //Close the temporary instance.
temp = null;
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
}
衷心感谢@Cindy Meister 帮助解决了这个问题。