自动安装excel VBA加载项

自动安装excel VBA加载项,vba,excel,installation,add-in,Vba,Excel,Installation,Add In,我已经编写了excel VBA加载项文件(.xlam)。我还有一个导出的功能区自定义(.exportedUI)。如何创建安装程序,以便我的用户只需运行安装程序即可安装excel VBA加载项和功能区自定义功能?如果您需要商业工具,高级安装程序对Office加载项安装程序具有一些内置支持: 另一种方法是使用自定义代码来配置外接程序。或许这将有助于: 我创建了一个自动安装过程,添加到XLAM文件的“此工作簿”部分,以便在文件打开时自动运行。 为了区分安装文件和已安装文件,安装版本名为“.instal

我已经编写了excel VBA加载项文件(.xlam)。我还有一个导出的功能区自定义(.exportedUI)。如何创建安装程序,以便我的用户只需运行安装程序即可安装excel VBA加载项和功能区自定义功能?

如果您需要商业工具,高级安装程序对Office加载项安装程序具有一些内置支持:

另一种方法是使用自定义代码来配置外接程序。或许这将有助于:

我创建了一个自动安装过程,添加到XLAM文件的“此工作簿”部分,以便在文件打开时自动运行。 为了区分安装文件和已安装文件,安装版本名为“.install.xlam”,而已安装版本名为“.xlam”。(否则Excel会显示“对不起,Excel无法同时打开两个同名工作簿。”

程序: –将XLAM文件重命名为.install.XLAM –打开它并在Visual Basic编辑器(VBE)中编辑 –将以下步骤添加到VBE中的“此工作簿”部分 –保存您的文件

为了共享/安装XLAM,您现在只需要求用户双击XLAM文件,根据需要启用宏并接受安装外接程序

如果以后要更新XLAM,只需双击它,根据需要启用宏并拒绝安装。然后编辑它并保存更改

以下是要添加到“ThisWorkbook”的代码:

‘ (c) Willy Roche (willy.roche(at)centraliens.net)
‘ Install procedure of XLAM (library of functions)
‘ This procedure will install a file name .install.xlam in the proper excel directory
‘ The install package will be name
‘ During install you may be prompt to enable macros (accept it)
‘ You can accept to install or refuse (which let you modify the XLAM file macros or install procedure

Option Explicit
Const bVerboseMessages = False ‘ Set it to True to be able to Debug install mechanism
Dim bAlreadyRun As Boolean ‘ Will be use to verify if the procedure has already been run

Private Sub Workbook_Open()
‘ This sub will automatically start when xlam file is opened (both install version and installed version)
Dim oAddIn As Object, oXLApp As Object, oWorkbook As Workbook
Dim i As Integer
Dim iAddIn As Integer
Dim bAlreadyInstalled As Boolean
Dim sAddInName As String, sAddInFileName As String, sCurrentPath As String, sStandardPath As String

sCurrentPath = Me.Path & “\”
sStandardPath = Application.UserLibraryPath ‘ Should be Environ(“AppData”) & “\Microsoft\AddIns”
DebugBox (“Called from:'” & sCurrentPath & “‘”)

If InStr(1, Me.Name, “.install.xlam”, vbTextCompare) Then
‘ This is an install version, so let’s pick the proper AddIn name
sAddInName = Left(Me.Name, InStr(1, Me.Name, “.install.xlam”, vbTextCompare) – 1)
sAddInFileName = sAddInName & “.xlam”

‘ Avoid the re-entry of script after activating the addin
If Not (bAlreadyRun) Then
DebugBox (“Called from:'” & sCurrentPath & “‘ bAlreadyRun = false”)
bAlreadyRun = True ‘ Ensure we won’t install it multiple times (because Excel reopen files after an XLAM installation)
If MsgBox(“Do you want to install/overwrite ‘” & sAddInName & “‘ AddIn ?”, vbYesNo) = vbYes Then
‘ Create a workbook otherwise, we get into troubles as Application.AddIns may not exist
Set oXLApp = Application
Set oWorkbook = oXLApp.Workbooks.Add
‘ Test if AddIn already installed
For i = 1 To Me.Application.AddIns.Count
If Me.Application.AddIns.Item(i).FullName = sStandardPath & sAddInFileName Then
bAlreadyInstalled = True
iAddIn = i
End If
Next i
If bAlreadyInstalled Then
‘ Already installed
DebugBox (“Called from:'” & sCurrentPath & “‘ Already installed”)
If Me.Application.AddIns.Item(iAddIn).Installed Then
‘ Deactivate the add-in to be able to overwrite the file
Me.Application.AddIns.Item(iAddIn).Installed = False
Me.SaveCopyAs sStandardPath & sAddInFileName
Me.Application.AddIns.Item(iAddIn).Installed = True
MsgBox (“‘” & sAddInName & “‘ AddIn Overwritten”)
Else
Me.SaveCopyAs sStandardPath & sAddInFileName
Me.Application.AddIns.Item(iAddIn).Installed = True
MsgBox (“‘” & sAddInName & “‘ AddIn Overwritten & Reactivated”)
End If
Else
‘ Not yet installed
DebugBox (“Called from:'” & sCurrentPath & “‘ Not installed”)
Me.SaveCopyAs sStandardPath & sAddInFileName
Set oAddIn = oXLApp.AddIns.Add(sStandardPath & sAddInFileName, True)
oAddIn.Installed = True
MsgBox (“‘” & sAddInName & “‘ AddIn Installed and Activated”)
End If
oWorkbook.Close (False) ‘ Close the workbook opened by the install script
oXLApp.Quit ‘ Close the app opened by the install script
Set oWorkbook = Nothing ‘ Free memory
Set oXLApp = Nothing ‘ Free memory
Me.Close (False)
End If
Else
DebugBox (“Called from:'” & sCurrentPath & “‘ Already Run”)
‘ Already run, so nothing to do
End If
Else
DebugBox (“Called from:'” & sCurrentPath & “‘ in place”)
‘ Already in right place, so nothing to do
End If
End Sub

Sub DebugBox(sText As String)
If bVerboseMessages Then MsgBox (sText)
End Sub

我这样做的方式是让用户运行一个批处理文件来复制该文件(我将其存储在服务器上,但我猜它可能是自解压Ziped文件的一部分),该文件将XLAM文件复制到Microsoft加载项默认文件夹中

然后,您需要从文件>选项>加载项激活加载项(一次) 然后,用户将拥有可用的外接程序

我在批处理中使用的伪代码是:希望有帮助

SET $Path2PlugIn=C:\Users\%USERNAME%\AppData\Roaming\Microsoft\AddIns\
SET $PathOrig="\\PathToServerWhereAddInSits\"

COPY %$PathOrig%AddInName.xlam %$Path2PlugIn%AddInName.xlam