Sergey Anisimov
Sergey Anisimov

Reputation: 895

Display MS Word document in window for editing

 Hello, I am building windows C# WPF application which communicates with Microsoft Office word document (.docx). The app should update .docx template file with user input and this step is achived successfuly using OpenXML. The other part of the app is to show the edited word document to the user inside of the applicaion window or using MS Word and allow him to add some more information if he wants to do it.
 The problem I am facing is: I should disable my application controls while word document is opened and I should enable them once the word is closed, also I want to know if the word app was saved (if a user made changes).
 The next code is button click event for openning word document:

using System.Windows;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;

public class MainWindowViewModel : BaseViewModel
{
    ...
    ... some view model initialization
    ...
    public bool AreControlsEnabled { get; set; } = true;

    private void OpenWord ()
    {
        AreControlsEnabled = false;

        var app = new Application()
        {
            Visible = true
        };

        var doc = app.Documents.Open("pathtofile.docx");
        var docClass = app.ActiveDocument as DocumentClass;
        docClass.DocumentEvents2_Event_Close += DocClass_DocumentEvents2_Event_Close;
        docClass.DocumentEvents_Event_Close += DocClass_DocumentEvents_Event_Close;
        app.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(DocBeforeClose);
        app.DocumentBeforeSave += new ApplicationEvents4_DocumentBeforeSaveEventHandler(DocBeforeSave);
    }

    private void DocClass_DocumentEvents2_Event_Close ()
    {
        MessageBox.Show("DocClass_DocumentEvents2_Event_Close");
        AreControlsEnabled = true;
    }

    private void DocClass_DocumentEvents_Event_Close ()
    {
        MessageBox.Show("DocClass_DocumentEvents_Event_Close");
        AreControlsEnabled = true;
    }

    private void DocBeforeClose (Document doc, ref bool cancel)
    {
        MessageBox.Show("DocBeforeClose");
        AreControlsEnabled = true;
    }

    private void DocBeforeSave (Document doc, ref bool SaveAsUI, ref bool cancel)
    {
        MessageBox.Show("DocBeforeSave");
        AreControlsEnabled = true;
    }
}

 When I run the code - I see opened MS Word document as expected, but when I close it or save - no one of events fired and I can't understand why. Also, I can use System.Diagnostics.Process to launch the Word and add exit event to it, but in this way I can't know if the user applied some changes. So, if someone solved this problem, help me please.
  Thank you for reading and answers

Upvotes: 0

Views: 414

Answers (1)

Markus
Markus

Reputation: 2281

You can:

  • Get the Current Changed Date of the File
  • Use System.Diagnostics.Process to start Word.
  • After The process ends you check the Changed Date again
  • If the user saved the file the Changed Date is updated

I don't know if the process is still running if the user just closes the document but not word. For this you could observe the Folder of the Dokument for thos ~... Temp Files Word creates while a document is open...

Upvotes: 1

Related Questions