点击开始 - > Microsoft Word 2010将新的空白文档附加到现有实例

时间:2015-12-18 05:48:18

标签: wpf ms-office word-automation

我在WPF应用程序中自动化MSWord。一切都很好,但是 a单击开始 - > Microsoft Word 2010将一个新的空白文档附加到我的实例,该实例已由Wpf应用程序创建。如何限制此行为?

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);
    }
}

1 个答案:

答案 0 :(得分:0)

Microsoft将此列为错误,并在以下知识库文章中说明了此问题的解决方法

BUG:手动启动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,他帮助解决了这个问题。