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

Send E-Mail From VB 4

Status
Not open for further replies.

rtshort

IS-IT--Management
Feb 28, 2001
878
US
I've been Googleing this for about 3 hours now. The only thing I can find is how to send the actual e-mail from vb exclusively using CDONTS or the MAPI Control.

I'm really looking for a way to actually open Outlook Express with all of the information transfered from the VB Program to the e-mail and let the user select the address from their address book.

I know I can use the Shell to open Outlook Express, but how do I get the information into the e-mail.

Any clues or can it even be done?

Rob
Just my $.02.
 
Take a look here unless you have done so already, it might be what you are looking for.
faq222-179
 
Thanks for the reply.

I had looked at that once and that will probably be the way I'll go.

I'd much rather just have Outlook Express open from the VB App if possible. Kind of like "mailto:" on a Web Page.

Rob
Just my $.02.
 
try/modify this:

Private Sub cmdMAIL_Click()
On Error Resume Next
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim strDATAA, strDATAB, strDATAS As String
Screen.MousePointer = vbHourglass

Set objOutlook = CreateObject("OUTLOOK.APPLICATION")
Set objMail = objOutlook.CreateItem(olMailItem)

strDATAA = ...
strDATAS = ...
strDATAB = ...

With objMail
.Attachments.Add (strDATAA)
.Recipients.Add strSENDTO
.Body = Dir$(strDATAB)
.Subject = Dir$(strDATAS)
.Send
End With
Set objMail = Nothing
objOutlook.Quit
Set objOutlook = Nothing
Screen.MousePointer = vbDefault
MsgBox "File " & strDATAA & " has been sent to " & strSENDTO & "."
End Sub
 
You could use this
Shell """C:\Program Files\Outlook Express\msimn.exe""" & "/mailto:email@email.com?subject=Whatever&cc=another@email.com&bcc=yetanother&email.com&body=What text you want", vbNormalFocus
 
The UserPC will only have Outlook Express installed on it. Will that code work with it?

Also, I tried the code in the above FAQ and get a message telling me I don't have a default e-mail client installed to configure Outlook. (not outlook express)

I'm also running Vista (yea I know) so that may be why I'm getting that error.

Thanks.

Rob
Just my $.02.
 
Thanks Disferente, I'll give that a try. I didn't see your message until after I replied to the others.

Rob
Just my $.02.
 
Using MAPI has the advantage that it will 'automate' the host computer's current email client application if the latter is MAPI compliant; that means Outlook or Outlook Express and probably Windows Mail.

Try;
Public Function DoMAPIemail(Subject$, MessageText$, AttPaths$(), Optional Recipient$ = "") As Boolean

'Requires a Project reference into MSMAPI32.OCX

Dim i%
Dim OrigDir$, Msg$

Dim MAPISess As MAPISession, MAPIMess As MAPIMessages
Set MAPISess = New MSMAPI.MAPISession
Set MAPIMess = New MSMAPI.MAPIMessages

OrigDir$ = CurDir$
With MAPISess
' XP may need to have a profile set so...
'.LogonUI = False causes problem under XP
.LogonUI = True
.DownLoadMail = False
On Error Resume Next
.SignOn 'changes CurDir$ to MAPI
If Err Then
MsgBox "Error " & Err & " " & Err.Description & " during MAPI Session SignOn"
GoTo exitMapiEmail
End If
On Error GoTo 0
End With

With MAPIMess
.SessionID = MAPISess.SessionID
.Compose
If Len(Recipient$) Then
.RecipAddress = Recipient$
.RecipDisplayName = Recipient$
End If
.MsgSubject = Subject$
.MsgNoteText = MessageText$
For i = 1 To UBound(AttPaths$)
If Len(AttPaths$(i)) Then
.AttachmentIndex = i - 1
.AttachmentPathName = AttPaths$(i)
.AttachmentPosition = i + 1
End If
Next
'by now the mapi controls have set curdir to that of MAPI
ChDrive OrigDir$
ChDir OrigDir$
On Error Resume Next
.Send True 'Using .Send with the True arg displays the default MAPI email client and lets user review edit the email before manually sending it
' CurDir$ has been changed to MAPI again
'Error 32001 returned if user cancels send
If Err.Number > 0 And Err.Number <> 32001 Then
Msg$ = "Error " & Err.Number & " " & Err.Description & ", sending email" & vbCrLf
If Err = 32003 Then
Msg$ = Msg$ & vbCrLf & "Please start your email program (e.g. Outlook) manually and try again"
End If
MsgBox Msg$, vbCritical
GoTo exitMapiEmail
End If
On Error GoTo 0
End With

