Cannot be canceled with PowerPoint event handler
I want to perform a custom process before saving the presentation and I want to cancel the standard process.
The event handler is called and flagged as cancel, but PowerPoint doesn't recognize it.
For example, if you save as, the Save As dialog will appear.
In Excel and Word, the same description works fine.
Also, I have confirmed that the VSTO add-in cancels correctly.
Please help someone to solve it..
Postscript.
I tried writing similar code in C++(native), but it worked the same.
The old methods (e.g.WindowBeforeRightClick) can be canceled as expected, and the cancellation of the methods added in the PowerPoint version upgrade does not seem to be recognized.
==========================
NG Source code (Visual Studio 2019 WPF(.NET Framework 4.7.2))
Reference settings
Microsoft Office 16.0 Object Library
Microsoft PowerPoint 16.0 Object Library
==========================
using System.Windows;
using Microsoft.Office.Interop.PowerPoint;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private Microsoft.Office.Interop.PowerPoint.Application pptApp;
public MainWindow()
{
InitializeComponent();
pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
pptApp.PresentationBeforeSave += PresentationBeforeSaveEventHandler;
pptApp.WindowBeforeRightClick += WindowBeforeRightClickEventHandler;
pptApp.Presentations.Open(@"c:\temp\powerpoint.pptx");
}
private static void PresentationBeforeSaveEventHandler(Presentation Pres, ref bool Cancel)
{
// Cancellation does not work as expected.
Cancel = true;
}
private static void WindowBeforeRightClickEventHandler(Selection Sel, ref bool Cancel)
{
// Cancellation works as expected.
Cancel = true;
}
}
}
==========================
OK Source code (VSTO PPT AddIn)
==========================
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace PowerPointAddIn1
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void PresentationBeforeSaveEventHandler(PowerPoint.Presentation Pres, ref bool Cancel)
{
Cancel = true;
}
#region VSTO で生成されたコード
/// <summary>
/// デザイナーのサポートに必要なメソッドです。
/// このメソッドの内容をコード エディターで変更しないでください。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.PresentationBeforeSave += PresentationBeforeSaveEventHandler;
}
#endregion
}