Question

I'm trying to use a late binding to email from VBA in Access 2010 and Access 2003. 2003 gives me 'Could not complete the operation. Once or more paramet values are not valid.' and 2010 gives me 'Invalid procedure call or argument.' I've done the step through and it fails at .send near the bottom. Am I setting up my binding wrong? I'm trying to do this without using Microsoft Object Library in the References.

Thanks.

'Refers to Outlook's Application object
Dim appOutlook As Object
'Refers to an Outlook email message
Dim appOutlookMsg As Object
'Refers to an Outlook email recipient
 Dim appOutlookRec As Object

'Create an Outlook session in the background
Set appOutlook = CreateObject("Outlook.Application")

'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)

'Using the new, empty message...
With appOutlookMsg

strSQL = "SELECT Email FROM Employees WHERE " & sqlVar & " = True"
Set myR = CurrentDb.OpenRecordset(strSQL)

Do While Not myR.EOF
Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olTo
myR.MoveNext
Loop

strSQL = "SELECT Email FROM Employees WHERE '" & user & "' = Username"    
Set myR = CurrentDb.OpenRecordset(strSQL)

Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olCC

.Subject = wonum
.Body = "Department: " & strDep & vbNewLine & vbNewLine & _
    "Issue is at: " & strIssue & vbNewLine & vbNewLine & _
    "Priority is: " & strPriority & vbNewLine & vbNewLine & _
    "Complete by: " & strDate & vbNewLine & vbNewLine & _
    "Description: " & strDesc

.Send

End With
Was it helpful?

Solution

Without a reference, VBA will not know about Outlook constants such as olTo and olCC. With late binding (no reference), you must supply the values for constants rather than the names of the constants.

However, since you didn't report a compile error about those constants, that suggests your code module does not include Option Explicit in its Declarations section. Trying to troubleshoot VBA code without Option Explicit is a waste of time.

Add Option Explicit, then choose Debug->Compile from the VB Editor's main menu and fix anything the compiler complains about. Resume you troubleshooting afterward.

OTHER TIPS

There is an article here on sending email via Outlook using early and late binding. In the "Late Bound Conversion Checklist" at the end, the last suggestion is

Add optional arguments that have a default value

I cannot vouch for that advice because when I need to send email messages from Access I use CDO, not Outlook. Still, it sounds like it might be worth a try.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top