Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 9499|回复: 3

[已解决]关于VBA的“模块未找到” 问题

[复制链接]
发表于 2011-7-31 04:57 | 显示全部楼层 |阅读模式
本帖最后由 glhfgtd 于 2011-7-31 05:11 编辑

我找到了下面代码来清空剪切板的内容,当时很好用,保存文件时,也没出现问题。
可再打开时, 会弹出如下错误提示,然后保存在模块里vba代码全都不见了。
是什么问题呀, 帮帮我!

2011-07-30_134154.jpg

2011-07-30_134217.jpg

2011-07-30_140803.jpg

使用的代码如下:

Option Explicit

Private Declare Function FindWindowEx _
    Lib "user32.dll" _
    Alias "FindWindowExA" ( _
        ByVal hWnd1 As Long, _
        ByVal hWnd2 As Long, ByVal lpsz1 As String, _
        ByVal lpsz2 As String) _
As Long

Private Declare Function AccessibleObjectFromWindow _
    Lib "oleacc" ( _
        ByVal hwnd As Long, _
        ByVal dwId As Long, _
        riid As tGUID, _
        ppvObject As Object) _
As Long

Private Declare Function AccessibleChildren _
    Lib "oleacc" ( _
        ByVal paccContainer As IAccessible, _
        ByVal iChildStart As Long, _
        ByVal cChildren As Long, _
        rgvarChildren As Variant, _
        pcObtained As Long) _
As Long

Private Declare Function LockWindowUpdate _
    Lib "user32" ( _
        ByVal hwndLock As Long) _
As Long

Private Type tGUID
    lData1            As Long
    nData2            As Integer
    nData3            As Integer
    abytData4(0 To 7) As Byte
End Type

Private Const ROLE_PUSHBUTTON = &H2B&

Sub ClearOfficeClipboard()

    Dim hMain        As Long
    Dim hExcel2      As Long
    Dim hClip        As Long
    Dim hWindow      As Long
    Dim hParent      As Long
    Dim lParameter   As Long
    Dim octl         As CommandBarControl
    Dim oIA          As IAccessible
    Dim oNewIA       As IAccessible
    Dim tg           As tGUID
    Dim lReturn      As Long
    Dim lStart       As Long
    Dim avKids()     As Variant
    Dim avMoreKids() As Variant
    Dim lHowMany     As Long
    Dim lGotHowMany  As Long
    Dim bClip        As Boolean
    Dim i            As Long
    Dim hVersion     As Long
   
    hMain = Application.hwnd

    hVersion = Application.Version

    If hVersion < 10 Then MsgBox "此程序不支持Excel2000及以下版本": Exit Sub
   
    If hVersion = 12 Then
        bClip = True
        With Application.CommandBars("Office Clipboard")
            If Not .Visible Then
                LockWindowUpdate hMain
                bClip = False
                Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
                If Not octl Is Nothing Then octl.Execute
            End If
        End With
    End If
   
    Do
         hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)
         hParent = hExcel2: hWindow = 0
         hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", vbNullString)
        If hWindow Then
             hParent = hWindow: hWindow = 0
             hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
            If hWindow Then
                 hParent = hWindow: hWindow = 0
                 hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
                If hClip > 0 Then
                    Exit Do
                End If
            End If
        End If
    Loop While hExcel2 > 0

    If hClip = 0 Then
         hParent = hMain: hWindow = 0
         hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
        If hWindow Then
             hParent = hWindow: hWindow = 0
             hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
        End If
    End If

    If hClip = 0 Then
        With Application.CommandBars("Task Pane")
            If Not .Visible Then
                LockWindowUpdate hMain
                Set octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
                If Not octl Is Nothing Then octl.Execute
                .Visible = False
                LockWindowUpdate 0
            End If
        End With
        hParent = hMain: hWindow = 0
        hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
        If hWindow Then
             hParent = hWindow: hWindow = 0
             hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", "Collect and Paste 2.0")
        End If
    End If

    If hClip = 0 Then
        MsgBox "没找到剪切板窗口"
        Exit Sub
    End If
   
    With tg
        .lData1 = &H618736E0
        .nData2 = &H3C3D
        .nData3 = &H11CF
        .abytData4(0) = &H81
        .abytData4(1) = &HC
        .abytData4(2) = &H0
        .abytData4(3) = &HAA
        .abytData4(4) = &H0
        .abytData4(5) = &H38
        .abytData4(6) = &H9B
        .abytData4(7) = &H71
    End With

    lReturn = AccessibleObjectFromWindow(hClip, 0, tg, oIA)
    lStart = 0

    lHowMany = oIA.accChildCount
    ReDim avKids(lHowMany - 1) As Variant
    lGotHowMany = 0

    lReturn = AccessibleChildren(oIA, lStart, lHowMany, avKids(0), lGotHowMany)
    For i = 0 To lGotHowMany - 1
        If IsObject(avKids(i)) = True Then
            If avKids(i).accName = "Collect and Paste 2.0" Then
                Set oNewIA = avKids(i)
                lHowMany = oNewIA.accChildCount
                Exit For
            End If
        End If
    Next i
    ReDim avMoreKids(lHowMany - 1) As Variant
    lReturn = AccessibleChildren(oNewIA, lStart, lHowMany, avMoreKids(0), lGotHowMany)

    For i = 0 To lHowMany - 1
        If IsObject(avMoreKids(i)) = False Then
            If oNewIA.accName(avMoreKids(i)) = "全部清空" And oNewIA.accRole(avMoreKids(i)) = ROLE_PUSHBUTTON Then
                oNewIA.accDoDefaultAction (avMoreKids(i))
                Exit For
            End If
        End If
    Next i
    If hVersion = 12 And bClip = False Then Application.CommandBars("Office Clipboard").Visible = bClip: LockWindowUpdate 0
   
End Sub
最佳答案
2011-7-31 05:16
你看一下,是不是你的模块的名字,用的是中文的,如果用中文的,有时会有这样的问题.
解决方法,只要把模块改成字母就可以了.
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-7-31 05:16 | 显示全部楼层    本楼为最佳答案   
你看一下,是不是你的模块的名字,用的是中文的,如果用中文的,有时会有这样的问题.
解决方法,只要把模块改成字母就可以了.
回复

使用道具 举报

 楼主| 发表于 2011-7-31 13:49 | 显示全部楼层
问题真的解决了,看来微软还是有语言兼容的问题呀
回复

使用道具 举报

发表于 2011-11-3 00:25 | 显示全部楼层
是不是真的该成字母鸡可以了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-25 00:06 , Processed in 0.231333 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表