Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve selected text from to another application

Status
Not open for further replies.

Groom84

Technical User
Feb 27, 2009
19
0
0
SE
Hi
I need some help, and to ask my question in the easiest way I will put an application window that we all share called: "Map Network Drive" (see the pic).


In that window, there is an textbox area (right to folder), where you can write, copy, etc.

What I would like to do in my macro is:

Copy the selected text, transform it into a string value and then insert it into another application.
And this without using SendKeys.


See the codes on my reply below.

PS: Outlook mail item and Map Network Drive are only, example apps/windows. In reality I will use other applications.

(I had some trouble with an earlier thread, sorry about that, hope it will go better on this one).
 

Simple VBA version (works, but I want to avoid sendkeys):
Code:
Sub GetStringValue()

Dim Sig As String

Dim OutlookMessage As Object


AppActivate ("Map Network Drive")
SendKeys ("^C"), True   'Need to avoid sendkeys, like using WM_COPY


Set OutlookMessage = CreateObject("Outlook.application").CreateItem(0)
AppActivate ("NewMailItem")
SendKeys ("^V"), True   'Need to avoid sendkeys, like using WM_PASTE


API version (does not work):
Code:
Private 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

Private Const WM_SETTEXT = &HC

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindow Lib "user32" _
(ByVal hWnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) 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 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 Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_PASTE = &H302
Private Const WM_COPY = &H301
'end



Sub GetStringValue()

    Dim lHwnd As Long, stext As String, sTitle As String, lhwndparent As Long, lmainbody As Long
    Dim MyData As New DataObject    '<<<----- Had to enter this as code required an object for MyData.
    Set MyData = New DataObject      '<<<----- Had to enter this as code required an object for MyData.
    Dim MyStr As String
    Dim Maitem As String
Dim OutlookMessage As Object
Dim oMailItem As MailItem


Dim sWinText As String, slen As Long, lret As Long

sTitle = "Map Network Drive"

lhwndparent = FindWindow(vbNullString, sTitle)
If lhwndparent <> 0 Then lmainbody = FindWindowEx(lhwndparent, 0&, "Edit", vbNullString)
If lmainbody <> 0 Then

slen = SendMessage(lmainbody, WM_GETTEXTLENGTH, ByVal 0&, ByVal 0& + 1)

sWinText = Space(slen)
lret = SendMessage(lmainbody, WM_GETTEXT, ByVal slen, ByVal sWinText)
sWinText = Left(sWinText, lret)

Text1 = sWinText

End If

Set OutlookMessage = CreateObject("Outlook.application").CreateItem(0)

Maitem = "OutlookMailitem" 'this is the next application that I will paste my data.


    '~~> Sample text
    stext = Text1    '<<<----- This part is unquoted
    MyData.SetText stext  ' Had to give MyData an value or I received an error.
    MyData.PutInClipboard
    stext = MyData.getText
    



    '~~> Find The Outlook mail
    lHwnd = FindWindow(vbNullString, Maitem)


    
    '~~> Trying to Get Hold of the Edit Area of Outlook email item
    lHwnd = FindWindowEx(lHwnd, 0, "Edit", vbNullString)
    SendMessage lHwnd, WM_SETTEXT, 0, stext 'WM_SETTEXT

End Sub
 




Hi,

You already have thread707-1592342

Why are you posting a new thread on the same topic?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I think I caused some kind of confusion. So I thought creating a new topic would simplify...
 
But it is not really a new topic.

I still think you may have better luck posting to the API forum, as you are using API. This is not strictly speaking a VBA issue. Actually...it is not a VBA issue at all.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top