本文主要是介绍VBS Excel插件的安装与卸载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
可以通过下面的安装与卸载脚本,将xxxxxxxx.xlam AddIns(插件)安装或卸载。
注意,在安装或卸载前需要退出所有启动的Excel文件。
下面的代码复制粘贴到文本文件,替换好文件名称,另存为.vbs文件,并将xlam文件放到同一个文件夹,双击该vbs文件即可。
安装脚本如下
install.vbs
On Error Resume NextDim 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 IFSet 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 , TrueSet 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 = NothingIF 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 NextDim 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.QuitSet objAddin = Nothing
Set objExcel = NothingSet 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 = NothingIF 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
这篇关于VBS Excel插件的安装与卸载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!