Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default WorkbookBeforeClose event is not canceled

Hi,

I'm developing an Office COM Add-in by using Visual C# .NET.

The purpose of the add-in is to prevent any open workbook to get closed.
To achieve this I've implemented the following handler for the
Workbook_BeforeClose event of the Excel.Application object.

private void Connect_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
{
System.Windows.Forms.MessageBox.Show("It's not allowed closing the
workbook!");
Cancel = true;
}

At runtime, closing a workbook it pops up the message AND CLOSES the
workbook. The even is not canceled.

Am I missing anything?

Thanks,
Mircea


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default WorkbookBeforeClose event is not canceled

Hi

It seems that I can not reproduce the problem.
Here is my whole test code your reference.
If you still have any problem, I think you may try to run it on another
machine to see if that works.
namespace ExcelAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;

#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish
to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary
/// The object for implementing an Add-in.
/// </summary
/// <seealso class='IDTExtensibility2' /
[GuidAttribute("BE133DE0-82CE-4EBB-AA88-517A54FCBC3F"),
ProgId("ExcelAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
/// <summary
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary
public Connect()
{
}

/// <summary
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary
/// <param term='application'
/// Root object of the host application.
/// </param
/// <param term='connectMode'
/// Describes how the Add-in is being loaded.
/// </param
/// <param term='addInInst'
/// Object representing this Add-in.
/// </param
/// <seealso class='IDTExtensibility2' /
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
exApp = application as Excel.Application;
exApp.WorkbookBeforeClose+=new
Microsoft.Office.Interop.Excel.AppEvents_WorkbookB eforeCloseEventHandler(exA
pp_WorkbookBeforeClose);
}

/// <summary
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary
/// <param term='disconnectMode'
/// Describes how the Add-in is being unloaded.
/// </param
/// <param term='custom'
/// Array of parameters that are host application specific.
/// </param
/// <seealso class='IDTExtensibility2' /
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
}

/// <summary
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary
/// <param term='custom'
/// Array of parameters that are host application specific.
/// </param
/// <seealso class='IDTExtensibility2' /
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary
/// <param term='custom'
/// Array of parameters that are host application specific.
/// </param
/// <seealso class='IDTExtensibility2' /
public void OnStartupComplete(ref System.Array custom)
{
}

/// <summary
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being
unloaded.
/// </summary
/// <param term='custom'
/// Array of parameters that are host application specific.
/// </param
/// <seealso class='IDTExtensibility2' /
public void OnBeginShutdown(ref System.Array custom)
{
}
private Excel.Application exApp=null;

private void
exApp_WorkbookBeforeClose(Microsoft.Office.Interop .Excel.Workbook Wb, ref
bool Cancel)
{
Debug.WriteLine("Before close");
System.Windows.Forms.MessageBox.Show("Before close");
Cancel = true;
}
}
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"this operation has been canceled due to restrictions in effect on stumped-in-excel[_2_] Excel Discussion (Misc queries) 2 March 14th 09 02:32 PM
question about Auto_Close and WorkbookBeforeClose Brian Murphy Excel Programming 8 October 9th 04 08:08 PM
Run Macro If Save Is Canceled On SaveAs Window In VBA Celtic_Avenger[_42_] Excel Programming 0 September 19th 04 09:30 AM
Run Macro If Save Is Canceled On SaveAs Window In VBA Celtic_Avenger[_41_] Excel Programming 2 September 19th 04 02:34 AM
Handling WorkbookBeforeClose and WorkbookBeforeSave Tom Ogilvy Excel Programming 0 September 19th 03 06:27 PM


All times are GMT +1. The time now is 03:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"