|
-
March 12th, 2010, 04:37 PM
#1
Need help with Macro: Copy selection from one app insert to another application!
Hi
I think this is an appropriate section to start this thread:
I will put an application window that we all share called: "Map Network Drive" (see the pic). Found in My Computer, Tools, Map Network Drive...
http://i49.tinypic.com/63srqe.jpg
1.
From this window, I would like to tab a specific amount of times (without using SendKeys) until I reach the text-area, right side of Folder.
2.
I would like to select and copy the manually typed text (in this case: "Wish to copy this selection").
3.
Then insert the text to a new Mail Item.
I would like to insert the text without using (see bold:
With OutlookMessage
.body = storeddata
End With
/you may ask why, it's because, what if I insert the text to another application (that application wont have .body).
Can you help me with this? I provide the initial code (with comments) I have for the whole macro.
PS: Is it possible to force the macro to work in the background without getting interrupted or copying from / insert to, wrong application?. ---->
Example of this problem occur when using sendkeys.
Say you are using a lot of sendkeys command, and all of a sudden middle of the macro another window pops up. The sendkeys will instead tab, enter, write etc in that window instead... And that is not good.
The initial part of the code:
Code:
Public Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Public Declare Function IsIconic Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Public Declare Function AttachThreadInput Lib "user32" _
(ByVal idAttach As Long, _
ByVal idAttachTo As Long, _
ByVal fAttach As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" _
() As Long
Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const SW_RESTORE = 9
Public Const SW_SHOW = 5
Public Const WM_PASTE = &H302
Public Const WM_COPY = &H301
Public Type FindWindowParameters
strTitle As String 'INPUT
hwnd As Long 'OUTPUT
End Type
String/ Long /Object /etc values
The next is our SUB code.
See me comments in the code. Also, I cant get this part to work as intended. Macro doesn't select the text, the macro doesn't copy the text (tried with manually selecting text) and it doesnt paste the text to another textbox area.
I really don't know how to write the code...
Code:
Sub GetStringValue()
Dim lhwndparent As Long
Dim CopyWord As String
Dim MyData As New DataObject
Set MyData = New DataObject
Dim OutlookMessage As Object
Dim oMailItem As MailItem
lhwndparent = FnSetForegroundWindow("Map Network Drive")
JumpToPreviousControl (8) 'Tab the fields until we reach our textbox destination
Call SendMessage(intRet, EM_SETSEL, 0&, 0&) ' I know this doesnt work (tried with Notepad with unmarked text)
CopyWord = SendMessage(lhwndparent, WM_COPY, 0&, 0&) 'This doesnt work either
'Now creating a new mail item.
Set OutlookMessage = CreateObject("Outlook.application").CreateItem(0)
lhwndparent2 = FnSetForegroundWindow("Untitled*") 'this is the next application/window that I will insert my data.
'store the text in the clipboard
MyData.SetText CopyWord
CopyWord = MyData.GetText
MyData.PutInClipboard
SendMessageString lhwndparent2 , WM_PASTE, 0, CopyWord 'here to insert our text in another application textbox area (in this case our mail item)
End Sub
Function to tab through textbox areas (I need a code that replaces SendKeys)
Code:
' Jumps backward a specified number of times. Private Sub JumpToPreviousControl(ByVal times As Integer)
Dim i As Integer
For i = 1 To times
SendKeys ("+{Tab}"), True
Next
End Sub
Last is our FindWindow function
Code:
'
Public Function FnFindWindowLike(strWindowTitle As String) As Long
'We'll pass a custom structure in as the parameter to store our result...
Dim Parameters As FindWindowParameters
Parameters.strTitle = strWindowTitle ' Input parameter
Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
FnFindWindowLike = Parameters.hwnd
End Function
Private Function EnumWindowProc(ByVal hwnd As Long, _
lParam As FindWindowParameters) As Long
Dim strWindowTitle As String
strWindowTitle = Space(260)
Call GetWindowText(hwnd, strWindowTitle, 260)
strWindowTitle = TrimNull(strWindowTitle) ' Remove extra null terminator
If strWindowTitle Like lParam.strTitle Then
lParam.hwnd = hwnd 'Store the result for later.
EnumWindowProc = 0 'This will stop enumerating more windows
End If
EnumWindowProc = 1
End Function
Private Function TrimNull(strNullTerminatedString As String)
Dim lngPos As Long
'Remove unnecessary null terminator
lngPos = InStr(strNullTerminatedString, Chr$(0))
If lngPos Then
TrimNull = Left$(strNullTerminatedString, lngPos - 1)
Else
TrimNull = strNullTerminatedString
End If
End Function
Public Function FnSetForegroundWindow(strWindowTitle As String) As Boolean
Dim MyAppHWnd As Long
Dim CurrentForegroundThreadID As Long
Dim NewForegroundThreadID As Long
Dim lngRetVal As Long
Dim blnSuccessful As Boolean
MyAppHWnd = FnFindWindowLike(strWindowTitle)
If MyAppHWnd <> 0 Then
'We've found the application window by the caption
CurrentForegroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), ByVal 0&)
NewForegroundThreadID = GetWindowThreadProcessId(MyAppHWnd, ByVal 0&)
'AttachThreadInput is used to ensure SetForegroundWindow will work
'even if our application isn't currently the foreground window
'(e.g. an automated app running in the background)
Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, True)
lngRetVal = SetForegroundWindow(MyAppHWnd)
Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, False)
If lngRetVal <> 0 Then
'Now that the window is active, let's restore it from the taskbar
If IsIconic(MyAppHWnd) Then
Call ShowWindow(MyAppHWnd, SW_RESTORE)
Else
Call ShowWindow(MyAppHWnd, SW_SHOW)
End If
blnSuccessful = True
Else
MsgBox "Found the window, but failed to bring it to the foreground!"
End If
Else
'Failed to find the window caption
'Therefore the app is probably closed.
MsgBox "Application Window '" + strWindowTitle + "' not found!"
End If
FnSetForegroundWindow = blnSuccessful
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|