clear or empty office clipboard form Excel VBA

photonblaster

New Member
Joined
Oct 4, 2005
Messages
37
I came back to a common problem of clearing the office clipboard from Excel VBA (I have WXP, Office 2003 all updates and service packe) to see if there are any elegant solutions posted on this message board, and found none. In fact, I found that many people suggesting help to clearing the clipboard gave help on clearing the windows clipboard rather than the Office clipboard. I did not even see a solution to this issue (at least something that I rcoginized as a solution, forgive me if I missed one), elegant or otherwise, so I am posting the solution I have so people can use it as they see fit, and to see if anyone has improvement suggestions or other methods of doing it.

I especially would like to know what code I could add to automatically open the clipboard, since the code only seems to work if the clipboard is open.
Of course, if someone has 10 lines of code that will empty the clipboard, I would love to see this rather than using this big module.

I am far from an expert on the solution I have, since, after litterally days of googling I found this solution concept on Guoqiang Wu's Blog. According to him, the "handle" (or whatever the technical term is) to access the office clipboard can change as you open and close Excel (or any office program). So you have to search for the handle first, and then send the appropriate signal to the clipboard. Mr. Wu was kind enough to help me modify his implementation so that the search function results gets cached, and any subsequent calls to the clear clipboard function bypasses the search and just clears the clipboard. I am very much indebted to him since clearing the clipboard was a do or die for my project.

Here is my implementation.
to clear the clipboard from within vba just put the code line
ClearOfficeClipboard.

You should run it at least once during any initialization phase of your code so that the overhead for finding the clipboard "handle" does not slow you up latter when you need fast execution.

Also, this is real pain in the whatever,but for this code to work, the clipboard must be visible!!!! I have not found a work around this. And I can't even find a way to make the clipboard visible from within vba, so I have to do it manually! I just minimize the window size and after months of use I do not even notice it there, but talk about brute force! However, since my project will not execute quickly enough without emptying the clipboard frequently of all the garbage it collects that sucks up memory and really slows things down.


