百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

WordVBA函数:删除所有的VBA组件

wptr33 2024-12-09 18:01 38 浏览

在备份后,可以导入相应代码。

主要用于代码在不同电脑中转移,以及分享代码。

这个包括多个函数组,具体如下:


Private Sub 导入所有的VBA组件()

    On Error GoTo ErrorHandler

    Dim strPath As String
    strPath = SelectedTheFolder(strIniPath)
    If strPath = "" Then Exit Sub

    Dim objDic As Variant
    objDic = FileFullNamesInFolder(strPath)
    If IsEmpty(objDic) Then Exit Sub

    Dim strName As Variant
    Dim strFileName As String
    Dim strFileFullName As String
    Dim strFileShortName As String
    Dim strExtend As String

    Dim strExisted As String
    strExisted = ExistedThreeComponentsObjectsNames()

    For Each strName In objDic
        strFileFullName = strName
        strFileName = Replace(strName, strPath, "")
        strFileShortName = Split(strFileName, ".")(0)
        strExtend = Split(strFileName, ".")(1)

        ' 排除 LibCore 和 ThisDocument 组件,这是手工导入
        If strFileShortName <> "LibCore" And strFileShortName <> "ThisDocument" And strExtend <> "frx" Then
            If InStr(1, strExisted, "|" & strFileShortName & "|") > 0 Then
                Select Case MsgBox("当前已经存在名称为:" & strFileName & " 的组件,是否要覆盖?", vbYesNoCancel + vbDefaultButton2)
                    Case vbYes
                        RemoveVBComponent strFileShortName
                        Application.VBE.ActiveVBProject.VBComponents.Import strFileFullName
                    Case vbNo
                        On Error Resume Next
                        Application.VBE.ActiveVBProject.VBComponents(strFileShortName).Name = "Pre_" & strFileShortName
                        Application.VBE.ActiveVBProject.VBComponents.Import strFileFullName
                        On Error GoTo ErrorHandler
                    Case vbCancel
                        ' 取消操作,继续下一个组件
                End Select
            Else
                Application.VBE.ActiveVBProject.VBComponents.Import strFileFullName
            End If
        End If
    Next

    MsgBox "所有组件已成功导入!", vbInformation, "提示"
    Exit Sub

ErrorHandler:
    MsgBox "导入组件时发生错误:" & Err.Description, vbExclamation, "错误"
End Sub

' 获取当前 VBA 工程中所有模块、类和表单的名称,主要这三类!
Private Function ExistedThreeComponentsObjectsNames() As String
    Dim objVBComponent As VBComponent
    Dim strAllVBComponentName As String
    strAllVBComponentName = "|"

    For Each objVBComponent In Application.VBE.ActiveVBProject.VBComponents
        Select Case objVBComponent.Type
            Case vbext_ct_StdModule, vbext_ct_ClassModule, vbext_ct_MSForm
                strAllVBComponentName = strAllVBComponentName & objVBComponent.Name & "|"
        End Select
    Next

    ExistedThreeComponentsObjectsNames = strAllVBComponentName
End Function

' 删除指定名称的 VBA 组件
Private Sub RemoveVBComponent(strVBComponentName As String)
    On Error Resume Next
    Dim objVBComponent As VBComponent
    Set objVBComponent = Application.VBE.ActiveVBProject.VBComponents(strVBComponentName)
    If Not objVBComponent Is Nothing Then
        Application.VBE.ActiveVBProject.VBComponents.Remove objVBComponent
    End If
    On Error GoTo 0
End Sub

