PDA

View Full Version : Workbook open fail problem



plihu
12-05-2014, 01:57 AM
Hi,

I've noticed strange problem with sub Workbook_open,
here's fragment of the code


Private Sub Workbook_Open()
UserWBName = Me.Name
Sheets("Statistics").DTPicker1.Value = Now
Call Import
End Sub


What is imprtant-that Workbook_Open sets string UserWBName the name of workbook and calls procedure Import.
And here's fragment of Import (in different module)


Sub Import()
userWSName = "Statistics"
Workbooks(UserWBName).Sheets(userWSName).Cells(1, 1).Activate
End Sub

Everything is ok, and works fine but the first time when user launch this Excel file.
Then the "yellow label" shows, when user has to enable macros to make it work. If he do so-he receive Run-time error '1004' Activate method of range class failed.
And this is ONLY for the first time, because the next time - when this file is trusted, and yellow label does not show - everything works fine.


Btw I've done some testing and noticed that when I put e.g. application.wait for 2 seconds in Workbook_Open() procedure it also does not work for this first time with yellow label.
I work in big company and I can't enable all macros without notification for all users.


Any suggestions?
thanks for help!

Aflatoon
12-05-2014, 04:48 AM
First, it would be easier to just ThisWorkbook.
Second, you need to ensure that sheet is active before you can select a cell on it:

With ThisWorkbook.Sheets(userWSName)
.Activate
.Cells(1, 1).Activate
End With
Third, it would be better to call your startup code using Application.OnTime from the Open event, rather than calling it directly.

plihu
12-05-2014, 05:07 AM
I've tried it but it still does not work.
There's an error when it tries to activate Sheet - ThisWorkbook.Sheets(userWSName).Activate
And still - it only happens for the first time when the user has to accept "yellow label". Second time-macro works just fine.

Aflatoon
12-05-2014, 05:11 AM
Using OnTime, or not?

plihu
12-05-2014, 06:00 AM
Yes,
Here's how I've done it:


Private Sub Workbook_Open()
UserWBName = Me.Name
Sheets("Statistics").DTPicker1.Value = Now
Application.OnTime Now + TimeSerial(0, 0, 1), "Import"
End Sub

and the errors occurs Run-time error '1004': Method 'onTime' of object '_Application' failed.

Aflatoon
12-05-2014, 06:07 AM
Is it just the Enable Macros dialog you get, or does the workbook open in Protected View initially?

plihu
12-05-2014, 06:56 AM
Here's dialog I get.
12586
Once user click Enable Editing button it starts Workbook_Open() procedure... and fails :)

Aflatoon
12-05-2014, 07:29 AM
Ah. I think this is a bug - I have seen it myself a couple of times and logically it shouldn't happen. I'll try and get it filed if it isn't already.

plihu
12-08-2014, 12:48 AM
Ok,

that's what I thought,
Thanks for help!

SamT
12-08-2014, 05:20 AM
:dunno

Private Sub Workbook_Open()
DoEvents
Continue
End Sub


Private Sub Continue()
UserWBName = Me.Name
Sheets("Statistics").DTPicker1.Value = Now
Import
End Sub