Clear the Clipboard Module: ( I just commented out things when I changed them from Mr. Wu's original version that is more generic since it was supposed to work from any office application).

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Const</SPAN> CHILDID_SELF = 0&
<SPAN style="color:#00007F">Const</SPAN> ROLE_TITLEBAR = &H1&
<SPAN style="color:#00007F">Const</SPAN> ROLE_MENUBAR = &H2&
<SPAN style="color:#00007F">Const</SPAN> ROLE_SCROLLBAR = &H3&
<SPAN style="color:#00007F">Const</SPAN> ROLE_GRIP = &H4&
<SPAN style="color:#00007F">Const</SPAN> ROLE_SOUND = &H5&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CURSOR = &H6&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CARET = &H7&
<SPAN style="color:#00007F">Const</SPAN> ROLE_ALERT = &H8&
<SPAN style="color:#00007F">Const</SPAN> ROLE_WINDOW = &H9&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CLIENT = &HA&
<SPAN style="color:#00007F">Const</SPAN> ROLE_MENUPOPUP = &HB&
<SPAN style="color:#00007F">Const</SPAN> ROLE_MENUITEM = &HC&
<SPAN style="color:#00007F">Const</SPAN> ROLE_TOOLTIP = &HD&
<SPAN style="color:#00007F">Const</SPAN> ROLE_APPLICATION = &HE&
<SPAN style="color:#00007F">Const</SPAN> ROLE_DOCUMENT = &HF&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PANE = &H10&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CHART = &H11&
<SPAN style="color:#00007F">Const</SPAN> ROLE_DIALOG = &H12&
<SPAN style="color:#00007F">Const</SPAN> ROLE_BORDER = &H13&
<SPAN style="color:#00007F">Const</SPAN> ROLE_GROUPING = &H14&
<SPAN style="color:#00007F">Const</SPAN> ROLE_SEPARATOR = &H15&
<SPAN style="color:#00007F">Const</SPAN> ROLE_TOOLBAR = &H16&
<SPAN style="color:#00007F">Const</SPAN> ROLE_STATUSBAR = &H17&
<SPAN style="color:#00007F">Const</SPAN> ROLE_TABLE = &H18&
<SPAN style="color:#00007F">Const</SPAN> ROLE_COLUMNHEADER = &H19&
<SPAN style="color:#00007F">Const</SPAN> ROLE_ROWHEADER = &H1A&
<SPAN style="color:#00007F">Const</SPAN> ROLE_COLUMN = &H1B&
<SPAN style="color:#00007F">Const</SPAN> ROLE_ROW = &H1C&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CELL = &H1D&
<SPAN style="color:#00007F">Const</SPAN> ROLE_LINK = &H1E&
<SPAN style="color:#00007F">Const</SPAN> ROLE_HELPBALLOON = &H1F&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CHARACTER = &H20&
<SPAN style="color:#00007F">Const</SPAN> ROLE_LIST = &H21&
<SPAN style="color:#00007F">Const</SPAN> ROLE_LISTITEM = &H22&
<SPAN style="color:#00007F">Const</SPAN> ROLE_OUTLINE = &H23&
<SPAN style="color:#00007F">Const</SPAN> ROLE_OUTLINEITEM = &H24&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PAGETAB = &H25&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PROPERTYPAGE = &H26&
<SPAN style="color:#00007F">Const</SPAN> ROLE_INDICATOR = &H27&
<SPAN style="color:#00007F">Const</SPAN> ROLE_GRAPHIC = &H28&
<SPAN style="color:#00007F">Const</SPAN> ROLE_STATICTEXT = &H29&
<SPAN style="color:#00007F">Const</SPAN> ROLE_TEXT = &H2A&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PUSHBUTTON = &H2B&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CHECKBUTTON = &H2C&
<SPAN style="color:#00007F">Const</SPAN> ROLE_RADIOBUTTON = &H2D&
<SPAN style="color:#00007F">Const</SPAN> ROLE_COMBOBOX = &H2E&
<SPAN style="color:#00007F">Const</SPAN> ROLE_DROPLIST = &H2F&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PROGRESSBAR = &H30&
<SPAN style="color:#00007F">Const</SPAN> ROLE_DIAL = &H31&
<SPAN style="color:#00007F">Const</SPAN> ROLE_HOTKEYFIELD = &H32&
<SPAN style="color:#00007F">Const</SPAN> ROLE_SLIDER = &H33&
<SPAN style="color:#00007F">Const</SPAN> ROLE_SPINBUTTON = &H34&
<SPAN style="color:#00007F">Const</SPAN> ROLE_DIAGRAM = &H35&
<SPAN style="color:#00007F">Const</SPAN> ROLE_ANIMATION = &H36&
<SPAN style="color:#00007F">Const</SPAN> ROLE_EQUATION = &H37&
<SPAN style="color:#00007F">Const</SPAN> ROLE_BUTTONDROPDOWN = &H38&
<SPAN style="color:#00007F">Const</SPAN> ROLE_BUTTONMENU = &H39&
<SPAN style="color:#00007F">Const</SPAN> ROLE_BUTTONDROPDOWNGRID = &H3A&
<SPAN style="color:#00007F">Const</SPAN> ROLE_WHITESPACE = &H3B&
<SPAN style="color:#00007F">Const</SPAN> ROLE_PAGETABLIST = &H3C&
<SPAN style="color:#00007F">Const</SPAN> ROLE_CLOCK = &H3D&

<SPAN style="color:#00007F">Type</SPAN> tGUID
    lData1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    nData2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    nData3 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    abytData4(0 To 7) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>

<SPAN style="color:#00007F">Type</SPAN> AccObject
    objIA <SPAN style="color:#00007F">As</SPAN> IAccessible
    lngChild <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>

<SPAN style="color:#007F00">'Global objButton As AccObject</SPAN>
<SPAN style="color:#007F00">'Dim accButton As AccObject</SPAN>

<SPAN style="color:#00007F">Const</SPAN> WM_GETTEXT = &HD

<SPAN style="color:#00007F">Public</SPAN> lngChild <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, strClass <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, strCaption <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> AccessibleObjectFromWindow <SPAN style="color:#00007F">Lib</SPAN> "oleacc" _
                                            (<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> dwId <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                             riid <SPAN style="color:#00007F">As</SPAN> tGUID, ppvObject <SPAN style="color:#00007F">As</SPAN> Object) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> AccessibleChildren <SPAN style="color:#00007F">Lib</SPAN> "oleacc" _
                                    (<SPAN style="color:#00007F">ByVal</SPAN> paccContainer <SPAN style="color:#00007F">As</SPAN> IAccessible, <SPAN style="color:#00007F">ByVal</SPAN> iChildStart <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                     <SPAN style="color:#00007F">ByVal</SPAN> cChildren <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, rgvarChildren <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, _
                                     pcObtained <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "FindWindowA" ( _
                            <SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _
                            <SPAN style="color:#00007F">ByVal</SPAN> lpWindowName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetParent <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> EnumChildWindows <SPAN style="color:#00007F">Lib</SPAN> "user32" (<SPAN style="color:#00007F">ByVal</SPAN> hwndParent _
                                                <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpEnumFunc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetClassName <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "GetClassNameA" (<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                                                  <SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> nMaxCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SendMessage <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "SendMessageA" (<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                                                <SPAN style="color:#00007F">ByVal</SPAN> wMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindowEx <SPAN style="color:#00007F">Lib</SPAN> "user32" Alias "FindWindowExA" (<SPAN style="color:#00007F">ByVal</SPAN> hWnd1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                                                  <SPAN style="color:#00007F">ByVal</SPAN> hWnd2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpClass <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpCaption <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>





<SPAN style="color:#007F00">'Retrieve window class name</SPAN>
<SPAN style="color:#00007F">Function</SPAN> GetWndClass(<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> buf <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, retval <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    buf = Space(256)
    retval = GetClassName(hWnd, buf, 255)
    GetWndClass = Left(buf, retval)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Retrieve window title</SPAN>

<SPAN style="color:#00007F">Function</SPAN> GetWndText(<SPAN style="color:#00007F">ByVal</SPAN> hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> buf <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, retval <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    buf = Space(256)
    retval = SendMessage(hWnd, WM_GETTEXT, 255, buf)
    GetWndText = Left(buf, InStr(1, buf, Chr(0)) - 1)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'The call back function used by EnumChildWindows</SPAN>
<SPAN style="color:#00007F">Function</SPAN> EnumChildWndProc(<SPAN style="color:#00007F">ByVal</SPAN> hChild <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> found <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
    EnumChildWndProc = -1
    <SPAN style="color:#00007F">If</SPAN> strClass > "" And strCaption > "" <SPAN style="color:#00007F">Then</SPAN>
        found = <SPAN style="color:#00007F">StrComp</SPAN>(GetWndClass(hChild), strClass, vbTextCompare) = 0 And _
                <SPAN style="color:#00007F">StrComp</SPAN>(GetWndText(hChild), strCaption, vbTextCompare) = 0
    <SPAN style="color:#00007F">ElseIf</SPAN> strClass > "" <SPAN style="color:#00007F">Then</SPAN>
        found = (StrComp(GetWndClass(hChild), strClass, vbTextCompare) = 0)
    <SPAN style="color:#00007F">ElseIf</SPAN> strCaption > "" <SPAN style="color:#00007F">Then</SPAN>
        found = (StrComp(GetWndText(hChild), strCaption, vbTextCompare) = 0)
    <SPAN style="color:#00007F">Else</SPAN>
        found = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

    <SPAN style="color:#00007F">If</SPAN> found <SPAN style="color:#00007F">Then</SPAN>
        lngChild = hChild
        EnumChildWndProc = 0
    <SPAN style="color:#00007F">Else</SPAN>
        EnumChildWndProc = -1
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Find the window handle of a child window based on its class and titie</SPAN>
<SPAN style="color:#00007F">Function</SPAN> FindChildWindow(<SPAN style="color:#00007F">ByVal</SPAN> hParent <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">Optional</SPAN> cls <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "", <SPAN style="color:#00007F">Optional</SPAN> title <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "") <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    lngChild = 0
    strClass = cls
    strCaption = title
    EnumChildWindows hParent, AddressOf EnumChildWndProc, 0
    FindChildWindow = lngChild
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Retrieve the IAccessible interface from a window handle</SPAN>
<SPAN style="color:#007F00">'Reference:Jean Ross,Chapter 17: Accessibility in Visual Basic,Advanced Microsoft Visual Basic 6.0, 2nd Edition</SPAN>
<SPAN style="color:#00007F">Function</SPAN> IAccessibleFromHwnd(hWnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> IAccessible
    <SPAN style="color:#00007F">Dim</SPAN> oIA <SPAN style="color:#00007F">As</SPAN> IAccessible
    <SPAN style="color:#00007F">Dim</SPAN> tg <SPAN style="color:#00007F">As</SPAN> tGUID
    <SPAN style="color:#00007F">Dim</SPAN> lReturn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    <SPAN style="color:#007F00">' Define the GUID for the IAccessible object</SPAN>
    <SPAN style="color:#007F00">' {618736E0-3C3D-11CF-810C-00AA00389B71}</SPAN>

    <SPAN style="color:#00007F">With</SPAN> 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
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    <SPAN style="color:#007F00">' Retrieve the IAccessible object for the form</SPAN>
    lReturn = AccessibleObjectFromWindow(hWnd, 0, tg, oIA)
    <SPAN style="color:#00007F">Set</SPAN> IAccessibleFromHwnd = oIA
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Recursively looking for a child with specified accName and accRole in the accessibility tree</SPAN>
<SPAN style="color:#00007F">Function</SPAN> FindAccessibleChild(oParent <SPAN style="color:#00007F">As</SPAN> IAccessible, strName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, lngRole <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> AccObject
    <SPAN style="color:#00007F">Dim</SPAN> lHowMany <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> avKids() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> lGotHowMany <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> oChild <SPAN style="color:#00007F">As</SPAN> IAccessible
    FindAccessibleChild.lngChild = CHILDID_SELF
    <SPAN style="color:#00007F">If</SPAN> oParent.accChildCount = 0 <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> FindAccessibleChild.objIA = <SPAN style="color:#00007F">Nothing</SPAN>
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    lHowMany = oParent.accChildCount
    <SPAN style="color:#00007F">ReDim</SPAN> avKids(lHowMany - 1) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    lGotHowMany = 0
    <SPAN style="color:#00007F">If</SPAN> AccessibleChildren(oParent, 0, lHowMany, avKids(0), lGotHowMany) <> 0 <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Error retrieving accessible children!"
        <SPAN style="color:#00007F">Set</SPAN> FindAccessibleChild.objIA = <SPAN style="color:#00007F">Nothing</SPAN>
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

    <SPAN style="color:#007F00">'To do: the approach described in http://msdn.microsoft.com/msdnmag/issues/0400/aaccess/default.aspx</SPAN>
    <SPAN style="color:#007F00">' are probably better and more reliable</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> lGotHowMany - 1
        <SPAN style="color:#00007F">If</SPAN> IsObject(avKids(i)) <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">StrComp</SPAN>(avKids(i).accName, strName) = 0 And avKids(i).accRole = lngRole <SPAN style="color:#00007F">Then</SPAN>
                <SPAN style="color:#00007F">Set</SPAN> FindAccessibleChild.objIA = avKids(i)
                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN>
            <SPAN style="color:#00007F">Else</SPAN>
                <SPAN style="color:#00007F">Set</SPAN> oChild = avKids(i)
                FindAccessibleChild = FindAccessibleChild(oChild, strName, lngRole)
                <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> FindAccessibleChild.objIA <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
                    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN>
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">Else</SPAN>
            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">StrComp</SPAN>(oParent.accName(avKids(i)), strName) = 0 And oParent.accRole(avKids(i)) = lngRole <SPAN style="color:#00007F">Then</SPAN>
                <SPAN style="color:#00007F">Set</SPAN> FindAccessibleChild.objIA = oParent
                FindAccessibleChild.lngChild = avKids(i)
                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Function</SPAN> FindAccessibleChildInWindow(hwndParent <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, strName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, lngRole <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> AccObject
    <SPAN style="color:#00007F">Dim</SPAN> oParent <SPAN style="color:#00007F">As</SPAN> IAccessible
    <SPAN style="color:#00007F">Set</SPAN> oParent = IAccessibleFromHwnd(hwndParent)
    <SPAN style="color:#00007F">If</SPAN> oParent <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> FindAccessibleChildInWindow.objIA = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Else</SPAN>
        FindAccessibleChildInWindow = FindAccessibleChild(oParent, strName, lngRole)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>



<SPAN style="color:#007F00">'Generic routine to retrieve the window handle of the active window of an Office Application</SPAN>
<SPAN style="color:#00007F">Function</SPAN> GetOfficeAppHwnd(app <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    GetOfficeAppHwnd = FindWindow(vbNullString, GetOfficeAppWindowTitle(app))
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Retrieve the window handle of the task pane</SPAN>
<SPAN style="color:#007F00">'Notice: the task pane window title is localized!</SPAN>
<SPAN style="color:#007F00">'You can find out the window class and title using Spy, Inspect32 or other tools</SPAN>
<SPAN style="color:#00007F">Function</SPAN> GetOfficeTaskPaneHwnd(app <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    GetOfficeTaskPaneHwnd = FindChildWindow(GetOfficeAppHwnd(app), _
                                            "MsoCommandBar", "Task Pane")
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Retrieve the window handle of the clipboard child window inside task pane</SPAN>
<SPAN style="color:#007F00">'The window title of the clipboard window seems to be language independent,</SPAN>
<SPAN style="color:#007F00">'making it a better start point to searching our UI element than the task pane window</SPAN>
<SPAN style="color:#00007F">Function</SPAN> GetOfficeClipboardHwnd(app <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    GetOfficeClipboardHwnd = FindChildWindow(GetOfficeAppHwnd(app), , "Collect and Paste 2.0")
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Generic routine to retrieve the window title of the active window of an Office application</SPAN>
<SPAN style="color:#00007F">Function</SPAN> GetOfficeAppWindowTitle(app <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrorHandler
    <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> app.Name
    <SPAN style="color:#00007F">Case</SPAN> "Microsoft Word"
        GetOfficeAppWindowTitle = app.ActiveWindow.Caption & " - " & app.Name
    <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN>
        GetOfficeAppWindowTitle = app.Name & " - " & app.ActiveWindow.Caption
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN>

ErrorHandler:
    MsgBox "Unsupported Office application!"
    GetOfficeAppWindowTitle = ""
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#007F00">'Using Active Accessibility to clear Office clipboard</SPAN>
<SPAN style="color:#007F00">'Assumption:</SPAN>
<SPAN style="color:#007F00">'    this is running within Word or Excel as a macro, thus the global Application object is available</SPAN>
<SPAN style="color:#00007F">Sub</SPAN> ClearOfficeClipboard()
    <SPAN style="color:#00007F">Static</SPAN> accButton <SPAN style="color:#00007F">As</SPAN> AccObject
    <SPAN style="color:#00007F">If</SPAN> accButton.objIA <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Dim</SPAN> fShown <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
        fShown = CommandBars("Task Pane").Visible
        wsRAW_DATA.Select
        Range("A1").Copy
        bSuccess = oAIX.Sleep(2000)
        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> (fShown) <SPAN style="color:#00007F">Then</SPAN>
            CommandBars("Task Pane").Enabled = <SPAN style="color:#00007F">True</SPAN>
            CommandBars("Task Pane").Visible = <SPAN style="color:#00007F">True</SPAN>
            <SPAN style="color:#007F00">'                        MsgBox ("Press OK and Open Clipboard , then F5 to continue or you will get an error message")</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#007F00">'             CommandBars("Task Pane").Visible = True</SPAN>
        <SPAN style="color:#007F00">'           WordBasic.EditOfficeClipboard  '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$</SPAN>
        accButton = FindAccessibleChildInWindow(GetOfficeClipboardHwnd(Application), "Clear All", ROLE_PUSHBUTTON)
        <SPAN style="color:#007F00">'                CommandBars("Task Pane").Visible = fShown</SPAN>
        <SPAN style="color:#007F00">'                  CommandBars("Task Pane").Visible = False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">If</SPAN> accButton.objIA <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Unable to locate the ""Clear All"" button!"
    <SPAN style="color:#00007F">Else</SPAN>
        accButton.objIA.accDoDefaultAction accButton.lngChild
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#007F00">''Using Active Accessibility to clear Office clipboard</SPAN>
<SPAN style="color:#007F00">''Input: app - the application object of an Office application</SPAN>
<SPAN style="color:#007F00">''Assumption: Clipboard task pane is shown in the Office application (app object)</SPAN>
<SPAN style="color:#007F00">'Function ClearOfficeClipboard(app As Object) As Boolean</SPAN>
<SPAN style="color:#007F00">'        Dim oButton As AccObject, fShow As Boolean</SPAN>
<SPAN style="color:#007F00">''        Dim fShow As Boolean      '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$</SPAN>
<SPAN style="color:#007F00">'        'Get the IAccessible interface and child id (wrapped in the AccObject type)</SPAN>
<SPAN style="color:#007F00">'        'Notice: the second parameter, accName "Clear All" is localized!</SPAN>
<SPAN style="color:#007F00">'        'You can find out the accName and accRole using Spy, Inspect32 or other tools</SPAN>
<SPAN style="color:#007F00">'        oButton = FindAccessibleChildInWindow(GetOfficeClipboardHwnd(app), "Clear All", ROLE_PUSHBUTTON)</SPAN>
<SPAN style="color:#007F00">'        If oButton.objIA Is Nothing Then</SPAN>
<SPAN style="color:#007F00">'                MsgBox "Unable to locate the ""Clear All"" button!"</SPAN>
<SPAN style="color:#007F00">'                ClearOfficeClipboard = False</SPAN>
<SPAN style="color:#007F00">'        Else</SPAN>
<SPAN style="color:#007F00">'                oButton.objIA.accDoDefaultAction oButton.lngChild</SPAN>
<SPAN style="color:#007F00">'                ClearOfficeClipboard = True</SPAN>
<SPAN style="color:#007F00">'        End If</SPAN>
<SPAN style="color:#007F00">'End Function</SPAN>



</FONT>[/b]
 
Thank you both so much for you help, I will do some more testing and post results. Tesitng so far indicates so far that the clipboard windowclass can get un-loaded under certain cercumstances that the code does not handle, details when I am done testing.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Thank you both so much for you help

I have been able to make Joe's code work for me, with some modifications. I will try to say what I did using the right buzz words, forgive me if I misuse them.
My testing indicates that the clipboard windows class name (hCLip variable) does NOT remain loaded if you clear the clipboard when it is not visible, or if you close an empty clipboard. It does remian loaded if the clipboard remains visible. It does remain loaded if you just close the clipboard without clearing it out.

So I modified the code to always force the closed clipboard to go through the visible-not visible steps to give it a loaded class name that we can find, and we have to find it in order to send the clear clipboard mouse click commands through PostMessage.

Since I will call this sub thousands of times, and execution speed is tremendously important to my project, I captured first time through so some static variable initializations do not have to be done more than once. A second "first time through" capture bypassed evaluating hClip if the clipboard has remained visible.

The code as previously written can change the state of ScreenUpdating if it is called from other code where screen updating is off, so I added a check for this.

I am assuming that the references to api clipboard functions are to control the windows clipboard, not the Office clipboard, since they seemed to do nothing to the status of the Office Clipboard, so I deleted those. (???did I do the right thing?)

This code now works with the clipboard viewer open or closed (at least for me), but it gives screen flicker when the clipboard is closed, even though the screen updating turned off. It is very visible if you put the ClearOfficeClipboard sub call in a loop. The flicker is so bad It makes my project non-functional as far as a user is concerned, so I am back to having to keep the clipboard open.

Any suggestions on how to eliminate the flicker?

Also, Mr. Wu's code, which seems to clear the clipboard from any Office product while this one is specific to Excel, uses the default action for the "Clear All" button rather than psuedo mouse clicks through PostMessage. The default button action would seem to be more self-contained within VBA (and more elegant?) but I can't see how to merge this concept with this code. His code line, in my original post, is
accButton.objIA.accDoDefaultAction accButton.lngChild
I get no flicker when I use my modified version of his code. Is it possilbe that the flicker is related to the method of "pushing" the clear all button?

Here is the modification of Joe's code:
<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> myClr()
<SPAN style="color:#007F00">'Put this sub inta a Sheet Module, like: Sheet1.</SPAN>
    <SPAN style="color:#00007F">Call</SPAN> ClearOfficeClipboard
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#007F00">'Put the code from here down into a Standard Module, like Module1.</SPAN>
<SPAN style="color:#00007F">Private</SPAN> Declare <SPAN style="color:#00007F">Function</SPAN> FindWindowEx Lib "user32.dll" _
                                      Alias "FindWindowExA" (<SPAN style="color:#00007F">ByVal</SPAN> hWnd1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                                             <SPAN style="color:#00007F">ByVal</SPAN> hWnd2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lpsz1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _
                                                             <SPAN style="color:#00007F">ByVal</SPAN> lpsz2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Private</SPAN> Declare <SPAN style="color:#00007F">Function</SPAN> PostMessage Lib "user32.dll" Alias _
                                     "PostMessageA" (<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
                                                     <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_LBUTTONDOWN <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = &H201&
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_LBUTTONUP <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = &H202&

<SPAN style="color:#007F00">'creates a long variable out of two words</SPAN>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> MakeLong(<SPAN style="color:#00007F">ByVal</SPAN> nLoWord <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> nHiWord <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    MakeLong = nHiWord * 65536 + nLoWord
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>


<SPAN style="color:#00007F">Sub</SPAN> ClearOfficeClipboard()
    <SPAN style="color:#00007F">Dim</SPAN> hMain&, hExcel2&, hWindow&, hParent&
    <SPAN style="color:#00007F">Static</SPAN> sTask <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#007F00">'    Dim hClip As Long                        'changed by Lary</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> octl, bScreenUpdatingIsOn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
    <SPAN style="color:#00007F">Static</SPAN> lParameter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, bNotFirstVisibleTime <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, hClip <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, bNotFirstTime <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>

    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> (bNotFirstTime) <SPAN style="color:#00007F">Then</SPAN>
        lParameter = MakeLong(120, 18)
        sTask = Application.CommandBars("Task Pane").NameLocal
        <SPAN style="color:#007F00">'Handle for XLMAIN</SPAN>
        hMain = Application.hwnd
        bNotFirstTime = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

    <SPAN style="color:#00007F">With</SPAN> Application.CommandBars("Task Pane")

        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> .Visible <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#007F00">'assume have to force the window if it is not visible, since it appears that</SPAN>
            <SPAN style="color:#007F00">' the window class does not remain loaded if you clear a non-visible clipboard</SPAN>


                <SPAN style="color:#007F00">'determine current status of screenupdating so that this sub does not change it</SPAN>
                bScreenUpdatingIsOn = Application.ScreenUpdating
                <SPAN style="color:#00007F">If</SPAN> bScreenUpdatingIsOn <SPAN style="color:#00007F">Then</SPAN> Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>

                <SPAN style="color:#00007F">Set</SPAN> octl = Application.CommandBars(1).FindControl(ID:=809, recursive:=True)
                <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> octl <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> octl.Execute
                .Visible = <SPAN style="color:#00007F">False</SPAN>

                <SPAN style="color:#007F00">'return to screenupdating on if that is what it was in the beginning</SPAN>
                <SPAN style="color:#00007F">If</SPAN> bScreenUpdatingIsOn <SPAN style="color:#00007F">Then</SPAN> Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>

                <SPAN style="color:#00007F">If</SPAN> hClip = 0 <SPAN style="color:#00007F">Then</SPAN>
                    hParent = hMain: hWindow = 0
                    hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
                    <SPAN style="color:#00007F">If</SPAN> hWindow <SPAN style="color:#00007F">Then</SPAN>
                        hParent = hWindow: hWindow = 0
                        hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)
                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">Else</SPAN>
            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> (bNotFirstVisibleTime) <SPAN style="color:#00007F">Then</SPAN>   <SPAN style="color:#007F00">'find hClip if window is visible</SPAN>
                <SPAN style="color:#00007F">Do</SPAN>
                    hExcel2 = FindWindowEx(hMain, hExcel2, "EXCEL2", vbNullString)

                    hParent = hExcel2: hWindow = 0
                    hWindow = FindWindowEx(hParent, hWindow, "MsoCommandBar", sTask)
                    <SPAN style="color:#00007F">If</SPAN> hWindow <SPAN style="color:#00007F">Then</SPAN>
                        hParent = hWindow: hWindow = 0
                        hWindow = FindWindowEx(hParent, hWindow, "MsoWorkPane", vbNullString)
                        <SPAN style="color:#00007F">If</SPAN> hWindow <SPAN style="color:#00007F">Then</SPAN>
                            hParent = hWindow: hWindow = 0
                            hClip = FindWindowEx(hParent, hWindow, "bosa_sdm_XL9", vbNullString)
                            <SPAN style="color:#00007F">If</SPAN> hClip > 0 <SPAN style="color:#00007F">Then</SPAN>
                                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN>
                            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
                <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> hExcel2 > 0
                bNotFirstVisibleTime = <SPAN style="color:#00007F">True</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>

    <SPAN style="color:#00007F">If</SPAN> hClip = 0 <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Cant find Clipboard window"
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Call</SPAN> PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter)
    <SPAN style="color:#00007F">Call</SPAN> PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

</FONT>
 
Upvote 0
I tested your modification and I like it.

To address your question: "I am assuming that the references to api clipboard functions are to control the windows clipboard, not the Office clipboard, since they seemed to do nothing to the status of the Office Clipboard, so I deleted those. (???did I do the right thing?) "

The calling code below for the code I posted, should clear all the memory hogs for the copy/paste history:

1. Clear the Office Clip Board.
2. Clear the Windows Clip Board
3. Clear the last Cut/Copy/Paste cell tag.

So if you need to use all of this or not is your call. I found for my use this works well?

My original use for this was for an application that opens an e-mail [data form that lists information about job assignments] copies the info, pastes it into an Excel database scheduling system. Then CLEARS all the copy/paste history setting up the application for importing the next assignment or information updating the progress of an existing job.

So I needed it all!

Sub myClr()
'Sheet Module Code, like: Sheet1.

Call ClearOfficeClipboard
apiOpenClipboard (0)
apiEmptyClipboard
apiCloseClipboard
Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks for the extra explanation, I understand now. I probably would only benifit from clearing the last Cut/Copy/Paste cell tag in this project, so I will use that.

Forgot to mention that sleeps and doevents are controlled in my main routine, so I did not need these in this sub either, so my posted code removed those too.

Also forgot to mention that this code solved an issue I had before too...now I can modify it to automatically open the clipboard window and leave it open rather than having to remember to open it manually. Sure wish I could leave it closed without flicker though!

Thanks again! I'll move on to the next phase of my project now.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top