VBA 从加载项导入 udf 模块到工作簿

VBA importing udf module from Add In to Workbook

所以,我有一个 .xlam 插件,里面有几个 UDF。作为众所周知的 absolute path problem 的解决方法,我正在将我的 UDF 导入当前工作簿,以便可以从工作簿调用 UDF,而不是使用以下代码从 AddIn 调用:

Sub CopyOneModule()
Dim FName As String
On Error GoTo errhandler
With ThisWorkbook
    FName = .Path & "\code.txt"
    .VBProject.VBComponents("HMFunctions").Export FName
End With
ActiveWorkbook.VBProject.VBComponents.Import FName
MsgBox ("Functions successfully imported")
errhandler:
If Err.Number <> 0 Then
        Select Case Err.Number
        Case Is = 0:
        Case Is = 1004:
            MsgBox "Please allow access to Object Model and try again.", vbCritical, "No Access granted"
        End Select
End If

它似乎工作正常。所以,我的(可能是愚蠢的)问题是:有没有一种方法可以使带有导入的 UDF "unsee" 的工作簿与存储在 AddIn 中的模块相同?需要避免以下可能很混乱的情况:

提前谢谢你。

按照 snoopen 的建议,从临时文本文件中删除私有标签非常有效。问题关闭。这是我用于导入的最终代码:

    Sub CopyOneModule()
Dim FName As String
Dim FileContent As String
Dim TextFile As Integer
Dim ws As Workbook

Set ws = ActiveWorkbook

On Error GoTo errhandler
With ThisWorkbook
    FName = .Path & "\code.txt"
    .VBProject.VBComponents("HMFunctions").Export FName
End With

TextFile = FreeFile
Open FName For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
Close TextFile
FileContent = Replace(FileContent, "Private", "")
TextFile = FreeFile
Open FName For Output As TextFile
Print #TextFile, FileContent
Close TextFile

ws.VBProject.VBComponents.Import FName
MsgBox ("Functions successfully imported")
errhandler:
If Err.Number <> 0 Then
        Select Case Err.Number
        Case Is = 0:
        Case Is = 1004:
            MsgBox "Please allow access to Object Model and try again.", vbCritical, "No Access granted"
        End Select
    End If

End Sub