Solved

Selecting text to clipboard from current active window.

Posted on 2003-11-09
7
763 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

911 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