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

Big trouble automating sending mail from Word VBA

Status
Not open for further replies.

Dor100

Technical User
Apr 9, 2001
279
US
I've searched past threads and the web, and haven't been able to find a VBA sub to enable me to automate sending mail with an attachment from Word. I'm using Word 97 and Notes 5.0.10. Here's the websites with code that I've tried:





---couldn't get to this article:


I've got the Lotes Notes and Domino references set, but no success, only errors; invalid use of keyword New, can't create object, object or property unavailable, can't locate names.nsf/file does not exist (for when Lotus actually tries to open), etc., etc. One of the subs has "Dim oMemo As NotesMailMemo," but I can't find NotesMailMemo anywhere at all (including Object Browser).

I've pasted all these routines into my Word 97 VB Editor, so now there's quite a lot of code and no results. Does anybody have a simple VBA soloution that will enable me to have people click a button in a Word doc that they fill out, and automate sending it back through Lotus Notes? Thanks in advance to all who try to answer this, and especially the one(s) who have it.
 
[Ponder] Here is some code that I've used successfully in Microsoft Access. I think that it may be helpful to you. I put it in the On Click event of a button. I've used it with Lotus 5 and Lotus 6. Depending on your Lotus and Network security conditions, it may or may not be nescessary to have Lotus Notes running when the code is executed. For me, Lotus would launch if it was not already running until we made some security enhancements. Now, my users must have Lotus Notes running for the code to work.

Private Sub YourButton_Click()
On Error GoTo Err_YourButton_Click

'Create Variables
Dim notesdb As Object 'Need these to call Notes
Dim notesdoc As Object '
Dim notesrtf As Object '
Dim notessession As Object '
Dim ToName As String 'Need these to build the email
Dim CCName As String '
Dim BCCName As String '
Dim Subject As String '
Dim MsgText As Variant '
Dim Body As String '
Dim BodyText As Variant '
Dim AgencyCode As String 'need these for my app
Dim AgencyName As String '
Dim RequestID As String '
Dim Msg As Variant '
Dim ToDone As String 'need these to check fields
Dim SubjDone As String '

If IsNull(MyComboToField.Value) Then ToDone = "N" Else ToDone = "Y" 'Is there a name in the To field

'If the To field is missing send the user back to it
CheckTo:
If ToDone = "N" Then
GoTo DontDoIt1
Else: GoTo CheckSubj
End If

'If the subject is missing send the user back to it
CheckSubj:
If IsNull(MyComboSubjectField.Value) Then SubjDone = "N" Else SubjDone = "Y" 'Is there a Subject in the subject field
If SubjDone = "N" Then
GoTo DontDoIt2
Else: GoTo DoIt
End If

DontDoIt1:
MsgBox ("You can't send this message without completing the 'To' field and the 'Subject' field. Please complete them to continue.")
GoTo CancelIt1

DontDoIt2:
MsgBox ("You can't send this message without completeing the 'To' field and the 'Subject' field. Please complete them to continue.")
GoTo CancelIt2

DoIt:
'Set Recipient Variables
ToName = Me![To]
If IsNull(CC.Value) Then CCName = "" Else CCName = Me![CC]
If IsNull(BCC.Value) Then BCCName = "" Else BCCName = Me![BCC]

'Set subject and Body Variables
Subject = Me![Subject]
AgencyName = "Agency: " & Me![AgencyCode] & " - " & Me![AgencyName]
RequestID = "Exemption Request ID: " & Me![ExemptionRequestID]
Body = "Please go to the Exemption Request Database and Review the record identified below:"
Msg = " " & Me![MsgText]

'Build the message body using variables and formatting codes
BodyText = Body & Chr$(10) & Chr$(13)
BodyText = BodyText & Chr$(10) & Chr$(13)
BodyText = BodyText & AgencyName & Chr$(10) & Chr$(13)
BodyText = BodyText & RequestID & Chr$(10) & Chr$(13) & Chr$(10) & Chr$(13)
BodyText = BodyText & Msg & Chr$(10) & Chr$(13) & Chr$(10) & Chr$(13)

'Setup Notes session
Set notessession = CreateObject("Notes.Notessession")
Set notesdb = notessession.getdatabase("", "")
Call notesdb.openmail
'make new mail message
Set notesdoc = notesdb.createdocument
Call notesdoc.replaceitemvalue("Sendto", ToName)
Call notesdoc.replaceitemvalue("CopyTo", CCName)
Call notesdoc.replaceitemvalue("BlindCopyTo", BCCName)
Call notesdoc.replaceitemvalue("Subject", Subject)
Call notesdoc.replaceitemvalue("Body", BodyText)
'send message
Call notesdoc.Send(False)
Call notesdoc.Save(True, False)
Set notessession = Nothing
GoTo Sent

'Tell user the message went
Sent:
MsgBox "Email sent"
DoCmd.Close
GoTo Exit_YourButton_Click

'Send user back to To field
CancelIt1:
DoCmd.CancelEvent
DoCmd.GoToControl "MyComboToField"
GoTo Exit_YourButton_Click

'Send user back to Subject field
CancelIt2:
DoCmd.CancelEvent
DoCmd.GoToControl "MyComboSubjectField"

Exit_YourButton_Click:
Exit Sub

Err_YourButton_Click:
MsgBox Err.Description
Resume Exit_YourButton_Click

End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top