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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.


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
actually, thats a good idea

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Call SetForegroundWindow(hWnd)
SendKeys "^c", True

Which is a little more simple


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
juliancrawfordAuthor Commented:
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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.