淯水之渊

淯水之渊,都市之颠。

首页 CNDEV 网志 联络 (RSS 2.0) (Atom) 登录
  随笔 9 :: 收藏 3 :: 评论 0 :: 寻迹: 0

News

随笔

随笔归档

收藏

图库

CBD

CSharp

Delphi

搜搜搜

网络安全

2004-06-11 #

'
'removeExcelMacro("Book1.Xls",Array("CheckBox1","TextBox1","ListBox"))
'
'直接删除目标文件的宏代码和控件(可选择保留的控件),Excel文件名称、要删除的控件名称数组
Public Static Function removeExcelMacro(targetExcelFileName As String, killOleObjectType As Variant) As Boolean
    On Error GoTo ErrHand
    Dim i, j, n As Byte
    Dim vbeComp As New VBComponents
    Dim vbaObje As OLEObject
    removeExcelMacro = False
    Set vbeComp = Application.Workbooks(targetExcelFileName).VBProject.VBComponents
    n = vbeComp.Count
    For i = 1 To n
        If i > vbeComp.Count Then Exit For

        If vbeComp(i).Type = 100 Then   '   100: xl_Document_Type(Include Workbook , Worksheet)
            '删除代码
            If vbeComp(i).CodeModule.CountOfLines > 0 Then vbeComp(i).CodeModule.DeleteLines 1, vbeComp(i).CodeModule.CountOfLines
            '删除控件
            vbeComp(i).Activate
            If killOleObjectType(0) <> "" Then
              For Each vbaObje In ActiveSheet.OLEObjects
                 For j = 0 To UBound(killOleObjectType)
                    If UCase(Split(vbaObje.ProgId, ".")(1)) = UCase(killOleObjectType(j)) Then
                      vbaObje.Select: Selection.Delete
                    End If
                 Next
              Next
            End If
        Else
            '删除整个模块
            vbeComp.Remove vbeComp(i)
            i = i - 1
        End If
     Next
    removeExcelMacro = True
    Exit Function
ErrHand:
    MsgBox Err.Description & vbCrLf & vbCrLf & "请与XXX联系!", vbOKOnly + vbCritical
End Function

发表于 @ 15:31 | 评论与反馈 (0)