Solved

Selecting text to clipboard from current active window.

Posted on 2003-11-09
7
760 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:juliancrawford
7 Comments
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 9711940
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







0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9712116
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
0
 
LVL 2

Accepted Solution

by:
gimmeadrink earned 250 total points
ID: 9712178
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:Belfry
ID: 9712186
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?
0
 
LVL 2

Expert Comment

by:gimmeadrink
ID: 9712376
hahahha, nice point
0
 
LVL 5

Author Comment

by:juliancrawford
ID: 9712579
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 :)
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9713811
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now