Professional Documents
Culture Documents
Timebombing A Workbook
Timebombing A Workbook
AdvancedSoftware PearsonSoftwareConsulting
DesignAndDevelopment www.cpearson.comchip@cpearson.com
OfficeIntegrationProjects
Home TopicIndex What'sNew Search Consulting Downloads NETProgramming
Feedback
XMLDevelopment
Contact
TimebombingAWorkbook
Thispagedescribeshowtolimitthefunctionalityofaworkbookaftera
periodoftime.
Introduction
Theremaybecircumstancesinwhichyouwanttolimitthefunctionalityofaworkbookafteraspecifiedperiodof
time,suchasdistributingademonstrationortrialversionofanapplication.Exceldoesn'tlenditselfwelltodoing
this.Nearlyanylimitationonaworkbookcanbecircumventedbyanexperienceduser.Thatsaid,therearea
numbersthingyoucandolimittheusablelifeofaworkbook,calledtimebombingasworkbook.
Unfortunately,noVBAbasedtimebombingmethodisfoolproof.Askilledusercanquiteeasilycircumventthe
timebombcodeandgetfullaccesstotheworkbook,evenafterithasexpired.Thecodeonthispagealso
requiresthattheuserhasmacrosenabled.Ifmacrosaredisabled,thecodewillnotwork.(SeeEnsuringMacros
AreEnabledforcodetoforcetheusertoenablemacros.)Allthatsaid,thetimebombmethodspresentedhere
areprobably"goodenough".Theywillnotpreventanexperienced(anddishonest)userfromcircumventingthe
protection,butthecodewillworkforthevastmajorityofExcelusers.AllprotectionmechanismsinExcelare
reallyintendedtopreventtheuserfromaccidentallychangingavalueorsomecode.Thesecurityjustisn't
strongenoughtoproviderealprotectionofproprietaryinformationorinformationorcodewithintellectualproperty
value.Forthatlevelofsecurity,youshouldbewritingyourcodeinVisualBasic6orVB.NET.SeeCreatingA
COMAddInfordetailsaboutcreatingaCOMAddInandAutomationAddInsAFunctionLibrariesfordetails
aboutcreatinganAutomationAddIns.
AquicknoteregardingsecurityinExcel:SecurityofanysortinExcelisextremelyweak.Therearemany
passwordbreakersavailableontheinternet.IusePassWare'sVBAKeyandExcelKeythatcancrackExceland
VBAProjectpasswordseffortlessly,usuallyinamatterofseconds.Inmostcases,thepasswordassignedto
anobject(asheet,workbook,orVBAProject)isn'ttheonlypasswordthatwillsucceedinopeningtheobject.It
isoneofmanymathematicallyrelatedpasswords,anyoneofwhichwillwork.Forexample,insometestsof
VBAKey,VBAKeywillreturnnotthepasswordthatIactuallyusedtoprotecttheproject,butaseemingly
randomstringofcharactersthatwillpasssuccessfullythroughthepasswordvalidationlogic.
AsItellallofmycommercialclients,passwordprotection,orprotectionofanysort,shouldbeviewedasa
methodtokeepthehonestuserfromaccidentallychangingordeletingsomethingheshouldn't.Thesecurityis
bynomeanssufficienttoprotectproprietarydataorcodeorcodewithintellectualpropertyvalue.Thesame
goesforanysortofregistrybasedkeystorageorsettings.AskilledusercaneasilyrunRegEditandseethe
valuesintheregistry.Thesameholdtrueformacrobasedsecurity.NotonlycantheVBApasswordbeeasily
broken,butnearlyallmacrobasedsecurityreliesonmacrosbeingenabledinthefirstplaceandthatthe
Application'sEnableEventssettingisTrue.Itisaonelinertocircumventeitherofthosetworestrictions.
Thebottomlineisthatifyouhavevaluablecodeandyouneedmorethan"honestperson"security,thenVBAis
notsufficient.YoushouldcreateaCOMAddIninVB6oraSharedAddInoranExcelWorkbookinVB.NET
andVisualStudioToolsForOffice.
Alloftheproceduresdesribedonthispageusetheconstant:
PrivateConstC_NUM_DAYS_UNTIL_EXPIRATION=30
Youshouldchangethevalueofthisconstanttonumberofdaysafterthefirstusageoftheworkbookthatthe
workbookshouldbedisabled.Thevalueprovidedintheis30days.Onthe31stdayafterthefirsttimethe
workbookisopened,itwillbecomeunusable.
Youcandownloadanexampleworkbookhere.
TimeBombWithADefinedName
Thisprocedure,TimeBombWithDefinedName,usesahiddendefinednametostoretheexpirationdate.The
firsttimetheworkbookisopened,thatdefinednamewillnotexistandwillbecreatedbythecode.(Besurethat
youdeletethisnamewhenyouaredonewithyourowndevelopmentworkontheworkbook.)Whenthedefined
nameiscreated,itusestheC_NUM_DAYS_UNTIL_EXPIRATIONconstanttocalculatetheexpirationdate.
Whenworkbookisopenedafterthefirsttime,thecodereadsthenameExpirationDateandteststhatvalue
againstthecurrentdate.
SubTimeBombWithDefinedName()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'TimeBombWithDefinedName
http://www.cpearson.com/excel/workbooktimebomb.aspx 1/5
4/27/2015 TimebombingAWorkbook
'Thisprocedureusesadefinednametostorethisworkbook's
'expirationdate.Iftheexpirationdatehaspassed,a
'MsgBoxisdisplayedandthisworkbookisclosed.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DimExpirationDateAsString
OnErrorResumeNext
ExpirationDate=Mid(ThisWorkbook.Names("ExpirationDate").Value,2)
IfErr.Number<>0Then
'''''''''''''''''''''''''''''''''''''''''''
'Namedoesn'texist.Createit.
'''''''''''''''''''''''''''''''''''''''''''
ExpirationDate=CStr(DateSerial(Year(Now),_
Month(Now),Day(Now)+C_NUM_DAYS_UNTIL_EXPIRATION))
ThisWorkbook.Names.AddName:="ExpirationDate",_
RefersTo:=Format(ExpirationDate,"shortdate"),_
Visible:=False
EndIf
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Ifthetodayispasttheexpirationdate,closethe
'workbook.Ifthedefinednamedidn'texist,weneed
'toSavetheworkbooktosavethenewlycreatedname.
''''''''''''''''''''''''''''''''''''''''''''''''''''''
IfCDate(Now)>CDate(ExpirationDate)Then
MsgBox"Thisworkbooktrialperiodhasexpired.",vbOKOnly
ThisWorkbook.Closesavechanges:=False
EndIf
EndSub
TimeBombWithSuicide
Amoreextremeversionoftimebombingcodeistopermanentlydeletetheworkbookoncetheexpirationdate
haspassed.Igenerallywouldn'trecommendthisapproach,butitiscertainlypossible.
CAUTION:Thiscodewillpermanentlydelete(notRecycle)theworkbookthatcontains
thecode.Oncedeleted,thereisnowaytogettheworkbookback.Besureyouthatwhen
youaretestingyouhaveanothercopyofthecode.
Howeveryoustoreandcalculatetheexpirationdate,youcanusethecodebelowtodeletetheworkbook
containingthecode.
SubCommintSuicide()
WithThisWorkbook
Application.DisplayAlerts=False
If.Path<>vbNullStringThen
.ChangeFileAccessxlReadOnly
Kill.FullName
EndIf
ThisWorkbook.CloseSaveChanges:=False
EndWith
EndSub
TimeBombToReadOnly
Thisprocedure,TimeBombMakeReadOnly,usesadefinedname,justasthepreviousproceduredid,butrather
thanclosingtheworkbook,itmakestheworkbookreadonly.
SubTimeBombMakeReadOnly()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'TimeBombMakeReadOnly
'Thisprocedureusesadefinednametostoretheexpiration
'dateandiftheworkbookhasexpired,makestheworkbook
'readonly.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DimExpirationDateAsString
DimNameExistsAsBoolean
OnErrorResumeNext
ExpirationDate=Mid(ThisWorkbook.Names("ExpirationDate").Value,2)
IfErr.Number<>0Then
'''''''''''''''''''''''''''''''''''''''''''
'Namedoesn'texist.Createit.
'''''''''''''''''''''''''''''''''''''''''''
ExpirationDate=CStr(DateSerial(Year(Now),_
Month(Now),Day(Now)+C_NUM_DAYS_UNTIL_EXPIRATION))
http://www.cpearson.com/excel/workbooktimebomb.aspx 2/5
4/27/2015 TimebombingAWorkbook
ThisWorkbook.Names.AddName:="ExpirationDate",_
RefersTo:=Format(ExpirationDate,"shortdate"),_
Visible:=False
NameExists=False
Else
NameExists=True
EndIf
''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Ifthetodayispasttheexpirationdate,makethe
'workbookreadonly.WeneedtoSavetheworkbook
'tokeepthenewlycreatednameintact.
''''''''''''''''''''''''''''''''''''''''''''''''''''''
IfCDate(Now)>=CDate(ExpirationDate)Then
IfNameExists=FalseThen
ThisWorkbook.Save
EndIf
ThisWorkbook.ChangeFileAccessxlReadOnly
EndIf
EndSub
TimeBombWithRegistry
Thisprocedure,TimeBombWithRegistry,storestheexpirationdateintheSystemRegistry.Tousethis
code,youmustincludethemodRegistrycodemodule,avaiableontheSystemRegistrypage,orusethe
RegistryWorx.dllavailableontheRegistryWorxpage.IfyouusethemodRegistrymodule,youwillalso
needtoincludethemodGetSystemErrorMessageText,availablehere.ThemodRegistrymoduleandthe
modGetSystemErrorMessageTextmodulesareincludedinthedownloadableexampleworkbook.
SubTimeBombWithRegistry()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'TimeBombWithRegistry
'Thisprocedurestorestheexpirationdateinthesystem
'registry.ChangeC_REG_KEYtoaregistrykeynamethat
'isusedbyyourapplication.
'
'ThisprocedurerequireseitherthemodRegistrymodulefrom
'www.cpearson.com/Excel/Registry.htmor
'www.cpearson.com/Excel/Registry.aspx
'ortheRegistryWorxDLLfrom
'www.cpearson.com/Excel/RegistryWorx.aspx.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ConstC_REG_KEY="Software\Pearson\Test\Settings"
DimKeyExistsAsBoolean
DimValueExistsAsBoolean
DimExpirationDateAsLong
DimBAsBoolean
KeyExists=RegistryKeyExists(HKEY_CURRENT_USER,C_REG_KEY,False)
IfKeyExists=TrueThen
'''''''''''''''''''''''''''''''''
'Keyexists.GettheValuefrom
'thekey.
'''''''''''''''''''''''''''''''''
ValueExists=RegistryValueExists(HKEY_CURRENT_USER,C_REG_KEY,"Expiration")
IfValueExists=TrueThen
'''''''''''''''''''''''''''''''''''''''''
'Valueexists.Itwillbethe
'expirationdate.
'''''''''''''''''''''''''''''''''''''''''
ExpirationDate=RegistryGetValue(HKEY_CURRENT_USER,C_REG_KEY,"Expiration")
Else
'''''''''''''''''''''''''''''''''''''''''
'Valuedoesn'texist.Settheexpiration
'dateandupdatetheRegistry.
'''''''''''''''''''''''''''''''''''''''''
ExpirationDate=DateSerial(Year(Now),Month(Now),_
Day(Now)+C_NUM_DAYS_UNTIL_EXPIRATION)
B=RegistryCreateValue(HKEY_CURRENT_USER,C_REG_KEY,"Expiration",CLng(ExpirationDate))
IfB=FalseThen
'errorcreatingregistryvalue
EndIf
EndIf
Else
''''''''''''''''''''''''''''''''''''''''
'Keydoesn'texist.Settheexpiration
'dateandcreatetheKeyandValue.
''''''''''''''''''''''''''''''''''''''''
ExpirationDate=DateSerial(Year(Now),Month(Now),_
http://www.cpearson.com/excel/workbooktimebomb.aspx 3/5
4/27/2015 TimebombingAWorkbook
Day(Now)+C_NUM_DAYS_UNTIL_EXPIRATION)
B=RegistryCreateKey(HKEY_CURRENT_USER,C_REG_KEY)
IfB=TrueThen
B=RegistryCreateValue(HKEY_CURRENT_USER,C_REG_KEY,"Expiration",ExpirationDate)
IfB=FalseThen
'errorcreatingregistryvalue
EndIf
Else
'errorcreatingregistrykey
EndIf
EndIf
'''''''''''''''''''''''''''''''''''''''''''
'IfNowispasttheexpirationdate,
'closetheworkbook.
'''''''''''''''''''''''''''''''''''''''''''
IfCLng(Now)>CLng(ExpirationDate)Then
ThisWorkbook.Closesavechanges:=False
EndIf
EndSub
MakingTheCodeRunAtOpen
Regardlessofwhichprocedureyouusetolimitusageoftheworkbook,youshouldcallthatprocedureforthe
Workbook_OpeneventprocedureintheThisWorkbookcodemodule:
PrivateSubWorkbook_Open()
TimeBombWithRegistry
EndSub
NotethatthiswillnotworkiftheuserhasdisabledVBAcodeorhassettheApplication.EnableEvents
propertytoFalse.
Thispagelastupdated:11Oct2007
http://www.cpearson.com/excel/workbooktimebomb.aspx 4/5
4/27/2015 TimebombingAWorkbook
Legal
CreatedByChipPearsonatPearsonSoftwareConsulting
ThisPage:www.cpearson.com/excel/workbooktimebomb.aspx Email:chip@cpearson.com
LastUpdated:06Nov2013 Pleasereadthispagebeforeemailingme.
Copyright19972014,CharlesH.Pearson Phone:(816)2146957USACentralTime(6:00UTC)
SiteLastUpdated: 18Apr2015 Between9:00AMand7:00PM
EssentialToolsForDevelopers
Citethispageas:
Source:www.cpearson.com/excel/workbooktimebomb.aspxCopyright2013,CharlesH.Pearson CitationInformation
ThissitecreatedwithMicrosoftVisualStudio2013PremiumandASP.NET4
AdvertiseYourProductOnThisSite
http://www.cpearson.com/excel/workbooktimebomb.aspx 5/5