要防止程序运行多个实例,只要使用App.PrevInstance属性就可以了。当有另一个实例正在运行时,该属性会返回True。你可以在程序的Load事件中调用这个属性进行判断。如果返回值为True,你可以向用户显示一条信息,更进一步,在关闭这个新实例前,还可以激活已有的实例,这也是目前大部分程序采用的方式。下面的代码可以在必要时显示一条警告信息:
If App.PrevInstance Then
MsgBox "Another instance of this program is already open."
AppActivate App.Title
Unload Me
End If