Fra*_*ank 5 excel vba windows-installer
我是一名 C# 开发人员,正在将同事的 VBA Excel 加载项(.xlam 文件)与我的 msi 安装程序(使用 VS 部署项目构建,如果重要的话)捆绑在一起。.xlam 放置在应用程序文件夹 (C:\Program Files (x86)\MyCompany\TheProduct) 目录中。用户被迫导航到 Excel 选项 > 加载项 > 管理 Excel 加载项转到... > 浏览,然后被迫导航到上面列出的安装目录。浏览屏幕默认目录是%APPDATA%\Microsoft\AddIns。
有没有办法让我无需点击就能自动启用此 VBA 插件?
提前致谢,
坦率
小智 5
我创建了一个自动安装过程,将其添加到 XLAM 文件的 \xe2\x80\x9cThis Workbook\xe2\x80\x9d 部分,以便在文件打开时\xe2\x80\x99s 自动运行。\n为了区分安装文件和已安装文件,安装版本名为\xe2\x80\x9c.install.xlam\xe2\x80\x9d,安装版本名为\xe2\x80\x9c.xlam\xe2\x80\ x9d。(否则Excel有一个\xe2\x80\x9c抱歉,Excel不能\xe2\x80\x99同时打开两个同名的工作簿。\xe2\x80\x9d
\n\n步骤:\n\xe2\x80\x93 使用 .install.xlam 重命名 XLAM 文件\n\xe2\x80\x93 打开它并在 Visual Basic 编辑器 (VBE) 中编辑\n\xe2\x80\x93 添加以下过程到 VBE 中的 \xe2\x80\x9cThis 工作簿\xe2\x80\x9d 部分\n\xe2\x80\x93 保存文件
\n\n为了共享/安装您的 XLAM,您现在只需要求用户双击 XLAM 文件,根据需要启用宏并接受安装加载项。
\n\n如果您想稍后更新 XLAM,只需双击它,根据需要启用宏并拒绝安装即可。然后编辑它并保存更改。
\n\n以下是添加到 \xe2\x80\x9cThisWorkbook\xe2\x80\x9d 的代码:
\n\nOption Explicit\n\' (c) Willy Roche (willy.roche(at)centraliens.net)\n\' Install procedure of XLAM (library of functions)\n\' This procedure will install a file name .install.xlam in the proper excel directory\n\' The install package will be name\n\' During install you may be prompt to enable macros (accept it)\n\' You can accept to install or refuse (which let you modify the XLAM file macros or install procedure\n\n\' Set it to True to be able to Debug install mechanism\nConst bVerboseMessages = False\n\n\' Will be use to verify if the procedure has already been run\nDim bAlreadyRun As Boolean\n\nPrivate Sub Workbook_Open()\n \' This sub will automatically start when xlam file is opened (both install version and installed version)\n Dim oAddIn As Object, oXLApp As Object, oWorkbook As Workbook\n Dim i As Integer\n Dim iAddIn As Integer\n Dim bAlreadyInstalled As Boolean\n Dim sAddInName As String, sAddInFileName As String, sCurrentPath As String, sStandardPath As String\n\n sCurrentPath = Me.Path & "\\"\n sStandardPath = Application.UserLibraryPath \' Should be Environ("AppData") & "\\Microsoft\\AddIns"\n DebugBox ("Called from:\'" & sCurrentPath & "\'")\n\n If InStr(1, Me.Name, ".install.xlam", vbTextCompare) Then\n \' This is an install version, so let\xe2\x80\x99s pick the proper AddIn name\n sAddInName = Left(Me.Name, InStr(1, Me.Name, ".install.xlam", vbTextCompare) - 1)\n sAddInFileName = sAddInName & ".xlam"\n\n\n \' Avoid the re-entry of script after activating the addin\n If Not (bAlreadyRun) Then\n DebugBox ("Called from:\'" & sCurrentPath & "\' bAlreadyRun = false")\n bAlreadyRun = True \' Ensure we won\xe2\x80\x99t install it multiple times (because Excel reopen files after an XLAM installation)\n\n If MsgBox("Do you want to install/overwrite \'" & sAddInName & "\' AddIn ?", vbYesNo) = vbYes Then\n \' Create a workbook otherwise, we get into troubles as Application.AddIns may not exist\n Set oXLApp = Application\n Set oWorkbook = oXLApp.Workbooks.Add\n \' Test if AddIn already installed\n For i = 1 To Me.Application.AddIns.Count\n If Me.Application.AddIns.Item(i).FullName = sStandardPath & sAddInFileName Then\n bAlreadyInstalled = True\n iAddIn = i\n End If\n Next i\n\n If bAlreadyInstalled Then\n \' Already installed\n DebugBox ("Called from:\'" & sCurrentPath & "\' Already installed")\n If Me.Application.AddIns.Item(iAddIn).Installed Then\n \' Deactivate the add-in to be able to overwrite the file\n Me.Application.AddIns.Item(iAddIn).Installed = False\n Me.SaveCopyAs sStandardPath & sAddInFileName\n Me.Application.AddIns.Item(iAddIn).Installed = True\n MsgBox ("\'" & sAddInName & "\' AddIn Overwritten")\n Else\n Me.SaveCopyAs sStandardPath & sAddInFileName\n Me.Application.AddIns.Item(iAddIn).Installed = True\n MsgBox ("\'" & sAddInName & "\' AddIn Overwritten & Reactivated")\n End If\n Else\n \' Not yet installed\n DebugBox ("Called from:\'" & sCurrentPath & "\' Not installed")\n Me.SaveCopyAs sStandardPath & sAddInFileName\n Set oAddIn = oXLApp.AddIns.Add(sStandardPath & sAddInFileName, True)\n oAddIn.Installed = True\n MsgBox ("\'" & sAddInName & "\' AddIn Installed and Activated")\n End If\n\n oWorkbook.Close (False) \' Close the workbook opened by the install script\n oXLApp.Quit \' Close the app opened by the install script\n Set oWorkbook = Nothing \' Free memory\n Set oXLApp = Nothing \' Free memory\n Me.Close (False)\n End If\n Else\n DebugBox ("Called from:\'" & sCurrentPath & "\' Already Run")\n \' Already run, so nothing to do\n End If\n\n Else\n DebugBox ("Called from:\'" & sCurrentPath & "\' in place")\n \' Already in right place, so nothing to do\n End If\nEnd Sub\n\nSub DebugBox(sText As String)\nIf bVerboseMessages Then MsgBox (sText)\nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
8538 次 |
| 最近记录: |