How to autostart macro's at startup *EXCEL*

October 4, 2011 at 05:43:07
Specs: Windows XP
Hello,

I want to start couple of macro's at the startup of the excel (html) file.
It's a webpage (html) format.

I did this with Save As -> htm*, html* format within excel.

How can I achieve this?

Any help is welcome,

Regards,


See More: How to autostart macros at startup *EXCEL*

Report •


#1
October 4, 2011 at 06:06:15
in the VBA editior if you click on 'ThisWorbook' you can use the 'Workbook_Open' method to run your macro as soon as Excel opens.

Private Sub Workbook_Open()
    
    'Call your method here
    
End Sub


Report •

#2
October 4, 2011 at 06:15:34
Thank-you for your fast re-ply, but I already tried this with no success.
Because when I close the excel file and re-open the file, the macro does not exist anymore.

Regards,


Report •

#3
October 4, 2011 at 06:56:46
You will need to save you excel file as a 'maco enabled workbook'

this will save the macro within your Excel document.


Report •

Related Solutions

#4
October 4, 2011 at 10:52:40
Now, I'll admit that I've never tried it (until today) but I do not believe that you can run macros in a file opened via a browser.

Think about the security issues. Let's say you decided to put malicious code in a Workbook_Open macro and then stored the file on a web server or sent the link to a user.

The user opens the file in their browser, the code runs and inflicts whatever damage you intended it to. That really wouldn't be fair to the user, now would it?

I put a Workbook_Open macro in a .htm file and the only time it will run is when I open the file from within Excel.

When I open that same file in a web browser the macro does not run...and that makes me very happy.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#5
October 5, 2011 at 01:40:21
1. I dont know where you get the idea of that I would put malicious code in it.
2. I want to embed excel file\worksheet in a webpage, that's why I'm saving it as html format.

The scenario:

I have a logger program that logs energy status and logs into a .csv file.
My excel file connects to that .csv file and reads the energy value's.
I've made a macro to connect to the .csv file automaticly when I manually start it.

The problem is that I want that the macro starts automaticly when I start the specific excel (html format) file.

One another thing is that I have to embed the excel into a webbox within another program.
So it must be in htm\html format.

Regards,


Report •

#6
October 5, 2011 at 09:42:54
re: "2. I want to embed excel file\worksheet in a webpage, that's why I'm saving it as html format."

I know.

re: 1. I dont know where you get the idea of that I would put malicious code in it.

You completely missed my point.

Are you familiar with the term "hypothetical situation"?

When I said "Think about the security issues. Let's say you decided to put malicious code in a Workbook_Open macro" the "Let's say" means, hypothetically speaking, you (or even me) could put malicious code in a Workbook_Open macro that would run as soon as the user opened it.

Since Excel has an option that allows the user that is opening the workbook from within Excel to choose how embedded macros are handled (the Macro Security setting) the user has accepted the responibility of anything bad that happens to his system should he decide to let macro's run automatically.

A browser has no such option, so the user would be at risk if the html file that he opened inside a browser had malicious code embedded in it. Heck, it doesn't even have to be "malicious code". It could even be well-intentioned code that perhaps renames a file or creates a directory that the user would not want to happen if given the choice.

Since a browser doesn't give the user the option to choose whether macros will be enabled or not, it is my speculation that Excel protects the user by preventing macros from being saved or being run when the html file format is used.

I did some web searching and everything I found says that you can not run a macro in a Excel sheet saved as an html file when that file is opened inside a browser. You can only run the macros when the file is opened within Excel - assuming the user has chosen to allow macros to run.

From there, I speculated that the reason for that is to protect the user who opens the file within a browser from unknowing allowing macros to run - because he wasn't given the choice to disable them or not.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#7
October 5, 2011 at 10:33:56
I thought of another situation, directly related to Excel which may help explain my point.

I have seen questions similiar to this in numerous Excel forums:

"I have a workbook with macros in it that I need to email to many users. How can I force macros to be enabled on the user's machine so that when they open the file the macros can be used?"

The answer is: "You can't", for the very reasons I mentioned in my previous posts.

If Excel allowed embedded code to enable macros on a user's machine then the author would essentially be overriding the user's chosen security settings, something that no program should be allowed to do.

Now, there are ways for the workbook to check and see if macros are enabled and present a message to the user telling him that he needs to enable macros in order to use the workbook, but then it becomes the user's choice - not the author's - as to whether macros will be enabled or not.

Unfortunately, that "check" can only be performed when the file is opened within Excel, not when opened in a browser since macros (apparently) can not be used in a file opened within a browser.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#8
October 6, 2011 at 00:27:52
Thank you for your time.

Ragards,



Report •

#9
October 6, 2011 at 12:25:12
Hey DerbyDad03, next time someone says, "Well *I* wouldn't do that!" tell them, "I'm not worried about you. One of our members, Razor2.3 would. I know because he has. When Microsoft allowed this, he did. Do you really want him to have control over your PC? Because that's what you're asking for."

Then send him over to Programming, and have him ask for a script from me. :V

How To Ask Questions The Smart Way


Report •

#10
October 6, 2011 at 12:31:37
Thanks for the offer! ;-)

Hopefully, I got my point across.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#11
October 6, 2011 at 13:15:45
I should add that if you're making HTML pages, then you can use JavaScript. The typical limitations imposed on JavaScript still apply, obviously, and you can't make the scripts while in Excel.

How To Ask Questions The Smart Way


Report •

Ask Question