可以通过下面的安装与卸载脚本,将xxxxxxxx.xlam AddIns(插件)安装或卸载。
注意,在安装或卸载前需要退出所有启动的Excel文件。
下面的代码复制粘贴到文本文件,替换好文件名称,另存为.vbs文件,并将xlam文件放到同一个文件夹,双击该vbs文件即可。
安装脚本如下
install.vbs
On Error Resume Next
Dim installPath
Dim addInName
Dim addInFileName
Dim objExcel
Dim objAddin
'设定AddIns情报
addInName = "xxxxxxxx Addin"
addInFileName = "xxxxxxxx.xlam"
IF MsgBox("Do you want to install " &addInName & " ?", vbYesNo + vbQuestion, addInName) = vbNo Then
WScript.Quit
End IF
Set objWshShell = CreateObject("WScript.Shell")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
'创建安装路径
'(ex)C:\Users\[User]\AppData\Roaming\Microsoft\AddIns\[addInFileName]
installPath = objWshShell.SpecialFolders("Appdata") & "\Microsoft\Addins\" & addInFileName
'文件复制(覆盖)
objFileSys.CopyFile addInFileName ,installPath , True
Set objFileSys = Nothing
'Excel 实例化
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
'AddIns注册
Set objAddin = objExcel.AddIns.Add(installPath, True)
objAddin.Installed = True
'Excel 退出
objExcel.Quit
Set objAddin = Nothing
Set objExcel = Nothing
IF Err.Number = 0 THEN
MsgBox "The add-ins have been installed.", vbInformation, addInName
'objWshShell.Run "xxxxxxxx_readme.txt"
ELSE
MsgBox "An error has occurred." & vbCrLF & "If Excel is running, exit it.", vbExclamation, addInName
End IF
Set objWshShell = Nothing
卸载脚本如下
uninstall.vbs
On Error Resume Next
Dim installPath
Dim addInName
Dim addInFileName
Dim objExcel
Dim objAddin
'设定AddIns情报
addInName = "xxxxxxxx Addin"
addInFileName = "xxxxxxxx.xlam"
IF MsgBox("Do you want to uninstall " &addInName & " ?", vbYesNo + vbQuestion, addInName) = vbNo Then
WScript.Quit
End IF
'Excel 实例化
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
'AddIns解除注册
For i = 1 To objExcel.Addins.Count
Set objAddin = objExcel.Addins.item(i)
If objAddin.Name = addInFileName Then
objAddin.Installed = False
End If
Next
'Excel 退出
objExcel.Quit
Set objAddin = Nothing
Set objExcel = Nothing
Set objWshShell = CreateObject("WScript.Shell")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
'创建卸载路径
'(ex)C:\Users\[User]\AppData\Roaming\Microsoft\AddIns\[addInFileName]
installPath = objWshShell.SpecialFolders("Appdata") & "\Microsoft\Addins\" & addInFileName
'删除文件
If objFileSys.FileExists(installPath) = True Then
objFileSys.DeleteFile installPath , True
Else
MsgBox "Add-in file does not exist.", vbExclamation, addInName
End If
'删除注册表
objWshShell.RegDelete("HKCU\Software\VB and VBA Program Settings\xxxxxxxx\")
Set objWshShell = Nothing
Set objFileSys = Nothing
IF Err.Number = 0 THEN
MsgBox "The add-ins have been uninstalled.", vbInformation, addInName
ELSE
MsgBox "An error has occurred." & vbCrLF & "If Excel is running, exit it.", vbExclamation, addInName
End IF