Save in PresentationBeforSave event

Add-in Express™ Support Service
That's what is more important than anything else

Save in PresentationBeforSave event
 
Ishaat Zahidi


Guest


We are attempting to save a Powerpoint file from PresentationBeforeSave event, however, this results in an exception as mentioned in following discussions.

https://www.add-in-express.com/forum/read.php?FID=5&TID=5477
https://www.add-in-express.com/forum/read.php?FID=5&TID=8531

So we tried to use SendMessage/OnSendMessage combination as suggested in those discussions. We are passing the presentation object reference in the SendMessage and performing operation on that object in OnSendMessage handler. However this results in an exception in OnSendMessage: "COM object that has been separated from its underlying RCW cannot be used".

It appears the COM object has already been released. Code looks like this:

private void adxPowerPointAppEvents_PresentationBeforeSave(object sender, ADXHostBeforeActionEventArgs e)
{
try
{
PowerPoint.Presentation pre = e.HostObject as PowerPoint.Presentation;
e.Cancel = true;
GCHandle handle = GCHandle.Alloc(pre);
IntPtr parameter = (IntPtr)handle;

this.SendMessage(MESSAGE_SAVE_PPT, parameter, IntPtr.Zero);
}
catch(Exception ex)
{
Console.WriteLine("Exception while saving powerpoint file : " +ex.Message);
}
return;
}

private void AddinModule_OnSendMessage(object sender, AddinExpress.MSO.ADXSendMessageEventArgs e)
{
if (e.Message == MESSAGE_SAVE_PPT)
{
try
{
GCHandle handle = (GCHandle)e.WParam;
PowerPoint.Presentation pre = (handle.Target as PowerPoint.Presentation); // Exception Here
pre.SaveAs(pre.FullName, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue);
...
}
catch (Exception ex)
{
}
}

Am I missing something here?
Posted 12 Sep, 2017 09:13:01 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ishaat,

First, It isn't recommended to cache COM objects. Instead, you can cache information required for creation of a new COM object instance when required. For example, since the indexer of the Presentation collection - Presentions.Item() in VB.NET and Presentions[] in C# - uses the full presentation name as an index to create a COM object representing a given presentation, you only need to store the full name.

Second. Add-in Express releases most COM objects passed to an event handler right after the event handler is completed. Please re-check section Releasing COM objects at https://www.add-in-express.com/docs/net-office-tips.php#releasing.


Andrei Smolin
Add-in Express Team Leader
Posted 13 Sep, 2017 04:50:09 Top
Ishaat Zahidi


Guest


Thank you. Would OnSendMessage handler run in the main thread of PowerPoint(similar to Outlook) application?
Posted 14 Sep, 2017 13:45:56 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Ishaat,

The OnSendMessage event may occur on the main thread only. This doesn't depend on the Office application used.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Sep, 2017 02:28:25 Top