Page 56 of 56 FirstFirst ... 646545556
Results 551 to 555 of 555

Thread: Tests Copying, Pasting, API Cliipboard issues. and Rough notes on Advanced API stuff

  1. #551
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,466
    Rep Power
    10

    Sub big_ClearOffPainBouton() ' aka Sub ClearOfficeClipBoard()

    This is post #551
    https://www.excelfox.com/forum/showt...-issues/page56
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page56
    https://bit.ly/40fepOB

    https://www.excelfox.com/forum/showt...ge56#post24317
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page56#post24317
    https://www.excelfox.com/forum/showt...ll=1#post24317
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=24317&viewfull=1#post24317
    http://bit.ly/3Yieb6Q




    Coding for ease of reference to from some other Thread Posts
    https://eileenslounge.com/viewtopic....31849&start=20
    https://eileenslounge.com/viewtopic.php?f=27&t=41223
    https://www.excelfox.com/forum/showt...ll=1#post17969
    https://www.mrexcel.com/board/thread...3#post-5229031
    https://www.excelfox.com/forum/showt...ge55#post24120
    https://eileenslounge.com/viewtopic....321822#p321822 (February 2019 post )
    https://eileenslounge.com/viewtopic....321817#p321817 ( 2024, Post 1 of 3)
    https://eileenslounge.com/viewtopic....321820#p321820 (2024, Post 2 of 3)
    https://eileenslounge.com/viewtopic....321821#p321821 (2024, Part 3 of 3)



    The typical big API coding that was used a lot up until at least 2020 to press that Clear All button ( https://i.postimg.cc/ZRRtvBtx/Clear-...Clipboards.jpg )

    This coding below given by me here is a slightly updated version of a coding of "mine"** from 2019 ( https://eileenslounge.com/viewtopic....246770#p246770
    https://eileenslounge.com/viewtopic....50e2b7#p246838
    )
    There are Just minor coding changes here, but also some layout/ formatting changes to make a better comparison to a slightly later appearing coding from Jaafar Tribak
    https://www.mrexcel.com/board/thread...2#post-5228787
    https://www.excelfox.com/forum/showt...ll=1#post17969

    ** "My" coding was based on a few very similar ones offered in a Thread which originated from Jaafar Tribak, but which at that time did not quite work as required. It’s encouraging that my coding is almost identical to that slightly later one from Jaafar Tribak. He, of course, very likely knew what he was doing. I was frantically blindly empirically trying things, along with a bit of blind intuition.
    So just to stop people getting upset with me again: Most Credit goes to Jaafar Tribak, its all based on some big API codings around the web which in turn I think probably originally started life as a coding from Jaafar Tribak


    Code:
    ' The main source of this coding is API guru Jaafar Tribak. As far as I can tell this  big  coding was something of a standard until at least the end of the last century.  Possibly after that somne smaller ones may have started appearing
    ' Rory http://www.eileenslounge.com/viewtopic.php?f=30&t=31849&p=246770#p246770
    ' Don un CK76 https://www.excelforum.com/excel-programming-vba-macros/1217178-clipboard-not-clearing-application-cutcopymode-false.html
    ' Jaafar Tribak https://www.mrexcel.com/forum/excel-questions/1087948-reset-clear-clipboard-2.html
    ' Rory and https://excelribbon.tips.net/T008938_Determining_Your_Version_of_Excel.html
    ' Jack's 'COMsOLEwollupsActivelyEmmbeddedXratedObjectHookMyBouton version ' https://www.youtube.com/watch?v=jY-PEeX5xYY&t=2s
    ' FOR NON ENGLISH EXCEL avec moi si vou ple La légende du bouton ' ##### http://www.eileenslounge.com/viewtopic.php?f=30&t=31849&p=246770#p246770
    Option Explicit '
    Private Type POINTAPI
     x As Long: Y As Long
    End Type
    Type RECT
     Left As Long
     Top As Long
     Right As Long
     Bottom As Long
    End Type
    '   VBA7 and Win64 Compiler Constant    Two new compiler constants have been introduced to allow code to work across both 32 bit and 64 bit office.     The Win64 constant is true if you are in a 64 bit version of office The Win32 constants is also true for 64 bit office The VBA7 constant is true for Office 2010 or later
        #If VBA7 Then ' The next 5 lines turn red for Excel 2007     VBA7    Nothing ia red for GB 32 Bit Office 2010    Nothing is red for 64 Bit windows
         Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
         Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
         Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
         Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hwnd As LongPtr) As Long
         Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
            #If Win64 Then ' Under is Red in KB Vista 32 Bit
             Private Declare PtrSafe Function AccessibleObjectFromPoint Lib "oleacc" (ByVal arg1 As LongPtr, ppacc As Any, pvarChild As Variant) As Long
            #Else ' ' Under is Red in KB Vista 32 Bit
             Private Declare PtrSafe Function AccessibleObjectFromPoint Lib "oleacc" (ByVal lX As Long, ByVal lY As Long, ppacc As IAccessible, pvarChild As Variant) As Long
            #End If
        Dim hwndClip As LongPtr
        Dim hwndScrollBar As LongPtr
        Dim lngPtr As LongPtr
        #Else
         Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
         Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
         Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
         Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
         Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
         Declare Function AccessibleObjectFromPoint Lib "oleacc" (ByVal lX As Long, ByVal lY As Long, ppacc As IAccessible, pvarChild As Variant) As Long
         Dim hwndClip As Long
         Dim hwndScrollBar As Long
        #End If
    Const GW_CHILD = 5
    Const S_OK = 0
    ' Update Version 2024     https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=24317&viewfull=1#post24317
    Sub big_ClearOffPainBouton() ' aka Sub ClearOfficeClipBoard()         ' OhFolloks
    ' Let Application.DisplayClipboardWindow = True
    Dim tRect1 As RECT, tRect2 As RECT
    Dim tPt As POINTAPI
    Dim oIA As IAccessible
    Dim vKid  As Variant
    Dim lResult As Long
    Dim i As Long
    Static bHidden As Boolean
    Dim MyPain As String 'COMsOLEwollupsActivelyEmmbeddedXratedObjectHookMyBoutonOhFolloks  This section makes the coding work in Office 2003 also
        If CLng(Val(Application.Version)) <= 11 Then  '  Case 11: "Excel 2003" Windows  "Excel 2004"
         Let MyPain = "Task Pane"
        Else
         Let MyPain = "Office Clipboard"
        End If
        
        If CommandBars(MyPain).Visible = False Then
         bHidden = True
         CommandBars(MyPain).Visible = True            ' Opens the viewer thing for the Office clipboard on XL 2007 +  but sometimes opens the OfficeOnline in XL 2003?
         Let Application.DisplayClipboardWindow = True ' Just incase the last line did not work
         Application.OnTime Now + TimeValue("00:00:01"), "big_ClearOffPainBouton": Exit Sub
        End If
    
    Let hwndClip = FindWindowEx(Application.hwnd, 0, "EXCEL2", vbNullString)
    Let hwndClip = FindWindowEx(hwndClip, 0, "MsoCommandBar", CommandBars(MyPain).NameLocal)
    Let hwndClip = GetNextWindow(hwndClip, GW_CHILD)
    Let hwndScrollBar = GetNextWindow(GetNextWindow(hwndClip, GW_CHILD), GW_CHILD)
        
        If hwndClip And hwndScrollBar Then
         GetWindowRect hwndClip, tRect1
         GetWindowRect hwndScrollBar, tRect2
         BringWindowToTop Application.hwnd
            For i = 0 To tRect1.Right - tRect1.Left Step 50
             tPt.x = tRect1.Left + i: tPt.Y = tRect1.Top - 10 + (tRect2.Top - tRect1.Top) / 2
                #If VBA7 And Win64 Then
                 CopyMemory lngPtr, tPt, LenB(tPt)
                 Let lResult = AccessibleObjectFromPoint(lngPtr, oIA, vKid)
                #Else
                 Let lResult = AccessibleObjectFromPoint(tPt.x, tPt.Y, oIA, vKid)
                #End If ' ##### avec moi si vou ple La légende du bouton
                If InStr("Clear All Borrar todo Effacer tout Alle löschen La légende du bouton", oIA.accName(vKid)) Then
                Call oIA.accDoDefaultAction(vKid) ' This does the clearing,  and
                     CommandBars(MyPain).Visible = Not bHidden ' this closes the viewer thing for the Office clipboard
                     Let bHidden = False
                     Exit Sub
                End If
             DoEvents
            Next i
        End If
    Let CommandBars(MyPain).Visible = Not bHidden
     MsgBox "Unable to clear the Office Clipboard"
    End Sub
    Last edited by DocAElstein; 11-05-2024 at 05:32 PM.

  2. #552
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,466
    Rep Power
    10
    This is post #552
    https://www.excelfox.com/forum/showt...ge56#post24323
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page56#post24323
    https://www.excelfox.com/forum/showt...ll=1#post24323
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=24323&viewfull=1#post24323




    This smaller coding below is an alternative to the big one in the previous post.
    It’s based on some experiments done by Jaafar Tribak in 2019 , when he was trying to get a coding to work in Office 2016. Whilst he failed at the time to get a solution to work in Office 2016, some of us possibly overlooked that this basic coding idea worked in Office 2013 and lower.
    So we did at least have another smaller alternative solution to the big one in the previous post.

    Code:
    ' new small one first occurrance we missed in 2019 at mrexcel    https://www.mrexcel.com/board/threads/reset-clear-clipboard.1087948/#post-5228633
    Option Explicit
        #If VBA7 Then
         Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
        #Else
         Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
        #End If
    
    
    '   https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page56#post24323    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=24323&viewfull=1#post24323
    Sub small_2019_ClearOfficeClipBoard()  ' Slightly modified attempt of Jaafar Tribak from 2019 to  do the  Offices Clipboard Viewer   Clear All   button   https://www.mrexcel.com/board/threads/reset-clear-clipboard.1087948/#post-5228633     https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page11#post17966
    Dim avAcc, bClipboard As Boolean, j As Long
    Dim MyPain As String
        If CLng(Val(Application.Version)) <= 11 Then  '       Case 11: "Excel 2003" Windows  "Excel 2004"
         Let MyPain = "Task Pane"
        Else
         Let MyPain = "Office Clipboard"
        End If
    Set avAcc = Application.CommandBars(MyPain)   '
    Let bClipboard = avAcc.Visible
        If Not bClipboard Then
         Let avAcc.Visible = True
         DoEvents
        End If
        For j = 1 To 4                '  J=  1, 2, 3, 4
         AccessibleChildren avAcc, Choose(j, 0, 3, 0, 3), 1, avAcc, 1
        Next
    avAcc.accDoDefaultAction 2&  '       This seems to do the clearing                     1& for paste
    Let Application.CommandBars(MyPain).Visible = bClipboard         '
    End Sub
    Last edited by DocAElstein; 10-28-2024 at 03:46 PM.

  3. #553
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,466
    Rep Power
    10
    Last edited by DocAElstein; 10-28-2024 at 08:52 PM.

  4. #554
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,466
    Rep Power
    10
    This is post #554
    https://www.excelfox.com/forum/showt...ge56#post24879
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues/page56#post24879
    https://www.excelfox.com/forum/showt...ll=1#post24879
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues?p=24879 &viewfull=1#post24879




    This is my latest attempt as of October 2024 for a small coding version that will work across Office versions from 2003 up to the latest.
    For recent discussions, see here
    https://eileenslounge.com/viewtopic....321822#p321822

    https://eileenslounge.com/viewtopic....321817#p321817
    https://eileenslounge.com/viewtopic....321820#p321820
    https://eileenslounge.com/viewtopic....321821#p321821

    https://stackoverflow.com/questions/...37208#79137208


    Code:
    Option Explicit      https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues-and-otes-on-API-stuff?p=24879&viewfull=1#post24879
        #If VBA7 Then
         Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
        #Else
         Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
        #End If
    Sub small_20202024_ClearOfficeClipBoard_()  ' https://eileenslounge.com/viewtopic.php?p=319159&sid=a5636ddee2213f0629c9f46423c324c5#p319159
    Dim avAcc, bClipboard As Boolean, j As Long
    Dim MyPain As String
        If CLng(Val(Application.Version)) <= 11 Then  '                   Case 11: "Excel 2003" Windows    "Excel 2004" mac
         Let MyPain = "Task Pane"
        Else
         Let MyPain = "Office Clipboard"
        End If
    Set avAcc = Application.CommandBars(MyPain)   '
    Let bClipboard = avAcc.Visible      '   bClipboard will be false if the viewer pain is not open
        If Not bClipboard Then
         avAcc.Visible = True           '   This opens the Viewer pain. The coding won't work if it is not open
         DoEvents: DoEvents
        Else
        End If
    '   coding change for Office versions at  --  Office 2016  ==
        If CLng(Val(Application.Version)) < 16 Then
    ' --For Office versions 2003 2007 2010 2013 ----------------------------------------
            For j = 1 To 4         '      J =    1  2  3  4
             AccessibleChildren avAcc, Choose(j, 0, 3, 0, 3), 1, avAcc, 1
            Next
         avAcc.accDoDefaultAction 2&  '           This seems to do the clearing   It will NOT error if viewer pain is already  Cleared                  1& for paste
    ' ----------------------------------------------------------------------------------
        Else
    ' ==For Office versions 2016 and higher ==============================================
            For j = 1 To 7      '           J =  1  2  3  4  5  6  7
             AccessibleChildren avAcc, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, avAcc, 1
            Next
         avAcc.accDoDefaultAction 0& '            This seems to do the clearing   It WILL error if viewer pain is already  Cleared
        End If ' =======================================================================
     Let Application.CommandBars(MyPain).Visible = bClipboard      '   Puts the viewer pain back as it was, open or closed
    End Sub
    Last edited by DocAElstein; 12-03-2024 at 05:08 PM.

  5. #555
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,466
    Rep Power
    10
    Forum Post #post24931 Thread Post 2824
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues-and-otes-on-API-stuff?p=24931&viewfull=1#post24931
    https://www.excelfox.com/forum/showt...ll=1#post24931
    https://www.excelfox.com/forum/showthread.php/2824-Tests-Copying-pasting-Cliipboard-issues-and-otes-on-API-stuff/page56#post24931
    https://www.excelfox.com/forum/showt...ge56#post24931







    December, 2024, two months later
    On and off I have been looking a bit more at all this VBA windows API stuff, and got a few insights, comments etc., in a few places such as here
    2024:
    http://www.eileenslounge.com/viewtop...321821#p321821
    http://www.eileenslounge.com/viewtop...321838#p321838 It works by ensuring that the 'Clear All' button is displayed on the screen, then searching through the windows hierarchy find the window that represents the panel that contains the button, then obtains the Accessibilty COM interface for that button (by looking for what control with an Accessibility interface is at a specific point, first ensuring that nothing unexpected is covering that point), and then uses that Accessibility interface to invoke the (default) action the button(hopefully the 'Clear All' button).

    It's just a different route to the same thing - the 'small' code walks through the relevant part of the application's accessibility hierarchy instead of having to search for relevant windows, and ends up at the same point - an Accessibility COM interface to the 'Clear All' button (well, depending on the version of Office ...)
    http://www.eileenslounge.com/viewtop...321985#p321985
    http://www.eileenslounge.com/viewtop...322029#p322029
    http://www.eileenslounge.com/viewtop...322075#p322075 …. window from a programmers perspective: …. Parent-Child Relationships: Windows can have hierarchical relationships. A parent window can host multiple child windows, organizing the interface within a main container. This setup is commonly seen in forms containing controls such as buttons, text boxes, or custom graphics elements. ……the Windows API provides us with tools (Win32 API functions such as FindWindow and FindWindowEx)) that let us walk that hierarchy, but they do NOT give us tools to jump direct to any window we want.

    But here’s the thing – not every window we see is necessarily a Windows window (by which I mean one that is managed by the OS), and that means the standard functions cannot walk the tree.

    This is the root cause of the difference in methodology between ‘big’ and ‘small’ code examples you've been looking at

    One is trying to walk the classic hierarchy – and runs into trouble when it hits NetUIHWND, because that is the bottom of the barrel, there are no children. (Which is why Spy+ and all the Spy+ workalikes such as WinSpy fail here – there’s no more hierarchy for them to walk and display)

    The other recognises this problem, and works on the fact that the Ribbon interface maintains its own hierarchy, and that hierarchy is sort of exposed by the Accessibility interface (iAccessible). So, instead of walking down the classic windows tree, we walk down the hierarchy presented to the iAccessible interface. It has the same limitations - there is no way to jump direct to a specific element in the hierarchy; you have to use the functions provided by the oleacc dll. (Accessibility can also be used to walk the classical windows hierarchy)
    http://www.eileenslounge.com/viewtop...322084#p322084

    2019 ++
    http://www.eileenslounge.com/viewtop...321822#p321822

    http://www.eileenslounge.com/viewtop...322424#p322424

    Lists:
    http://www.eileenslounge.com/viewtop...322050#p322050
    http://www.eileenslounge.com/viewtop...322151#p322151

    http://www.eileenslounge.com/viewtopic.php?f=30&t=41610
    http://www.eileenslounge.com/viewtop...322238#p322238 2's flippin compliment +1 ?? https://www.excelfox.com/forum/showt...ll=1#post24921
    http://www.eileenslounge.com/viewtop...322270#p322270
    http://www.eileenslounge.com/viewtop...322357#p322357

    Spys and VBA windows aoi
    https://www.excelfox.com/forum/showt...ll=1#post24913
    https://www.excelfox.com/forum/showt...ll=1#post24908
    https://www.excelfox.com/forum/showt...ll=1#post24914
    https://www.excelfox.com/forum/showt...ll=1#post24909
    https://www.excelfox.com/forum/showt...ll=1#post24915
    https://www.excelfox.com/forum/showt...dows-API/page2
    https://www.excelfox.com/forum/showt...ll=1#post24921
    https://www.excelfox.com/forum/showt...ll=1#post24922 2’s Compliment Function Decimal To Binary conversion
    https://www.excelfox.com/forum/showt...ll=1#post24922
    https://www.excelfox.com/forum/showt...ll=1#post24923

    https://www.excelfox.com/forum/showt...ll=1#post24925
    https://www.excelfox.com/forum/showt...ll=1#post24926
    https://www.excelfox.com/forum/showt...ll=1#post24932


    https://stackoverflow.com/questions/...37208#79137208
    https://www.youtube.com/watch?v=C43b...Lza_0st4AaABAg
    https://www.youtube.com/watch?v=C43b...S24NBeR4AaABAg
    https://www.youtube.com/watch?v=C43b...2UjDK8d4AaABAg
    https://www.youtube.com/watch?v=suUq...pqtdqPx4AaABAg
    https://www.youtube.com/watch?v=suUq...OoGABZFQ8vjEvY
    https://www.youtube.com/watch?v=C43b...t_HABa3KswxL3c
    https://www.youtube.com/watch?v=C43b...t_HABa3tnAjhZU
    https://www.youtube.com/watch?v=3t8M...IOGABa4I83JelY
    https://www.youtube.com/watch?v=3t8M...IOGABa4Pr15NUt
    https://www.youtube.com/watch?v=3t8M...IOGABa6BSa173Z



    Last edited by DocAElstein; 12-03-2024 at 11:18 PM.
    Seasonal greetings :-)

Similar Threads

  1. Replies: 21
    Last Post: 12-15-2024, 07:13 PM
  2. Replies: 114
    Last Post: 03-04-2024, 02:39 PM
  3. Replies: 42
    Last Post: 05-29-2023, 01:19 PM
  4. Some Date Notes and Tests
    By DocAElstein in forum Test Area
    Replies: 0
    Last Post: 11-23-2021, 10:40 PM
  5. Replies: 11
    Last Post: 10-13-2013, 10:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •