Search code examples
c#vbaexcelvstoshared-addin

Extracting Excel workbook name before it actually opens


I am trying to get the name of the workbook before it actually opens up.

((Excel.AppEvents_Event)this.Application).WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(App_WorkBookOpen);



private void App_WorkBookOpen(Excel.Workbook Wb)
            {
                System.Windows.Forms.MessageBox.Show("Shakti " + " " + Wb.Name);

            }

With the handler as shown above, Excel application shows the workbook name when it is opened completely.My intention is to do some formal check before it is actually opened up and data is shown to the user.

Is there any way or mechanism to extract the file name before the contents are loaded on to Excel and shown to the user? Any sort of help is highly appreciated.Thanks.


Solution

  • AFAIK you can't do that. But like I mentioned in my comment you could hide the workbook the moment it is visible. So the user will see the workbook open for a split second and then go invisible. In that split second you can read the name of the workbook and then hide the workbook.

    Based on your calculations/conclusion you can then close/unhide the workbook as required.

    You can hide the workbook using

    Wb.Windows[1].Visible = false;