VBS Excel插件的安装与卸载

可以通过下面的安装与卸载脚本,将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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值