|
楼主 |
发表于 2016-1-15 10:25
|
显示全部楼层
本帖最后由 XIAORW 于 2016-1-15 11:13 编辑
install.exe文件安装完成,会自己删除自己。uninstall.exe运行后也会删除自己以及安装目录和addins下的加载宏宏和相关文件。
本程序适合excel2003-2016非免安装(无注册表项目)的各个版本,win64位未测试。
install.exe 源码:
Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long‘声明删除install.exe自身的一个函数,原本可以通过调用vbs或者批处理可以达到,这个简单,也是参考网上一位前辈的代码
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)’声明sleep函数
Private Declare Function MessageBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long, ByVal wlange As Long, ByVal dwTimeout As Long) As Long‘声明messageboxtimeout函数,可实现定时关闭msgbox消息框,关键是其关闭的时间是以毫秒计算的。比wsh.popup更方便。
Sub Main()
On Error Resume Next
'Dim Wsh As Object
'Set Wsh = CreateObject("Wscript.Shell")
Dim xlapp As Object 'Excel对象
Set xlapp = CreateObject("Excel.application")
versNo = xlapp.Version’excel版本号
MessageBoxTimeout hwnd, "当前可安装功能的Microsoft Excel版本是" & versNo & ",安装正在倒计时。", "安装完成提示", vbInformation, 0, 1600
If versNo = "15.0" Then‘excel2013 宏安全性设置在注册表中的位置与其他版本不一样。一下代码通过修改注册表 更改excel宏安全等级为低。安装完成后会恢复默认等级-中
RegStr1 = "HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Office\" & versNo & "\Excel\Security\VBAWarnings"
RegStr2 = "HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Office\" & versNo & "\Excel\Security\Level"
RegStr3 = "HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Office\" & versNo & "\Excel\Security\AccessVBOM"
Else:
RegStr1 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & versNo & "\Excel\Security\VBAWarnings"
RegStr2 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & versNo & "\Excel\Security\Level"
RegStr3 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & versNo & "\Excel\Security\AccessVBOM"
'RegStr4 = "HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & versNo & "\Excel\Security\AccessVBOM"
End If
levelNo = Wsh.RegRead(RegStr2) '读取当前安全级别
levelNo = Wsh.RegWrite(RegStr1, "1", "REG_DWORD") 'VBA安全级别值1-4分别对应:低,中,高,非常高
levelNo = Wsh.RegWrite(RegStr2, "1", "REG_DWORD")
VBcomtrust = Wsh.RegWrite(RegStr3, "1", "REG_DWORD") '添加VBA工程信任
'VBcomtrust = Wsh.RegWrite(RegStr4, "1", "REG_DWORD")
Dim fso
Set fso = CreateObject("Scripting.FilesyStemObject")’创建fso对象,针对不同版本的excel进行加载宏操作
Select Case versNo
Case Is = "11.0"
Dim mbtn As Object
Dim mbtns As Object
On Error Resume Next
xlapp.CommandBars("worksheet menu bar").Controls("帮助(&H)").Controls("表格区域加密(L)").Delete‘以下在excel2003中帮助菜单下创建菜单按钮。
Set mbtn = xlapp.CommandBars("worksheet menu bar").Controls("帮助(&H)")
With mbtn
.Controls.Add(Type:=msoControlPopup, before:=1).Caption = "表格区域加密(L)"
End With
Set mbtns = xlapp.CommandBars("worksheet menu bar").Controls("帮助(&H)").Controls("表格区域加密(L)")
With mbtns
.Controls.Add(Type:=msoControlButton, before:=1).Caption = "区域加密"
.Controls("区域加密").FaceId = 107
.Controls("区域加密").OnAction = "safeback"
.Controls.Add(Type:=msoControlButton, before:=2).Caption = "区域解锁"
.Controls("区域解锁").OnAction = "safeopen"
.Controls("区域解锁").FaceId = 144
End With
sfile = App.Path & "\LockedAreas.xla"’复制安装包里的加载宏文档到默认加载宏路径,并自动加载
despath = xlapp.UserLibraryPath‘office加载宏默认目录,非常好用的方法
fso.CopyFile sfile, despath
xlapp.Visible = False
xlapp.AddIns.Add FileName:=despath & "LockedAreas.xla"
xlapp.AddIns("Lockedareas").Installed = 1
xlapp.Quit
Case Is > "11.0"
sfile = App.Path & "\Lockedareas2007.xlam"
despath = xlapp.UserLibraryPath
fso.CopyFile sfile, despath
xlapp.Visible = False
xlapp.AddIns.Add FileName:=despath & "Lockedareas2007.xlam"‘2007以上版本的加载宏自动加载
xlapp.AddIns("Lockedareas2007").Installed = True
xlapp.Quit
End Select
'
levelNo = Wsh.RegWrite(RegStr1, "2", "REG_DWORD") ’恢复宏安全等级
levelNo = Wsh.RegWrite(RegStr2, "2", "REG_DWORD")
Set sfile1 = fso.getfile(App.Path & "\LockedAreas.xla")
Set sfile2 = fso.getfile(App.Path & "\LockedAreas2007.xlam")
sfile1.Attributes = 0
sfile2.Attributes = 0
sfile1.Delete
sfile2.Delete
MessageBoxTimeout hwnd, "功能安装成功", "完成提示", vbInformation, 0, 820'安装成功
Set xlapp = Nothing
Set xlbook = Nothing
Set fso = Nothing
Set Wsh = Nothing
Sleep (300)
WinExec "cmd /c ping 127.0.0.1 -n 2 && del /q """ & App.Path & "\" & App.EXEName & ".exe""", vbHide ' 删除安装文件-install.exe
End Sub
|
|