Discussion:
|
Sometimes you want to (say) convey an important message to a new user when they first open your project, but this message would become really annoying if it were always shown, so you want to just run it once and never show it again. This goal can be accomplished using several methods such as putting an entry in the workbook itself (which is not always feasible) or, putting an entry in the registry, or, to create a log file on the hard-drive (or wherever else you choose) to mark the event as shown here > http://www.vbaexpress.com/kb/getarticle.php?kb_id=510. The alternative given here is a very literal no-options 'run once' code that deletes itself after it's been run... (Another possible reason for a run-once code could be for a users automatic on-line registration of your project when first opened. On-line registration may involve a password and this use of the password would usually be required to have a reasonable degree of in-built confidentiality, hence the need to delete it immediately after use). With this procedure, after opening the workbook there is no code module to read about what has happened and nothing in the ThisWorkbook module to indicate that anything even existed prior to opening the workbook. {CAVEATS: 1) When security is set to medium, Auto_Open or Workbook_Open events can be cancelled by holding down ''Shift'' to cancel the Auto events. So although this provides some additional degree of security, it's advisable to also lock the project for viewing. 2) Unless the workbook is saved after opening the module deletion will not become 'Fixed', so it's advisable to run another procedure to ensure that the user saves the workbook before closing}
|
Option Explicit
Sub Auto_Open()
Dim ThisModule As Object
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromGuid _
"{0002E157-0000-0000-C000-000000000046}", 5, 3
MsgBox "This project has been registered using code in this module" & vbLf & _
"(Demo: the registration code module will now be deleted)"
Set ThisModule = Application.VBE.ActiveVBProject.VBComponents
End Sub
|