Triggering auto_open

A

avi

Hello,

With VBA I open a file which has am Auto_open macro but it looks that
it is not triggered as it would be if the file is opened directly (not
with VBA)

Is the a way to trigger it while opening through VBA?

Thanks
Avi
 
C

Chris Smith

You can use the Workbook_Open event, which goes in the ThisWorkbook module.

Auto_Open is a legacy throwback, and rarely used anymore anyway.

HTH
 
G

Gord

Instead of Auto_Open in a general module place the code in
Thisworkbook module as workbook_open event


Private Sub Workbook_Open()
MsgBox "hello"
End Sub


Gord Dibben Microsoft Excel MVP
 
J

Jim Rech

Without disagreeing with the wisdom previously offered, this is how you can
invoke Auto_Open:

Workbooks([Workbook name]).RunAutoMacros xlAutoOpen
 
G

GS

avi expressed precisely :
Hello,

With VBA I open a file which has am Auto_open macro but it looks that
it is not triggered as it would be if the file is opened directly (not
with VBA)

Is the a way to trigger it while opening through VBA?

Thanks
Avi

Avi,
When you open a workbook using Sub Auto_Open manually it behaves as
expected. When you open a workbook via automation you need to tell the
workbook (and Excel) to use Sub Auto_Open as in the following example.

Dim sPath As String
sPath = ThisWorkbook.Path
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

Workbooks.Open sPath & g_sAPP_FILE
Workbooks(g_sAPP_FILE).RunAutoMacros xlAutoOpen
 
A

avi

avi expressed precisely :





Avi,
When you open a workbook using Sub Auto_Open manually it behaves as
expected. When you open a workbook via automation you need to tell the
workbook (and Excel) to use Sub Auto_Open  as in the following example.

  Dim sPath As String
  sPath = ThisWorkbook.Path
  If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

  Workbooks.Open sPath & g_sAPP_FILE
  Workbooks(g_sAPP_FILE).RunAutoMacros xlAutoOpen

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc

Thanks to all of you

Looks that Garry's approach is the right one

Best regards
Avi
 
G

GS

It happens that avi formulated :
Thanks to all of you

Looks that Garry's approach is the right one

Best regards
Avi

Avi,
Thanks for the feedback! Glad it worked for you.

<FYI>
Not meaning to discount any of the advice given by others (who are
cleary more experienced than me), but I've run into too many projects
that fail miserably using code under ThisWorkbook whenever the
slightest thing that can go wrong does go wrong. Not saying it's code
errors per se, just that for some reasons known only to Excel the files
get corrupted and things go awry. I was advised to use the older
Auto_Open/Auto_Close routines by Rob Bovey some years back and so have
never had an issue ever since. Also, every client project I fix that
way also has never had their issues repeat.

So while the RunAutoMacros method may indeed be legacy, IMO it still
works better than the newer event procedures.
</FYI>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top