The solution that worked for me in the end is based on the article at https://theofficecontext.com/2013/04/26/updated-word-after-save-event/
For reference, I'm working with Office 2019 and below is the code that worked for me - a slightly simplified version to filter out the user clicking Don't Save and trigger a Post Save event.
using System.Threading;
using Word = Microsoft.Office.Interop.Word;
public class WordSaveHandler
{
public delegate void AfterSaveDelegate(Word.Document doc, bool isClosed);
public event AfterSaveDelegate AfterSaveEvent;
private bool preserveBackgroundSave;
private Word.Application oWord;
private string closedFilename = string.Empty;
public WordSaveHandler(Word.Application oApp)
{
this.oWord = oApp;
this.oWord.DocumentBeforeSave += this.OWord_DocumentBeforeSave;
this.oWord.WindowDeactivate += this.OWord_WindowDeactivate;
}
public string ClosedFilename
{
get
{
return this.closedFilename;
}
}
private void OWord_DocumentBeforeSave(Word.Document doc, ref bool saveAsUI, ref bool cancel)
{
this.preserveBackgroundSave = this.oWord.Options.BackgroundSave;
this.oWord.Options.BackgroundSave = true;
bool uiSave = saveAsUI;
new Thread(() =>
{
this.Handle_WaitForAfterSave(doc, uiSave);
}).Start();
}
private void Handle_WaitForAfterSave(Word.Document doc, bool uiSave)
{
bool docSaved = false;
try
{
if (uiSave)
{
while (this.IsBusy())
{
Thread.Sleep(1);
}
}
docSaved = doc.Saved;
while (this.oWord.BackgroundSavingStatus > 0)
{
Thread.Sleep(1);
}
}
catch (ThreadAbortException)
{
if (uiSave)
{
this.AfterSaveEvent(null, true);
return;
}
else
{
if (docSaved)
{
this.AfterSaveEvent(null, true);
}
return;
}
}
catch
{
this.oWord.Options.BackgroundSave = this.preserveBackgroundSave;
return;
}
try
{
if (uiSave)
{
try
{
if (doc.Saved == true)
{
this.AfterSaveEvent(doc, false);
}
}
catch
{
this.AfterSaveEvent(null, true);
}
}
else
{
try
{
this.AfterSaveEvent(doc, false);
}
catch
{
this.AfterSaveEvent(null, true);
}
}
}
catch { }
finally
{
this.oWord.Options.BackgroundSave = this.preserveBackgroundSave;
}
}
private void OWord_WindowDeactivate(Word.Document doc, Word.Window wn)
{
this.closedFilename = doc.FullName;
}
private bool IsBusy()
{
try
{
object o = this.oWord.ActiveDocument.Application;
return false;
}
catch
{
return true;
}
}
}
Close
event - this lets you catch the process before the dialog above is shown. You can then use your own save dialog and bypass Word's entirely. With that method you would have to call.Save
yourself, of course. - J...