?
Solved

Automatically fire come code when workbook is opened

Posted on 2011-09-06
10
Medium Priority
?
212 Views
Last Modified: 2012-05-12
I have an access application that exports some data out to an excel template (.xlt) and subsequently opens the template in excel.

I have a command button at the top of the excel worksheet that fires some code DoTasks() when it is clicked.

The DoTasks() procedure resizes some columns and drops a subtotal line at the end of the data.

The user pretty much always clicks the command button, so I would like to just automatically run the DoTasks() when the workbook opens and remove the command button altogether.

How can I make the DoTasks() automatically fire?  Thanks!
0
Comment
Question by:snyperj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 36491409
Let me direct your attention to this:

http://www.ozgrid.com/VBA/auto-run-macros.htm

0
 

Author Comment

by:snyperj
ID: 36491474
I cannot get it to work by putting it in the  Workbook_Open event.  I think it is because Access actually opens the template and then copies the data to it... so I think that event is firing before the data is pasted.

What other events can I use?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36491580
Use the open event. If you are automating Excel to do the export, then simply disable events before you open the file, and reenable them afterwards:

xlApp.Enableevents = false
' your code here
xlApp.Enableevents = True

Open in new window


for example.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:snyperj
ID: 36491716
Rorya- I am putting that in the access code?
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36491725
If you add a public sub to the ThisWorkbook module of your template, it is then available as a method of your workbook when you open the object from Access, and you can call it explicitly:

So in ThisWorkbook of your template:

Public Sub DoTasks()

    '  or call your own DoTasks routine....
    MsgBox "hi"

End Sub

Open in new window


Then in your Access caller:
Sub test()

    Dim p As Excel.Workbook
    
    Set p = Application.Workbooks.Add("your template path\your template.xltm")
    
    Call p.DoTasks

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36491746
Sorry, my second code snippet was created in Excel, so you would want whatever your Excel app object is called instead of Application in the Workbooks.Add call
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36491748
Wouldn't you have to declare p as Object since DoTasks is not a method of the Workbook class?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1000 total points
ID: 36491759
snyperj
Without seeing how you are doing the export, I can't really answer specifically, but yes, that needs to go in the Access code.
0
 
LVL 17

Assisted Solution

by:andrewssd3
andrewssd3 earned 1000 total points
ID: 36491783
rorya - yes interesting point - you would expect that, but it compiles as it is in Excel 2010 and works.  It would also work if you declared it as Object, which you might want to do in Access anyway of you were late binding.
0
 

Author Closing Comment

by:snyperj
ID: 36495236
I split the points, thanks for the help.  I went with another solution gleaned from the Access zone.

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27293981.html#a36492355
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question