none
Enable events C# VSTO Workbook RRS feed

  • Question

  • Hi all,

    I have created a VSTO Workbook in C# and I have some code that runs when the file opens to prepare the file. I have had a case when a user has disabled events (in VBA) and therefore when this workbook opens the code doesn't run.

    Is there a way to always enable events?

    Many thanks,

    Joe

    Thursday, June 22, 2017 8:35 AM

Answers

  • Hi JoeyStyles,

    I think EnableEvents could not be always fixed as enabled, I didn't find any property or event which could prevent somebody changing it. VSTO Workbook project need StartUp event to load it's assembly. If EbableEvents is disabled, that means all you code in the VSTO Workbook project won't work, so I think you could not do anything in the project.

    Best Regards,

    Terry

    • Marked as answer by JoeyStyles Thursday, July 20, 2017 3:38 PM
    Wednesday, June 28, 2017 10:43 AM

All replies

  • Hi JoeyStyles,

    How did the VBA code disable events? Did it set Application.EnableEvents=False? When will the VBA code run? We could set the value as true to enable events in VSTO after the VBA code ran. Here is the example.

    Excel.Application xlapp = Globals.ThisWorkbook.Application;

                xlapp.EnableEvents = true;

    Best Regards,

    Terry

    Friday, June 23, 2017 5:40 AM
  • Hi Terry,

    I think they were running another macro and it tried to debug and the EnableEvents was set to false. They then opened my VSTO and because events wasn't enable the startup event didn't fire.

    Here is my code I am using to test it when events have been disabled in another already opened workbook.

    namespace ExcelWorkbook8
    {
        
        public partial class ThisWorkbook
        {
                 
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Excel.Application xlapp = Globals.ThisWorkbook.Application;

                xlapp.EnableEvents = true;
                MessageBox.Show("You cannot stop me", "Haha",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }

            private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
            {
            }

            #region VSTO Designer 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(ThisWorkbook_Startup);
                this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
            }

            #endregion

        }
    }

    As you can see i have used your suggestion which does re-enable events but only when events are already active if they have been disabled it still doesn't fire. 

    Failing this I think forcing the workbook to open in a new instance would work? (Although not sure how to do this)

    Thanks for your help.

    Joe

    Friday, June 23, 2017 8:36 AM
  • Hi JoeyStyles,

    I can reproduce your issue, I set EnableEvents to false and then opened the workbook, nothing happened. I think this is because that EnableEvents is an application-level property which disabled all events in current application instance and your VSTO workbook need StartUp Event for loading, so your VSTO workbook failed to load your custom assembly. That means whatever you do in your VSTO workbook project, it won't work. You need force EnableEvents to true before they open your workbook. I suggest you discuss with your user how to set EnableEvents to true before opening your workbook. Opening in a new instance would work but it still need to be done by your user using code.

    Best Regards,

    Terry

    Monday, June 26, 2017 9:43 AM
  • Thanks Terry, ok so if I understand correctly there is no way I can always make sure events are enabled?

    If not would I be able to force a new instance of Excel to open when this file is opened? 

    Many thanks,

    Joe

    Tuesday, June 27, 2017 5:03 PM
  • Hi JoeyStyles,

    I think EnableEvents could not be always fixed as enabled, I didn't find any property or event which could prevent somebody changing it. VSTO Workbook project need StartUp event to load it's assembly. If EbableEvents is disabled, that means all you code in the VSTO Workbook project won't work, so I think you could not do anything in the project.

    Best Regards,

    Terry

    • Marked as answer by JoeyStyles Thursday, July 20, 2017 3:38 PM
    Wednesday, June 28, 2017 10:43 AM