P
PeteCresswell
I'll confess to shopping around on this one.
Started threads in two of the Outlook NGs, but I'm not getting
anywhere.
This all started when we moved an MS Access app to a Citirx server and
DoCmd.SendObject began provoking
"A program is trying to automatically send e-mail on your behalf."
dialogs - courtesy, as I undestrand it, of Outlook's "Object model
guard".
I'm hearing that the workaround is to use CDO (Collaboration Data
Objects) to feed the messages direct to "the user's SMTP
server" (whatever that is.....)
But in trying to implement that, I'm running into what seems tb a
fairly common problem: "The "SendUsing" configuration value is
invalid."
Tried a few fixes, but found no joy.
Upon reflection, it seems like there's at least one obvious problem:
authentication of the user. No way that SMTP server is going to let
just anybody dump email into it - at least in a well-run corporate
environment. I'd guess that the only hope would be some way to
present the user's credentials (ID/PW) to CDO from VBA.
Bottom Line: Has anybody had success in using CDO to send email from
MS Access?
For the masochistically-inclined, my current source code - which I
can email it as a .txt file if the wrapping is excessively heinous:
-----------------------------------------------------------------------------------------
Public Sub Email_Report(ByVal theObjectName As String,
theReportDescription As String, ByVal theEmailReportSelectionBasis As
Long)
1000 DebugStackPush mModuleName & ": Email_Report"
1001 On Error GoTo Email_Report_err
' PURPOSE: To send a copy of the named report to each person on the
' list in ttblEmailAddresses
' ACCEPTS: - Object name of the report. e.g. "rptMaturities"
' - Description of the report
' - Whether we want addresses selected for report or trade
buy ticket
'
' NOTES: 1) We discontinued using MS Access' .SendObject command
because it was provoking
' an "Object model guard" issue in Outlook - resulting
in an irritating
' "...program is trying to automatically send e-mail on
your behalf."
' confirmation dialog issued by MS Outlook.
'
' Supposedly the CDO.Message object goes direct to
SMTP, bypassing Outlook.
' CDO = Collaboration Data Objects
1002 Dim myRS As DAO.Recordset
Dim myCdoMessage As CDO.Message
Dim myCdoConfig As CDO.Configuration
Dim curAddress As String
Dim myQueryName As String
Dim myTempDir As String
Dim mySnpPath As String
Dim i As Long
'
------------------------------------------------------------------
' Get path to user's "Documents and Settings", then create a Temp
' directory under it
1010 myTempDir = Environ("UserProfile")
1019 myTempDir = myTempDir & "\Temp"
On Error Resume Next
MkDir myTempDir
On Error GoTo Email_Report_err
'
------------------------------------------------------------------
' Create a snapshot of our report in the temp dir, after having
' deleted any pre-existing file
1020 mySnpPath = myTempDir & "\" & theObjectName & ".snp"
On Error Resume Next
Kill mySnpPath
On Error GoTo Email_Report_err
1030 DoCmd.OutputTo acOutputReport, theObjectName, "Snapshot Format",
mySnpPath
'
------------------------------------------------------------------
' Customize CDO configuration as needed
' Docs are in http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx
' PROBLEM: Got to assume that whatever "SMTP Server" is, it's going
to require
' an ID/PW.... but how to get/supply those?
1040 Set myCdoConfig = New CDO.Configuration
1050 With myCdoConfig.Fields
1051 .Item(cdoSendUsingMethod).Value = cdoSendUsingPort
1052 .Item(cdoSMTPServerPort).Value = 25
1053 .Item(cdoSMTPServer).Value = "localhost"
1054 .Update
1059 End With
'
------------------------------------------------------------------
' Create a CDO Message object, whose "TO:" we will customize for
each
1090 Set myCdoMessage = New CDO.Message
1091 With myCdoMessage
1092 .From = CurrentUserGet()
1093 .Subject = theReportDescription
1094 .textbody = theReportDescription & " report attached as .SNP
file."
1095 .AddAttachment mySnpPath
1099 End With
'
------------------------------------------------------------------
' Determine our input query and open our recordset of email
addresses
1110 Select Case theEmailReportSelectionBasis
Case gEmailReportSelectionBasis_Report
1112 myQueryName = "qryEmailAddresses_Selected_Report"
1113 Case gEmailReportSelectionBasis_Trade_Buy
1114 myQueryName = "qryEmailAddresses_Selected_Trade_Buy"
1115 Case Else
1116 BugAlert True, "Unexpected EmailReportSelectionBasis=" &
theEmailReportSelectionBasis & "'."
1119 End Select
1120 Set myRS = CurrentDb.OpenRecordset(myQueryName, dbOpenSnapshot,
dbForwardOnly)
'
------------------------------------------------------------------
' Loop through the email addresses, sending a copy of the message
to each
1130 With myRS
1131 If ((.BOF = True) And (.EOF = True)) Then
1132 MsgBox "Please select at least one eMail address and try
again.", vbExclamation, "Cannot eMail: No Addresses Selected"
1133 Else
1134 Do Until .EOF = True
1139 curAddress = !EmailAddress & ""
1140 If Len(curAddress) > 0 Then
1150 With myCdoMessage
1151 .To = curAddress
1152 .Send
1159 End With
'1159 DoCmd.SendObject acSendReport, theObjectName,
"Snapshot Format", curAddress, , , theReportDescription,
theReportDescription & " report attached as .SNP file...", False
1990 End If
1991 .MoveNext
1992 Loop
1993 End If
1999 End With
Email_Report_xit:
DebugStackPop
On Error Resume Next
Kill mySnpPath
Set myCdoMessage = Nothing
Set myCdoConfig = Nothing
myRS.Close
Set myRS = Nothing
Exit Sub
Email_Report_err:
BugAlert True, "curAddress='" & curAddress & "'."
Resume Email_Report_xit
End Sub
-----------------------------------------------------------------------------------------
Started threads in two of the Outlook NGs, but I'm not getting
anywhere.
This all started when we moved an MS Access app to a Citirx server and
DoCmd.SendObject began provoking
"A program is trying to automatically send e-mail on your behalf."
dialogs - courtesy, as I undestrand it, of Outlook's "Object model
guard".
I'm hearing that the workaround is to use CDO (Collaboration Data
Objects) to feed the messages direct to "the user's SMTP
server" (whatever that is.....)
But in trying to implement that, I'm running into what seems tb a
fairly common problem: "The "SendUsing" configuration value is
invalid."
Tried a few fixes, but found no joy.
Upon reflection, it seems like there's at least one obvious problem:
authentication of the user. No way that SMTP server is going to let
just anybody dump email into it - at least in a well-run corporate
environment. I'd guess that the only hope would be some way to
present the user's credentials (ID/PW) to CDO from VBA.
Bottom Line: Has anybody had success in using CDO to send email from
MS Access?
For the masochistically-inclined, my current source code - which I
can email it as a .txt file if the wrapping is excessively heinous:
-----------------------------------------------------------------------------------------
Public Sub Email_Report(ByVal theObjectName As String,
theReportDescription As String, ByVal theEmailReportSelectionBasis As
Long)
1000 DebugStackPush mModuleName & ": Email_Report"
1001 On Error GoTo Email_Report_err
' PURPOSE: To send a copy of the named report to each person on the
' list in ttblEmailAddresses
' ACCEPTS: - Object name of the report. e.g. "rptMaturities"
' - Description of the report
' - Whether we want addresses selected for report or trade
buy ticket
'
' NOTES: 1) We discontinued using MS Access' .SendObject command
because it was provoking
' an "Object model guard" issue in Outlook - resulting
in an irritating
' "...program is trying to automatically send e-mail on
your behalf."
' confirmation dialog issued by MS Outlook.
'
' Supposedly the CDO.Message object goes direct to
SMTP, bypassing Outlook.
' CDO = Collaboration Data Objects
1002 Dim myRS As DAO.Recordset
Dim myCdoMessage As CDO.Message
Dim myCdoConfig As CDO.Configuration
Dim curAddress As String
Dim myQueryName As String
Dim myTempDir As String
Dim mySnpPath As String
Dim i As Long
'
------------------------------------------------------------------
' Get path to user's "Documents and Settings", then create a Temp
' directory under it
1010 myTempDir = Environ("UserProfile")
1019 myTempDir = myTempDir & "\Temp"
On Error Resume Next
MkDir myTempDir
On Error GoTo Email_Report_err
'
------------------------------------------------------------------
' Create a snapshot of our report in the temp dir, after having
' deleted any pre-existing file
1020 mySnpPath = myTempDir & "\" & theObjectName & ".snp"
On Error Resume Next
Kill mySnpPath
On Error GoTo Email_Report_err
1030 DoCmd.OutputTo acOutputReport, theObjectName, "Snapshot Format",
mySnpPath
'
------------------------------------------------------------------
' Customize CDO configuration as needed
' Docs are in http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx
' PROBLEM: Got to assume that whatever "SMTP Server" is, it's going
to require
' an ID/PW.... but how to get/supply those?
1040 Set myCdoConfig = New CDO.Configuration
1050 With myCdoConfig.Fields
1051 .Item(cdoSendUsingMethod).Value = cdoSendUsingPort
1052 .Item(cdoSMTPServerPort).Value = 25
1053 .Item(cdoSMTPServer).Value = "localhost"
1054 .Update
1059 End With
'
------------------------------------------------------------------
' Create a CDO Message object, whose "TO:" we will customize for
each
1090 Set myCdoMessage = New CDO.Message
1091 With myCdoMessage
1092 .From = CurrentUserGet()
1093 .Subject = theReportDescription
1094 .textbody = theReportDescription & " report attached as .SNP
file."
1095 .AddAttachment mySnpPath
1099 End With
'
------------------------------------------------------------------
' Determine our input query and open our recordset of email
addresses
1110 Select Case theEmailReportSelectionBasis
Case gEmailReportSelectionBasis_Report
1112 myQueryName = "qryEmailAddresses_Selected_Report"
1113 Case gEmailReportSelectionBasis_Trade_Buy
1114 myQueryName = "qryEmailAddresses_Selected_Trade_Buy"
1115 Case Else
1116 BugAlert True, "Unexpected EmailReportSelectionBasis=" &
theEmailReportSelectionBasis & "'."
1119 End Select
1120 Set myRS = CurrentDb.OpenRecordset(myQueryName, dbOpenSnapshot,
dbForwardOnly)
'
------------------------------------------------------------------
' Loop through the email addresses, sending a copy of the message
to each
1130 With myRS
1131 If ((.BOF = True) And (.EOF = True)) Then
1132 MsgBox "Please select at least one eMail address and try
again.", vbExclamation, "Cannot eMail: No Addresses Selected"
1133 Else
1134 Do Until .EOF = True
1139 curAddress = !EmailAddress & ""
1140 If Len(curAddress) > 0 Then
1150 With myCdoMessage
1151 .To = curAddress
1152 .Send
1159 End With
'1159 DoCmd.SendObject acSendReport, theObjectName,
"Snapshot Format", curAddress, , , theReportDescription,
theReportDescription & " report attached as .SNP file...", False
1990 End If
1991 .MoveNext
1992 Loop
1993 End If
1999 End With
Email_Report_xit:
DebugStackPop
On Error Resume Next
Kill mySnpPath
Set myCdoMessage = Nothing
Set myCdoConfig = Nothing
myRS.Close
Set myRS = Nothing
Exit Sub
Email_Report_err:
BugAlert True, "curAddress='" & curAddress & "'."
Resume Email_Report_xit
End Sub
-----------------------------------------------------------------------------------------