我是一名C#开发人员,他将同事的VBA Excel加载项(.xlam文件)与我的msi安装程序捆绑在一起(如果重要的话,使用VS部署项目构建)。 .xlam放在应用程序文件夹(C:\ Program Files(x86)\ MyCompany \ TheProduct)目录中。用户被迫导航到Excel选项>加载项>管理Excel加载项Go ...>浏览并然后强制导航到上面列出的安装目录。浏览屏幕默认目录为%APPDATA%\ Microsoft \ AddIns。
有没有办法在没有全部点击的情况下自动启用此VBA加载项?
提前致谢,
谢
答案 0 :(得分:3)
我创建了一个自动安装过程,添加到XLAM文件的“This Workbook”部分,以便在文件打开时自动运行。 为了区分安装文件和安装的文件,安装版本命名为“.install.xlam”,安装的版本名为“.xlam”。 (否则Excel有一个“抱歉,Excel无法同时打开两个同名的工作簿。”
步骤: - 使用.install.xlam重命名您的XLAM文件 - 在Visual Basic编辑器(VBE)中打开并编辑 - 将以下过程添加到VBE中的“此工作簿”部分 - 保存文件
为了共享/安装您的XLAM,您现在只需要求用户双击XLAM文件,根据需要启用宏并接受安装加载项。
如果您想稍后更新XLAM,只需双击它,根据需要启用宏并拒绝安装它。然后编辑它并保存更改。
以下是要添加到“ThisWorkbook”的代码:
Option Explicit
' (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
' Set it to True to be able to Debug install mechanism
Const bVerboseMessages = False
' Will be use to verify if the procedure has already been run
Dim bAlreadyRun As Boolean
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
答案 1 :(得分:2)
这通常涉及更新注册表的HKCU部分(当您在Excel选项中手动检查插件时会发生这种情况)。但是,我有时在Excel的安装目录中使用XLStart文件夹。如果您的插件是正确的类型,它将在启动时由Excel加载到系统上的所有用户,并且他们没有选项可以将其关闭。有时这是可以接受的。部署方面它更容易。
答案 2 :(得分:1)
Windows Installer对此没有直接支持。因此,要么您使用某些自定义操作,要么购买一个工具,该工具可直接支持安装Office加载项。
答案 3 :(得分:1)
你可以在你的* .xlam中将这段代码插入到“ThisWorkBook”这段代码安装并激活当前的AddIns,只需打开
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub