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

automatically inserting an email adress

Status
Not open for further replies.

ironj32

Technical User
Dec 7, 2006
73
US
ok, i found this module code in a different thread and it works great. however would anyone know how to select an email address from from a record in a table? i need to pull the email address from: tblPropertyMngrInfo.PropMngrEmail

i am using this code for a button, cmdSendEmail, on each record for a tabular form.

here is the code i am using:

Thanks in advance for any help!


Option Compare Database
Option Explicit

Private Declare Function apiFindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long

Private Declare Function apiSendMessage Lib "user32" Alias _
"SendMessageA" (ByVal Hwnd As Long, ByVal msg As Long, ByVal _
wParam As Long, lParam As Long) As Long

Private Declare Function apiSetForegroundWindow Lib "user32" Alias _
"SetForegroundWindow" (ByVal Hwnd As Long) As Long

Private Declare Function apiShowWindow Lib "user32" Alias _
"ShowWindow" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function apiIsIconic Lib "user32" Alias _
"IsIconic" (ByVal Hwnd As Long) As Long

Function SendNotesMail(strTo As String, strSubject As String, strBody As String, strFilename As String, ParamArray strFiles())
Dim doc As Object 'Lotus NOtes Document
Dim rtitem As Object '
Dim Body2 As Object
Dim ws As Object 'Lotus Notes Workspace
Dim oSess As Object 'Lotus Notes Session
Dim oDB As Object 'Lotus Notes Database
Dim X As Integer 'Counter
'use on error resume next so that the user never will get an error
'only the dialog "You have new mail" Lotus Notes can stop this macro
If fIsAppRunning = False Then
MsgBox "Lotus Notes is not running" & Chr$(10) & "Make sure Lotus Notes is running and you have logged on."
Exit Function
End If

On Error Resume Next

Set oSess = CreateObject("Notes.NotesSession")
'access the logged on users mailbox
Set oDB = oSess.GETDATABASE("", "")
Call oDB.OPENMAIL

'create a new document as add text
Set doc = oDB.CREATEDOCUMENT
Set rtitem = doc.CREATERICHTEXTITEM("Body")
doc.sendto = strTo
doc.Subject = strSubject
doc.Body = strBody & vbCrLf & vbCrLf

'attach files
If strFilename <> "" Then
Set Body2 = rtitem.EMBEDOBJECT(1454, "", strFilename)
If UBound(strFiles) > -1 Then
For X = 0 To UBound(strFiles)
Set Body2 = rtitem.EMBEDOBJECT(1454, "", strFiles(X))
Next X
End If
End If
doc.SEND False
End Function

Sub test()
Dim strTo As String 'The sendee(s) Needs to be fully qualified address. Other names seperated by commas
Dim strSubject As String 'The subject of the mail. Can be "" if no subject needed
Dim strBody As String 'The main body text of the message. Use "" if no text is to be included.
Dim FirstFile As String 'If you are embedding files then this is the first one. Use "" if no files are to be sent
Dim SecondFile As String 'Add as many extra files as is needed, seperated by commas.
Dim ThirdFile As String 'And so on.

strTo = "EMAIL@ADDRESS.COM"
strSubject = "15 Day Notice!"
strBody = "This is a notice to let you know that your contract will end in 15 days."
strBody = strBody & vbCrLf & "Just add new lines by concatenating vbCrLF"
FirstFile = "G:\Apps\Windows\4bpo\ExcelUtilities.exe"
SecondFile = "G:\Apps\Windows\4bpo\life.xls"
ThirdFile = "G:\Apps\Windows\ImpactXP\CompactDbs.vbs"

SendNotesMail strTo, strSubject, strBody, FirstFile, SecondFile, ThirdFile
End Sub

Private Function fIsAppRunning() As Boolean
'Looks to see if Lotus Notes is open
'Code adapted from code by Dev Ashish

Dim lngH As Long
Dim lngX As Long, lngTmp As Long
Const WM_USER = 1024
On Local Error GoTo fIsAppRunning_Err
fIsAppRunning = False

lngH = apiFindWindow("NOTES", vbNullString)

If lngH <> 0 Then
apiSendMessage lngH, WM_USER + 18, 0, 0
lngX = apiIsIconic(lngH)
If lngX <> 0 Then
lngTmp = apiShowWindow(lngH, 1)
End If
fIsAppRunning = True
End If
fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
 
Try using the DLookup function to assign your value from table to strTo.

Ignorance of certain subjects is a great part of wisdom
 
