Link to home
Start Free TrialLog in
Avatar of juliancrawford
juliancrawford

asked on

Selecting text to clipboard from current active window.

I have a VB app that I have registered a hotkey with so that when i select Control & 1 my task is launched.The hotkey is working fine but I am unable to find how code my task to copy the selected highlighted text to the clipboard from the CURRENT active window.
I understand that ClipBoard.SetText Screen.ActiveControl.SelText can be used to capture the selected text from a form - but how do I get it to capture the selected text of the active window - whether it be highlighted text from a word document, web page or whatever text I have currently highlighted in any application currently.

Below is the code I am using ..

---- Codes in a BAS module
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Public Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, _
     ByVal nIndex As Long, _
     ByVal dwNewLong As Long) As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Const SW_NORMAL = 1
Public Const GWL_WNDPROC = -4
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4

Public lpPrevWndProc As Long
Public AtomID As Integer

Public Sub SubClass(ByVal hwnd As Long)
    lpPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub UnSubClass(ByVal hwnd As Long)
    Dim lngReturnValue As Long
    If lpPrevWndProc Then _
        Call SetWindowLong(hwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hwnd As Long, _
                    ByVal uMsg As Long, _
                    ByVal wParam As Long, _
                    ByVal lParam As Long) As Long
       
    If uMsg = WM_HOTKEY Then
        ClipBoard.SetText Screen.ActiveControl.SelText  'hotkey code
    End If
    WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)

End Function

----Code in Form module
Private Sub Form_Load()
     AtomID = GlobalAddAtom("Atom1")
     Call RegisterHotKey(hwnd, AtomID, MOD_CONTROL, vbKey1)
     SubClass Me.hwnd
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    UnSubClass Me.hwnd
    Call UnregisterHotKey(hwnd, AtomID)
    Call GlobalDeleteAtom(AtomID)
End Sub

So to summarize my issue - I can copy selected text from a visual basic form into the clipboard but how do I copy the selected highlighted text from the screen from any application where I have selected text to the clipboard using my hotkey.

Many thanks in advance.
Avatar of gimmeadrink
gimmeadrink

Hi

There is no way to directly grab selected text from a edit control (or sim. like RichEdit)

What you must do instead is grab the start and the end positions of the selection
Use sendmessage and EM_GETSEL


Private Declare Function SendMessage Lib "user32"  Alias "SendMessageA" (ByVal hwnd As Long, _
  ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Private Const EM_GETSEL = &HB0

Dim iStart As Long, iEnd As Long, iLength as Long
Dim sAllText As String, sSelectedText as String


'hWnd is the handle to the edit control you want to get the text from
Call SendMessage(hWnd, EM_GETSEL, iStart, iEnd
' Determine the length
iLength = iEnd - iStart

' now use getwindowtext to get all of the text
'Create a buffer
sAllText = String(100, Chr$(0))
'Get the windowtext
GetWindowText Me.hwnd, sAllText, 100
'strip the rest of buffer
sAllText = Left$(MyStr, InStr(sAllText, Chr$(0)) - 1)

sSelectedText = Mid(sAllText, iStart, iLength)

This will give you the selected text in the variable sSelectedText. Im not sure whether this will work for other types of controls as well or not, but its a headstart at least and should work for applications like word.

HTH







You could simulate pressing CTRL + C to copy the selected Text in the active application to the clipboard.

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_CONTROL = &H11
Private Const VK_C = &H43

' press ctrl + c
keybd_event VK_CONTROL, 0, 0, 0
keybd_event VK_C, 0, 0, 0

' release ctrl + c
keybd_event VK_C, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
ASKER CERTIFIED SOLUTION
Avatar of gimmeadrink
gimmeadrink

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually, what's the point in pressing Ctrl-1 instead of Ctrl-C while both do the same thing of copying currently selected text to clipboard?
hahahha, nice point
Avatar of juliancrawford

ASKER

I'm using CTRL-1 to copy the data to the clipboard, which I will then manipulate further to suit my needs.
Let me do a little testing and I will award points.'

Thanks for the input :)
Why don't you use the sendkeys command?

If you start your application in background, it will not get the focus and thus the currently active window will not loose the focus as well.

If your application will run SendKeys ("^c")

Then you can retrieve the data from Clipboard.gettext.

Bye,

Max