2

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.

Shakti saxena
  • 183
  • 2
  • 19
  • 1
    You could hide the workbook, read the name and then close/unhide it as required? – Siddharth Rout Nov 22 '13 at 19:05
  • @SiddharthRout Rout:how do we hide the workbook? is there any direct way of doing it? can you please provide a code snippet? – Shakti saxena Nov 24 '13 at 08:27
  • http://stackoverflow.com/questions/11354456/hiding-active-workbook-programmatically-in-excel – Siddharth Rout Nov 24 '13 at 08:31
  • my intention is to get the workbook name before it is actually opened up.It doesn't help. – Shakti saxena Nov 24 '13 at 09:02
  • When you execute your above code, do you get the `"Shakti " + " " + Wb.Name` messagebox? – Siddharth Rout Nov 24 '13 at 09:04
  • yeah, but it shows up when workbook is visible to me... my intention is that workbook should not be at all visible.. rather intercept this with a dialog box and ask user if he really wants to open workbook or close it just like that. – Shakti saxena Nov 24 '13 at 09:28
  • 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 close/unhide it as required. – Siddharth Rout Nov 24 '13 at 09:31
  • is there a way to hide the workbook? there is no workbook hidden or visible property in C#? it might be possible in VBA but in interop, I guess its not possible.. or may be I am not able to find it.:( – Shakti saxena Nov 24 '13 at 09:35

3 Answers3

0

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...

Black.Jack
  • 1,878
  • 2
  • 22
  • 39
0

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;
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I would mark this as an answer bec it helped me to some extent but not completely. Still window containing workbook is visible and I intercept it when it gets opened then hide it. I am not able to find any provision to have it not visible and then prompt user if really likes to open it. Is there a way out that I can have late loading of workbook window?? – Shakti saxena Nov 24 '13 at 12:13
  • `Is there a way out that I can have late loading of workbook window??` Not that I am aware of... – Siddharth Rout Nov 24 '13 at 12:18
0

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?

Kaz
  • 1,324
  • 9
  • 20