is dlookup a vbs function or just access vba function?
DLookUp is a method of the Access.Application object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks!
i tried the dlookup
varTo = DLookup("[PropMngrEmail]", "tblPropertyMngrInfo")

but it keeps sending the email to my email address (the account that it is using to send it out)???



Option Compare Database
Option Explicit

Private Declare Function apiFindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long

Private Declare Function apiSendMessage Lib "user32" Alias _
"SendMessageA" (ByVal Hwnd As Long, ByVal msg As Long, ByVal _
wParam As Long, lParam As Long) As Long

Private Declare Function apiSetForegroundWindow Lib "user32" Alias _
"SetForegroundWindow" (ByVal Hwnd As Long) As Long

Private Declare Function apiShowWindow Lib "user32" Alias _
"ShowWindow" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function apiIsIconic Lib "user32" Alias _
"IsIconic" (ByVal Hwnd As Long) As Long



Function SendNotesMail(varTo As Variant, strSubject As String, strBody As String, strFilename As String, ParamArray strFiles())
Dim doc As Object 'Lotus NOtes Document
Dim rtitem As Object '
Dim Body2 As Object
Dim ws As Object 'Lotus Notes Workspace
Dim oSess As Object 'Lotus Notes Session
Dim oDB As Object 'Lotus Notes Database
Dim X As Integer 'Counter
'use on error resume next so that the user never will get an error
'only the dialog "You have new mail" Lotus Notes can stop this macro
If fIsAppRunning = False Then
MsgBox "Lotus Notes is not running" & Chr$(10) & "Make sure Lotus Notes is running and you have logged on."
Exit Function
End If

On Error Resume Next

Set oSess = CreateObject("Notes.NotesSession")
'access the logged on users mailbox
Set oDB = oSess.GETDATABASE("", "")
Call oDB.OPENMAIL

'create a new document as add text
Set doc = oDB.CREATEDOCUMENT
Set rtitem = doc.CREATERICHTEXTITEM("Body")
doc.sendto = varTo
doc.Subject = strSubject
doc.Body = strBody & vbCrLf & vbCrLf

'attach files
If strFilename <> "" Then
Set Body2 = rtitem.EMBEDOBJECT(1454, "", strFilename)
If UBound(strFiles) > -1 Then
For X = 0 To UBound(strFiles)
Set Body2 = rtitem.EMBEDOBJECT(1454, "", strFiles(X))
Next X
End If
End If
doc.SEND False
End Function

Private Sub cmdSendEmail_Click()

Dim varTo As Variant 'The sendee(s) Needs to be fully qualified address. Other names seperated by commas
Dim strSubject As String 'The subject of the mail. Can be "" if no subject needed
Dim strBody As String 'The main body text of the message. Use "" if no text is to be included.
Dim FirstFile As String 'If you are embedding files then this is the first one. Use "" if no files are to be sent
Dim SecondFile As String 'Add as many extra files as is needed, seperated by commas.
Dim ThirdFile As String 'And so on.

varTo = DLookup("[PropMngrEmail]", "tblPropertyMngrInfo")
strSubject = "Is this still working???"
strBody = "This is a notice to let you know that you contract will end in 15 days."
strBody = strBody & vbCrLf & "Just add new lines by concatenating vbCrLF"
FirstFile = "G:d\Apps\Windows\4bpo\ExcelUtilities.exe"
SecondFile = "G:\Apps\Windows\4bpo\life.xls"
ThirdFile = "G:\Apps\Windows\ImpactXP\CompactDbs.vbs"

SendNotesMail varTo, strSubject, strBody, FirstFile, SecondFile, ThirdFile
End Sub

Private Function fIsAppRunning() As Boolean
'Looks to see if Lotus Notes is open
'Code adapted from code by Dev Ashish

Dim lngH As Long
Dim lngX As Long, lngTmp As Long
Const WM_USER = 1024
On Local Error GoTo fIsAppRunning_Err
fIsAppRunning = False

lngH = apiFindWindow("NOTES", vbNullString)

If lngH <> 0 Then
apiSendMessage lngH, WM_USER + 18, 0, 0
lngX = apiIsIconic(lngH)
If lngX <> 0 Then
lngTmp = apiShowWindow(lngH, 1)
End If
fIsAppRunning = True
End If
fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
 
If you want a specific email (like the person who is displayed in your subform) I'm pretty sure you need to add a third parameter in order to get the specific address for that record.

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases:
The Fundamentals of Relational Database Design
Understanding SQL Joi
 
Awesome! I got it to work. Thank you much for the help!

~Jay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top