Jump to content

Microsoft Office VBA Editor IDE Ctrl-Y Redo


Page2PagePro
 Share

Recommended Posts

Excel VBA's IDE registers a Control-y as "cut this line of code".

For those prone to Undo/Redo (Ctrl+Z/Ctrl+Y) you may find frustration when your code in the editor does not redo, but in fact clears your active line of code while killing redo history.

Though not perfect, I keep this tool running in background on startup.

The purpose is to allow Cltr+Y to act normally throughout Windows and Office and only interact *differently* with the "Microsoft Visual Basic for Applications" window that is active.

If the Standard Menu bar exists, it'll try to click the ReDo (Blue Arrow to the right), else "Alt+e, r" keystrokes (less desired).

 

Here's the code:

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", 1)

HotKeySet("^y", "TriggerRedo")

While 1
    Sleep(10)
WEnd

Func TriggerRedo()
    ConsoleWrite("TriggerRedo()" & @CRLF)
    Local $title = "Microsoft Visual Basic for Applications - "
    Local $hWnd
    If WinExists($title) And WinActive($title) Then
;~      Parent Window Handle
        $hWnd = WinGetHandle($title)
        Local $aWindowPos = WinGetPos($hWnd)
;~      Control Bar Handle, Position and If Visible
        Local $sControlID = "[CLASS:MsoCommandBar; TEXT:Standard;]"
        Local $hStandardBar = ControlGetHandle($hWnd, "", $sControlID)
        Local $bIsVisible = ControlCommand($hWnd, "", $sControlID, "IsVisible")

        If $hStandardBar And $bIsVisible Then
            ConsoleWrite("Using Mouse Click." & @CRLF)
;~          Determine Redo button location on visible Control Bar
            Local $aBarPos = ControlGetPos($hWnd, "", $sControlID)
            Local $mX = $aWindowPos[0] + $aBarPos[0] + 217 + Int(23/2)
            Local $mY = $aWindowPos[1] + $aBarPos[1] + 27 + Int(22/2)
            MouseClick("Left", $mX, $mY, 1, 0)
        Else
            ConsoleWrite("Using VBA Send Keys." & @CRLF)
            $sControlID = "[CLASS:MsoCommandBar; TEXT:Menu Bar;]"
            Local $hMenuBar = ControlGetHandle($hWnd, "", $sControlID)
            ControlSend($hWnd, "", $hMenuBar, "!e")
;~          Send("r")
            $sControlID = "[CLASS:MsoCommandBarPopup; TEXT:Edit;]"
            Local $hPopupBar = ControlGetHandle($hWnd, "", $sControlID)
            ControlSend($hWnd, "", $hPopupBar, "r")
        EndIf
    Else
        ConsoleWrite("Using NATIVE Send Keys." & @CRLF)
        HotKeySet("^y")
        Send("^y") ;~ may cause "yyy..." when held
        HotKeySet("^y", "TriggerRedo")
    EndIf
EndFunc   ;==>TriggerRedo

Hope this inspires someone.

 

 

Edited by Page2PagePro
Type-o
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...