Solved

Automatically fire come code when workbook is opened

Posted on 2011-09-06
10
206 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
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
Let me direct your attention to this:

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

0
 

Author Comment

by:snyperj
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:andrewssd3
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 17

Expert Comment

by:andrewssd3
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now