Question

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.

Was it helpful?

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;

OTHER TIPS

No you can't.

You anyway could create a Macro on a WorkBook Module with Open class tag as here:

Private Sub Workbook_Open()

Dim ws As Workbooks

For Each ws In ActiveWorkbook.Worksheets

MsgBox ws.Name

Next

ActiveWorkbook.Worksheets.Close

End Sub

Then call this sub via c# on opening the file, this sub runs before loading the workbook then it closes it. It has not that sense, because you'll never access the wb again... Maybe with some tweaking here and there you could accomplish your task, but it depends to you.

Hope it helps...

Isn't Wb.name the same as the filename? In which case, since you must know the filename/location in order to open it, you can check it beforehand?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top