Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate or modify VBA on the fly?

Status
Not open for further replies.

cthaxter

Programmer
Aug 2, 2001
71
US
Is it possible to generate or modify VBA code on the fly, i.e., at runtime?

I'm using Outlook 2000, and I want to run a certain procedure at Startup, but only once a day at Startup, not every time Outlook is opened throughout the day. I thought the best way to do this would be to modify the code on the fly, say, the first line could be a Const that would tell the rest of the code whether to Exit Sub before the bulk of the code, or not.

Otherwise, I can find an external object in which to store this information, but that may present problems in code distribution. Someone else running my code might get confused with this extra object in one of their folders.

Designating a specific time of day, like 9:30 a.m., before which this code will run, is not an option.

Any help is much appreciated.

-Christopher
 
If you know how to code, then whatever you're coding to do, there must be a way to do a lookup to see if the coding has been run already...

For instance, in my archiving code, I have an Archive history and in that Archive history, there's the date. If the date is today, I don't run the code, however, once the next day becomes today, bam, it runs... There must be some way to "know" if the coding was already run and do that qualifier.
Roy McCafferty
aka BanditWk
Las Vegas, NV
roy@cccamerica.org
RLMBandit@aol.com (private)

"I do this because I know I can and I'm bored at work - no need to send flowers, wine, dinner, or coffee... Just send me a smile to show me that I've helped." ~ Roy McCafferty, seen on a corner in Las Vegas with a sign that said, 'Will work
 
I must protest! Not really meaning to be harsh, but firm:

Speaking generically, as I have not worked much with Outlook: Doing such by modifying code, if even possible, would be somewhat against the laws of nature. Instead, store/save a "flag" somewhere that tells that the One-A-Day has been gonged yet. If that can be done in a table within Outlook, great (and I imagine that there're several available); but even if not, you could write have a one line (text) disk file for that.
 
A text file would work, but I have to agree with BanditWK. The easiest solution to this would be to store a value in your code that represents the date that the macro last ran. When it is opened, compare the current date to that value, and if they are different, run the rest of the macro, update the value to the current day, and save the file.
 
If it were my app I would put a text file record in the user's Windows or Windows\System dir, and read and write necessary flags and dates back to that, also your code could easily recreate it if the user accidently wacked it.

Years ago, back in the old days of MSDOS, we had a program that wrote the next apps program's code, then exited and the next app ran the new code - a dos batch file ran the calling program. We did all this because the apps couldn't pass parameters between themselves the way they can now. That's about the closet I can think of anything like that. Sounds a little too weird for Windows.
 
How about checking a key in the registry when the program first runs. Quit if the date stored there is today, otherwise continue.

Private Sub SaveCurrentDate()
SaveSetting appname:="MyProg", section:="Settings", key:="LastRunDate", setting:=Date
End Sub

Private Function ReadLastRunDate() As Date
'return yesterdays date if no setting exists
ReadLastRunDate = GetSetting(appname:="MyProg", section:="Settings", key:="LastRunDate", Default:=(Date - 1))
End Function

Sub main()
If ReadLastRunDate = Date Then
Exit Sub
End If

'...

'save run date
SaveCurrentDate
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top