PDA

View Full Version : paste without clipboard manager



TrippyTom
02-15-2007, 12:20 PM
Hi everyone.

I think I have a relatively easy question here: I want to be able to define a certain string to a hotkey (like ALT + V) in Windows so it will work in any application I'm in. The problem is, I can't install things at work (IT gets mad if I try)... so is there an easy way to make a generic macro to do this in Windows 2000?

Brandtrock
02-17-2007, 11:37 PM
This is a routine that I yoinked from VBA Programmer (http://www.vba-programmer.com/Snippets/Code_VB/Creating_a_Global_Hotkey.html) which may be of some use in getting you pointed in the direction you want to go.

Hope it helps.

' API Constants & Declarations
Public Const GWL_WNDPROC As Long = (-4)
Public Const WM_HOTKEY As Long = &H312

Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4

Public Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" _
(ByVal lpString As String) As Long

Public Declare Function GlobalDeleteAtom Lib "kernel32" _
(ByVal nAtom As Long) As Long

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 SetWindowLong Lib "User32" Alias "SetWindowLongA" _
(ByVal hWND As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Public 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

Public mlngLastWndProc As Long
Public mlngAtom As Long

' Functions and Subroutines to store in main module
' Function which determines what happens when HotKey pressed
Function WindowProc(ByVal hWND As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

If hWND = Clipboard_Mgr_Frm.hWND And uMsg = WM_HOTKEY Then
Clipboard_Mgr_Frm.Text1.Text = Clipboard_Mgr_Frm.Text1.Text & _
Clipboard.GetText(vbCFText) & vbCrLf & vbCrLf
End If

WindowProc = CallWindowProc(mlngLastWndProc, hWND, uMsg, wParam, lParam)
End Function

' Subroutine which creates the Hot Key
Public Sub CreateHotKey(ByVal intKeyCode As Integer, ByVal hWND As Long)
mlngAtom = GlobalAddAtom(CStr(Now))
mlngLastWndProc = SetWindowLong(hWND, GWL_WNDPROC, AddressOf WindowProc)
RegisterHotKey hWND, mlngAtom, 0, intKeyCode
End Sub

' Subroutine which destroys the hot key
Public Sub DestroyHotKey(ByVal hWND As Long)
UnregisterHotKey hWND, GlobalDeleteAtom(mlngAtom)
Call SetWindowLong(hWND, GWL_WNDPROC, mlngLastWndProc)
End Sub

' Procedures in a particular form
Private Sub Form_Load()
CreateHotKey vbKeyF2, Me.hWND
' Message below to let user know hot key has been created and what it does.
MsgBox "F2 is now a HOT KEY which will copy the clipboard contents" & vbCrLf & _
"To the textbox (minimized) - from ANYWHERE on the system!" & vbCrLf & vbCrLf & _
"REMEMBER to hit both Ctrl + C and F2 when copying text." & vbCrLf & vbCrLf & _
"MAXIMIZE from the Taskbar to Insert text in Word document.", vbOKOnly, _
" HOT KEY F2 PROCESS START"
End Sub

' Remember to destroy hot key when exiting the form
Private Sub cmdExit_Click()
DestroyHotKey Me.hWND
Unload Me
End
End Sub

Regards,

TrippyTom
02-20-2007, 10:32 AM
Thanks Brandtrock, this looks promising. :thumb

Am I going to get in trouble if I mess with API stuff at work? This, vba arrays and messing with registry items are things I'm afraid of. I know it's probably an irrational fear.

Brandtrock
02-20-2007, 08:20 PM
Care does need to be exercised when working with API calls and registry settings. Checking with the powers that be in the IT/IS department is probably a good idea, although they generally frown on anyone outside of their dominion doing anything more complex than strict alpha numeric data entry. Your relationship with the individuals in the department will be your guide as far as this is concerned.

Having said that, don't be afraid of using these types of solutions. Just be sure of what you are doing ahead of time. Ivan Moala's site (http://www.xcelfiles.com/) has a boat load of information on using API calls.

Regards,

lucas
02-21-2007, 08:12 AM
Hi Tom and Brantrock,
I don't wish to distract from your discussion but I use an excel workbook for pasting data to other apps. I use a worksheet change code that copies whats in the cell when I click on it then I can paste to any application from the clipboard. I know it's a little clunky but it works nicely and I use it often. Only caveat is you have to have the workbook open in the background. Not very elegant but it works.....

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Copy
End Sub