gpt4 book ai didi

excel - 无法刷新对外部 xlsm 文件的引用

转载 作者:行者123 更新时间:2023-12-04 20:36:35 26 4
gpt4 key购买 nike

我有两个 Excel 文件,父文件和子文件,其中子文件包含父函数使用的函数库。出于版本控制的目的,我将它们保存在同一个文件夹中,并在完全相同的位置复制和重命名该文件夹以跟踪我的版本。我还希望动态更新引用,以便当我移动到新版本时,父级始终指向同一位置的子级。

因此,为了实现这一点,我在父级中实现了两个例程。

一、在ThisWorkbook中我使用了Workbook_Open子:

Private Sub Workbook_Open()
'Force the location of the shared library to the current project folder irrespective where the project is located
Call reloadSharedLibrary
End Sub

二,在 Modules.Libraries 中我添加了另一个子 reloadSharedLibrary:
Public librName As Variant


Public isRefReloaded As Boolean

Sub reloadSharedLibrary()

isRefReloaded = True

Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Dim BoolExists As Boolean
Dim librPath As String

Set VBAEditor = Application.VBE
Set vbProj = ActiveWorkbook.VBProject

librName = "lib_emtm"
librPath = Application.ActiveWorkbook.Path & "\lib.xlsm"

' delete any shared lib (if exists)
For Each chkRef In vbProj.References
If chkRef.Name = librName Then
vbProj.References.Remove chkRef
BoolExists = True
End If
Next

' you can only add it to VBAProject only after you quit the above loop
On Error Resume Next
vbProj.References.AddFromFile librPath

If Err.Number <> 0 Then
MsgBox "FATAR ERROR: Cannot find shared library file in project root": End
End If


Set vbProj = Nothing
Set VBAEditor = Nothing

End Sub

现在,问题在于,当我将项目文件夹复制到新版本文件夹时,对子项的引用没有得到更新。版本使用的子版本来自旧版本。

我究竟做错了什么?

最佳答案

问题在于,当 VBA 项目加载文档及其引用时,它会为它们分配一个名称,即 lib_emtm。在你的情况下。当您取消选中对它的引用时,该引用将从 VBA 项目中删除,但项目编辑器会将名称保留在其缓存中。此名称将保留在缓存中,直到您关闭工作簿并重新打开它。

您可以在项目引用菜单中验证这一点:您将看到即使您取消选中引用,库的名称 lib_emtm仍然会出现在那里。

然后当您尝试添加对“其他”子工作簿(同一文件夹中的那个)的引用时,编辑器会发现名称为lib_emtm这与缓存中的相同,因此不会打开新文档并对其进行解析,而是使用缓存的版本,即旧版本!

如果您关闭然后重新打开应用程序,该库的名称将从缓存中消失,因此您可以安装正确的版本。完整地说,此模式仅在引用其他工作簿时出现,而不是在常规 DLL 中出现。安装在系统上。

我试过但找不到删除 Cached library 的 VBA 方法在重新安装之前从编辑器的缓存中获取。如果有人找到一种方法,它将完成解决方案。因此,目前我们必须先关闭该文档,然后再重新打开它并安装 lib。这个过程可能是自动化的,但我建议使用一个提示用户的解决方案。

' Module ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
'Force the location of the shared library to the current project folder irrespective where the project is located
Dim check As Boolean: check = checkSharedLibrary
If check Then Exit Sub
Dim prompt
prompt = MsgBox("The installed lib_emtm library was uninstalled because it was not the correct version." & vbCrLf & _
"If you click Ok, document will close and the correct version will be automatically installed when you reopen it." & vbCrLf & _
"If you click Cancel, library will not be available in this session but will be installed next time you open the document", vbOKCancel)
If prompt = vbOK Then ThisWorkbook.Close True
End Sub

' Regular module
Option Explicit
Private librName As String, librpath As String

' if correct version already installed (correct path) return true
' if library installed with incorrect version, uninstall it and return false
' if library not installed, install it and return true
Public Function checkSharedLibrary() As Boolean
librName = "lib_emtm"
librpath = ThisWorkbook.Path & "\lib_emtm.xlsm"

Dim chkRef As VBIDE.Reference
For Each chkRef In ThisWorkbook.VBProject.References
If chkRef.name = librName Then Exit For
Next

If chkRef Is Nothing Then
install_emtm
checkSharedLibrary = True
ElseIf Left(chkRef.FullPath, InStrRev(chkRef.FullPath, "\") - 1) = ThisWorkbook.Path Then
checkSharedLibrary = True ' we have the correct version
Else
ThisWorkbook.VBProject.References.Remove chkRef ' return false
End If
End Function

Private Sub install_emtm()
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromFile(librpath)
If Err.Number <> 0 Then MsgBox "FATAR ERROR: Could not install lib_emtm:" & vbCrLf & Err.Description & vbCrLf & vbCrLf & _
"Please verify that the library's file is present in the same folder or try a manual install"
End Sub

最后一点,如果我们直接关闭应用程序,该过程可以在没有用户干预的情况下自动化,但在此之前我们可以安排重新打开工作簿。但事情可能会变得复杂,因为用户可能打开了其他 Excel 文档,因此我们不能强制她关闭所有内容。

关于excel - 无法刷新对外部 xlsm 文件的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com