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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Sendkeys" problem

Status
Not open for further replies.

PortyAL

Technical User
May 13, 2005
126
GB
Hi

I use the following code to create an e-mail. (The original code is quite lengthy so I have deleted sections which are not relevant to my question).


Code:
Private Sub Command184_Click()
If IsNull(Forms![form1]![PAR Due1]) = True Then GoTo norespdate

Rem On Error GoTo nofiles

Dim strbody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim MailTo As String
Dim attnameDR As String
Dim attnameAP As String

attnameDR = Forms![form1]![docfolder] & "\" & Forms![form1]![Job] & " Final Report.doc"
attnameAP = Forms![form1]![docfolder] & "\Signed Action Plan.pdf"

'**creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

(SECTION OF CODE DELETED)


'***creates email
 With objEmail
     .To = Forms("form1").Controls("empname").Value
     .CC = Forms("form1").Controls("contact").Value & ";Brona Slevin"
     .Subject = "Final Internal Audit Report - " & Forms![form1]!Job
     .BodyFormat = olFormatHTML
     .htmlBody = strbody
     .Importance = olImportanceHigh
     .ReadReceiptRequested = True
     .Attachments.Add attnameDR
     .Attachments.Add attnameAP
     .Display
 Rem .SaveAs Forms("form1").Controls("docfolder").Value & "\Email - Final Report.msg", olMSG
SendKeys "{pgdn}{pgdn}{enter}%is{enter}"
SendKeys "%obpcbc{enter}"

 
 End With

Set objEmail = Nothing

Exit Sub

nofiles:
MsgBox "Final Report or Action Plan is not present", vbInformation, "Cannot create e-mail"
Exit Sub

norespdate:
MsgBox "PAR Due not completed.", vbInformation, "Cannot create e-mail"
Exit Sub
End Sub

My query relates to the "Sendkeys" part of the code i.e.

Code:
 SendKeys "{pgdn}{pgdn}{enter}%is{enter}"
SendKeys "%obpcbc{enter}"

This inserts a corporate logo as background to the e-mail and my signature at the bottom.

This works roughly every other time. When it does not work, the e-mail is created, but the background and signature are not inserted. I then close the created e-mail and run the routine again and it usually works OK the second time.

What would be the reason for this?

Thanks

AL
 
The companion of SendKeys is AppActivate (as clearly stated in the VBA help).

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I don't know this for sure, but practically every action you can do in Outlook (or any of the Office apps) has an equivalent VBA method. If you can find the VBA method, then you can get rid of the untrusty SendKeys way of doing it.

 
Been down that way before Joe without much success. I have since been advised to use DoEvents before the SendKeys function, and it appears to have solved the problem.

AL
 
Just a note - Put the sendkeys in a single command, otherwise you can get the unusual side effect of having the num lock turn off
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top