Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default ActiveWorkbook is nothing in Workbook_Open

I've built an Excel add-in that needs to execute some code when the
workbook opens. I've added the following code to the Workbook_Open
event:

Private Sub Workbook_Open()

For Each ws In ActiveWorkbook.Worksheets
If ws.Name = crFinancialsSheet Then
sheetExists = True
End If
Next

End Sub

I add the add-in, close the workbook and open a new instance of Excel.
At this point, the add-in throws an error because the ActiveWorkbook in
the Workbook_Open event is nothing and obviously there are no sheets (I
can visually see that the sheets are not there).

What I'm wondering is why the ActiveWorkbook is nothing in the
Workbook_Open event and is there another event that I can put my code.
I need the workbook to have sheets.

Thanks,

alex

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default ActiveWorkbook is nothing in Workbook_Open

the workbook_open event in the addin fires when the addin is loaded. I
suspect you want to have the code fire when another workbook is opened in
Excel. If that is the case, then you need to use application level events
which can be instantiated from your addin

http://www.cpearson.com/excel/appevent.htm

for some guidance.

--
Regards,
Tom Ogilvy


" wrote:

I've built an Excel add-in that needs to execute some code when the
workbook opens. I've added the following code to the Workbook_Open
event:

Private Sub Workbook_Open()

For Each ws In ActiveWorkbook.Worksheets
If ws.Name = crFinancialsSheet Then
sheetExists = True
End If
Next

End Sub

I add the add-in, close the workbook and open a new instance of Excel.
At this point, the add-in throws an error because the ActiveWorkbook in
the Workbook_Open event is nothing and obviously there are no sheets (I
can visually see that the sheets are not there).

What I'm wondering is why the ActiveWorkbook is nothing in the
Workbook_Open event and is there another event that I can put my code.
I need the workbook to have sheets.

Thanks,

alex


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default ActiveWorkbook is nothing in Workbook_Open

Thanks, Tom...

alex

Tom Ogilvy wrote:
the workbook_open event in the addin fires when the addin is loaded. I
suspect you want to have the code fire when another workbook is opened in
Excel. If that is the case, then you need to use application level events
which can be instantiated from your addin

http://www.cpearson.com/excel/appevent.htm

for some guidance.

--
Regards,
Tom Ogilvy


" wrote:

I've built an Excel add-in that needs to execute some code when the
workbook opens. I've added the following code to the Workbook_Open
event:

Private Sub Workbook_Open()

For Each ws In ActiveWorkbook.Worksheets
If ws.Name = crFinancialsSheet Then
sheetExists = True
End If
Next

End Sub

I add the add-in, close the workbook and open a new instance of Excel.
At this point, the add-in throws an error because the ActiveWorkbook in
the Workbook_Open event is nothing and obviously there are no sheets (I
can visually see that the sheets are not there).

What I'm wondering is why the ActiveWorkbook is nothing in the
Workbook_Open event and is there another event that I can put my code.
I need the workbook to have sheets.

Thanks,

alex



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default ActiveWorkbook is nothing in Workbook_Open

Are you wanting this triggered when any workbook is opened? If so this
will require a Class Module to handle an application level event. What
is causing the error is the fact that as Excel opens, it opens the
Addins, Personal.xls, and other items in the "xlStart" directory. Your
addin runs in the background and therefore cannot be the
"ActiveWorkbook". After all the Addins are loaded then Excel creates
the new workbook, "Book1". Anyhow here is some code to get you going on
the Application Level event:
Place this code in a class module:
Option Explicit

Public WithEvents App As Application

Private Sub App_Workbook_Open()
'Your Code
End Sub

And this in a normal module
Option Explicit
Public AppClass As New EventClass

And this in the "ThisWorkbook" Object:
Private Sub Workbook_Open()
Set AppClass.App = Application
End Sub

Let me know if you need help or if I'm totally off on what you're
trying to do.

Charles Chickering

wrote:
I've built an Excel add-in that needs to execute some code when the
workbook opens. I've added the following code to the Workbook_Open
event:

Private Sub Workbook_Open()

For Each ws In ActiveWorkbook.Worksheets
If ws.Name = crFinancialsSheet Then
sheetExists = True
End If
Next

End Sub

I add the add-in, close the workbook and open a new instance of Excel.
At this point, the add-in throws an error because the ActiveWorkbook in
the Workbook_Open event is nothing and obviously there are no sheets (I
can visually see that the sheets are not there).

What I'm wondering is why the ActiveWorkbook is nothing in the
Workbook_Open event and is there another event that I can put my code.
I need the workbook to have sheets.

Thanks,

alex


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveWorkbook.RefreshAll Matt Cromer[_2_] Excel Programming 3 July 24th 06 08:45 PM
trouble getting addin's workbook_open procedure to recognize activeworkbook martin Excel Programming 6 April 30th 06 05:18 PM
ActiveWorkBook Pete Excel Discussion (Misc queries) 3 May 9th 05 04:14 PM
activeworkbook problems texastig Excel Programming 1 February 23rd 05 03:01 AM
ActiveWorkbook.RefreshAll Mark[_22_] Excel Programming 0 October 15th 03 02:22 AM


All times are GMT +1. The time now is 03:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"