' 选择文件夹并返回路径
Public Function SelectedTheFolder(Optional strInitialFileName As String = "D:\") As String
    Dim strFolder As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = strInitialFileName
        .Title = "选择一个文件夹:"
        If .Show <> -1 Then Exit Function
        strFolder = .SelectedItems(1)
    End With

    SelectedTheFolder = strFolder & Application.PathSeparator
End Function

相关推荐

抢先体验Windows 10 20H2新功能,该怎样操作呢?

Win10系统通常会在一年当中进行两次重大更新,分别于上下半年分别推出。上半年的更新主要针对功能的变化,而下半年则是提升系统稳定性。最近Win10下半年最重要的更新Windows1020H2已经开...

教程:如何关闭Win10/Win8.1/Win7管理共享

教程:如何关闭Win10/Win8.1/Win7管理共享出处:IT之家原创(晨风)默认情况下,Windows会创建一些隐藏的共享文件夹,这些文件夹在名称的末尾都有美元“$”标志。当用户在文件资源管理...

Win11学院:如何强制让Windows 11设备蓝屏

IT之家12月15日消息,在Win11系统中蓝屏(BSoD)也称为“停止错误”(StopError)和“错误检查”(BugCheck),通常情况下只有在遇到关键问题的时候才会出现。显然...

微软承认Windows 10新BUG:错误显示没有网络连接

来源:cnBeta.COM在7月补丁星期二活动中,微软发布的累积更新已经修复Windows10系统中的大量BUG。不过近日,微软承认了存在于Windows10May2020(20H...

一课译词:双标(双标英文怎么写)

PhotobyMarkusSpiskeonUnsplash“双标[shuāngbiāo]”,网络流行语,完整说法是“双重标准”,翻译为“doublestandard”。“双标”是指“对同...

知识科普:USB端口如何禁用和解锁?

2015-07-3005:32:00作者:赵为民经常有人会说,我要保护我的笔记本电脑的USB端口,在未经授权的情况下不能够访问。是否有专业的软件可以将USB端口锁死,然后在需要的时候解锁呢?是的,...

小迈科技 X Hologres:高可用的百亿级广告实时数仓建设

通过本文,我们将会介绍小迈科技如何通过Hologres搭建高可用的实时数仓。一、业务介绍...

Modbus-RTU通信(modbus rtu rtu over tcp)

通常情况下我们做Modbus通信的时候,都会先用测试软件进行测试,等通信测试通过之后,我们才会进行移植,我这边主要讲的是移植到PLC上,我现在这边还没有开始做PLC程序,那先把前期的用测试软件如何测...

警惕!利用Github进行水坑攻击安全风险通告

2022年5月19日,亚信安全CERT监测发现Github账户为rkxxz的用户发布了CVE-2022-26809和CVE-2022-24500的项目,项目内容介绍为:CVE-2022-26809...

手机越用越慢?小编教你如何用黑狱冰箱调教它!

看完智趣狗昨天推送的《看完秒懂!这就是Android手机越用越卡的原因!》一文后,我们不难知晓手机越用越慢多是体量更大的APP,以及APP之间相互唤醒而导致资源过度消耗引起的。所以,想让手机恢复高效率...

秒杀系统—3.第二版升级优化的技术文档一

大纲1.秒杀系统的服务细分和服务定位...

Redis命令介绍(二十五)HSET &amp; HSETNX

HSET将上送的键值对保存在key中存储的哈希表中。如果key不存在则创建一个新的哈希表。如果key已存在,则覆盖。在4.0版本后,HSET支持同时上送多键值对。...

IDEA用上这十大插件就很舒服(intellij idea插件推荐)

本文翻译自国外论坛medium,原文地址:https://medium.com/@xjpp22/top-10-plugins-for-intellij-idea-you-dont-want-to-m...

常用 Git 命令清单(git常用命令速查表)
常用 Git 命令清单(git常用命令速查表)

下面是整理的常用Git命令清单。几个专用名词的译名如下。...

2025-07-07 23:38 wptr33

GitHub|清晰理解本地目录、暂存区、本地仓库、远程仓库的交互

GitHub是一个在线平台,旨在促进在一个共同项目上工作的个人之间的代码托管、版本控制和协作。通过该平台,无论何时何地,都可以对项目进行操作(托管和审查代码,管理项目和与世界各地的其他开发者共同开发...