DoEvents

MAPISess.SignOff
DoMAPIemail = True

exitMapiEmail:
Set MAPIMess = Nothing
Set MAPISess = Nothing
On Error GoTo 0
ChDrive OrigDir$
ChDir OrigDir$

End Function

 
Kinf of like mailto:? Why not be exactly like mailto:? This example requires a form with acommand button:
Code:
[blue]Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
   Dim ret As Long
   Dim mailInfo(0 To 4) As String
   Dim msgString As String
   'mailInfo(0) = "anexample@somewhere.com"
   'mailInfo(1) = "cc@somehere.com"
   'mailInfo(2) = "bcc@somewhere.com"
   mailInfo(3) = "This is the subject"
   mailInfo(4) = "Here is some message body"
   
   ' open new message' in OE
   msgString = msgString & "mailto:" & mailInfo(0)
   msgString = msgString & "?cc=" & mailInfo(1)
   msgString = msgString & "&bcc=" & mailInfo(2)
   msgString = msgString & "&subject=" & mailInfo(3)
   msgString = msgString & "&body=" & mailInfo(4)
'   msgString = msgString + "&Attach=c:\autoexec.bat" ' Only works for some implementations
   ret = ShellExecute(Me.hwnd, vbNullString, msgString, vbNullString, "c:\", 1)
End Sub[/blue]
 
Thanks to all of you for your help.

strongm, that hit the nail on the head. Exactly what I was looking for.

I said "kind of like mailto:" because I wasn't sure after searching the web for half of the day if it was possible in VB and because I was talking about a web page.

Thanks a Million.

Rob
Just my $.02.
 
Well, I see that this thread has been found useful and archived. :)

As Lt. Columbo would say, Just one more thing.

I'm using this code:
Code:
    mailInfo(4) = "Name:" & vbTab & vbTab & txtInsFName.Text & txtInsLName.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "Address:" & vbTab & vbTab & txtInsAddress.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "City:" & vbTab & vbTab & txtInsCity.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "State:" & vbTab & vbTab & txtInsState.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "Zip:" & vbTab & vbTab & txtInsZip.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "Home Phone:" & vbTab & txtInsHome.Text & vbCrLf
    mailInfo(4) = mailInfo(4) & "Cell Phone:" & vbTab & txtInsCell.Text & vbCrLf
    MsgBox (mailInfo(4))

When the message box comes up everything is in the proper format. eg:

Name: FirstName LastName
Address: Address
City: City
State: State
Zip: Zip
etc.

When it comes up in the actual e-mail message, it's all one the same line with no spaces.

How can I keep the format like the MsgBox?

Rob
Just my $.02.
 
As for tabs, you will have to space it out by yourself.
You could use something like string(15-len("Name:")," ")
 
Yep, should have given you the following info as well: you need to use ASCII hexadecimal equivalents as variables for punctuation characters:
Punctuation Hex
----------------------------
Space ( ) %20
Comma (,) %2C
Question mark (?) %3F
Full Stop (.) %2E
Exclamation mark (!) %21
Colon :)) %3A
Semicolon (;) %3B
Line feed %0A
Line break (ENTER key) %0D
 
Thanks Disferente. The "%0D%0A" worked GREAT.

I'm still trying to figure out the tab thing.

Thanks again.

Rob
Just my $.02.
 
Thanks strongm. I didn't see your message until I posted the comment to Disferente.

Do you know what the "vbTab" equivalent would be. I'm looking up the chart now to see if I can find it.

Rob
Just my $.02.
 
Theoretically %09, but I don't believe it has any effect I'm afraid
 
Thanks strongm. I found that on an ASCII chart on the Internet doing a search. I tried it with quotes and without quotes, but no go on either.

I might just have to do the app with no tabs in the e-mail making it look like:

Name:FirstName LastName
Address:123 Main Street
etc.

That will have to do for now, but I'll continue to work on it. I'll post back if I figure it out.



Rob
Just my $.02.
 
Well, just to let everyone know, I never got it to space out like I wanted. It's fine with my employer though so that works for me.

Thanks again guys.

Rob
Just my $.02.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top