Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

Sendkeys to Outlook - Word Editor

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
Packerland
Asked:
Packerland
  • 2
2 Solutions
 
bobbit31Commented:
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
 
PackerlandAuthor Commented:
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
 
aParserCommented:
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
 
aParserCommented:
It appears that bobbit31 answered the initial question and I believe I filled in a gap.  I recommend a split.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now