Solved

Sendkeys to Outlook - Word Editor

Posted on 2004-08-23
5
559 Views
Last Modified: 2012-06-27
Using VB Pro 6.0

I am trying to wite a VB app that will paste an address into the [To:] field of a new mail message.

Outlook is using Word editor.

When I use [Sendkeys "mailaddr@company.com", True"] command to notepad it works everytime.

When I use [Sendkeys "mailaddr@company.com", True"] command to Outlook it only works occasionally.

Any ideas?  Any alternatives to using sendkeys?

Thanks.
0
Comment
Question by:Packerland
  • 2
5 Comments
 
LVL 18

Assisted Solution

by:bobbit31
bobbit31 earned 250 total points
ID: 11872693
use FindWindow and SendMessage API:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 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 Const WM_SETTEXT = &HC

Private Sub Command1_Click()
    Dim hwnd As Long
    Dim stringToSend As String
   
    hwnd = FindWindow("rctrl_renwnd32", "Untitled - Message (Plain Text) ") '' <-- replace 'Untitled - ... ' w/ the title of the compose mail window
    afxhwnd = FindWindowEx(hwnd, 0, "AfxWnd", vbNullString)
    dlghwnd = FindWindowEx(afxhwnd, 0, "#32770", vbNullString)
    editHwnd = FindWindowEx(dlghwnd, 0, "RichEdit20A", vbNullString)

    If editHwnd > 0 Then '' found the textbox so send the text
        stringToSend = "Hello World!"
        SendMessage editHwnd, WM_SETTEXT, 256, ByVal stringToSend
    End If
End Sub
0
 

Author Comment

by:Packerland
ID: 11904827
Bobbit,

Problem is that I don't know what the window name might be.  If it is a new message then the name is static but if it is forwarded or replied to then the Window name is the subject of the message.

Is there anyway to get a list of all of the Open windows?
0
 
LVL 3

Accepted Solution

by:
aParser earned 250 total points
ID: 11967671
Enumerate All Open Windows (Parent and Children)
http://www.freevbcode.com/ShowCode.Asp?ID=701

I also found something at http://www.mvps.org/access/api/api0051.htm which contains this loop:

  '   Start with the first child of the Desktop window
  hWnd = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
  '   Enumerate all open windows
  Do While Not hWnd = 0
    ...
    hWnd = apiGetWindow(hWnd, GW_HWNDNEXT)
  Loop

The page gives the actual Declares for the APIs.
0
 
LVL 3

Expert Comment

by:aParser
ID: 12380132
It appears that bobbit31 answered the initial question and I believe I filled in a gap.  I recommend a split.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now