删除与要导入的模块同名的模块

时间:2019-06-25 07:24:48

标签: excel vba import module

我正在尝试设置导入模块,如果满足条件,我需要它在导入之前删除模块。它需要检查4个模块。

代码检查文件修改日期是否符合范围,如果日期较大,则导入。该代码在运行时会引发错误。

For Each objFile In objFSO.GetFolder(szImportPath).Files
    If (objFSO.GetExtensionName(objFile.Name) = "bas") Or _
       (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
       (objFSO.GetExtensionName(objFile.Name) = "frm") And _
        objFile.DateLastModified > Range("ImportDate") Then

        'Delete all modules/Userforms from the ActiveWorkbook
        'Call DeleteVBAModulesF    
        szFileName = objFile.Name

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent

        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents(szFileName)
        VBProj.VBComponents.Remove VBComp

        Range("MacroName") = objFile.Name
        Range("ImportDate") = Format(Date, "dd/mm/yyyy") & " " & Format(Time, "hh.nn.ss")
        '^ Keep time as without, it will import the same module throughout the day when opened.

        cmpComponents.Import objFile.path
    End If

Next objFile

我希望它先删除当前模块,然后再导入。

1 个答案:

答案 0 :(得分:0)

找到了答案。必须以扩展名结尾,并且现在可以使用了。这是code

            Dim FileName

            FileName = Left(objFile.Name, (InStrRev(objFile.Name, ".", -1, vbTextCompare) - 1))

            Dim VBProj As VBIDE.VBProject
            Dim VBComp As VBIDE.VBComponent

            Set VBProj = ActiveWorkbook.VBProject
            Set VBComp = VBProj.VBComponents(FileName)
            VBProj.VBComponents.Remove VBComp