Solved

Selecting text to clipboard from current active window.

Posted on 2003-11-09
7
770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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