Click here to Skip to main content
15,892,768 members
Articles / Desktop Programming / Win32

Determining if Excel is in Edit mode with Win32 Interop

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
13 Apr 2009CPOL3 min read 46.3K   742   22  
In this article, one can find a possible solution how to check or to be notified if the Excel Application is in Edit mode
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ExcelInEditModeAddin
{
    public partial class ThisAddIn
    {
        ExcelUtil.ExcelEditorModeEventPublisher _editorModeEventPublisher = null;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            #region VSTO generated code

            this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);
            _editorModeEventPublisher = new ExcelUtil.ExcelEditorModeEventPublisher(this.Application);
            _editorModeEventPublisher.EditModeOn += _editorModeEventPublisher_EditModeOn;
            _editorModeEventPublisher.EditModeOff += _editorModeEventPublisher_EditModeOff;

            // CHECK THE STATUS IMMEDIATELLY
            ReportEditorModeStatus(_editorModeEventPublisher.IsExcelInEditMode);
            #endregion

        }

        private void ReportEditorModeStatus(bool _editorMode)
        {
            string report = string.Empty;

            if (_editorMode)
                report = "Excel is in editor mode now!";
            else
                report = "Excel is NOT in editor mode now!";

            Application.StatusBar = report;

        }
        void _editorModeEventPublisher_EditModeOff(object sender, EventArgs e)
        {
            ReportEditorModeStatus(false);
        }

        void _editorModeEventPublisher_EditModeOn(object sender, EventArgs e)
        {
            ReportEditorModeStatus(true);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            _editorModeEventPublisher.EditModeOn -= _editorModeEventPublisher_EditModeOn;
            _editorModeEventPublisher.EditModeOff -= _editorModeEventPublisher_EditModeOff;

            _editorModeEventPublisher.Dispose();
            _editorModeEventPublisher = null;
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Hungary Hungary
Office (Word, Excel) specialist
C#, COM, VC++, VB6
MFC, ATL,

Comments and Discussions