Email and Outlook (1 Viewer)

Douglas Post

Registered User.
Local time
Today, 09:28
Joined
Feb 10, 2012
Messages
22
My goal is to send Email thru Outlook from MS Access utilizing certain fields in my form. I am testing the concept with the following code:

Code:
Private Sub Send_Info_Click()
   
  Dim Email As String
   
  '**create variables for Outlook
  Dim objOutlook As Object
  Dim objEmail As Object
   
  '**gathers information from your form.  this sets the string variable to your fields
  Email = Me!Email
   
  '***creates an instance of Outlook
  Set objOutlook = CreateObject("Outlook.Application")
  Set objEmail = objOutlook.CreateItem(olMailItem)
   
  '***creates and sends email
  With objEmail
      .To = Email
      .Body = "TESTING INSERTING TEXT IN BODY OF EMAIL"
      .Display 'SEND sends the email in Outlook.  Change to DISPLAY if you want to be able to modify or see what you have created before sending the email
  End With
   
  '**Closes Outlook
  '**objOutlook.Quit
  '**Set objEmail = Nothing
   
  Exit Sub
  '****end code****
  End Sub
The issue I am running into is that the Email field is saves as a Data Type "Hyperlink". I have done this because sometimes I just want to send an email easily without additional text. It causes a problem with the above code as my email looks like this in the (.To) field once it opens up the mail client which obviously does not work.

Code:
someemail@yahoo.com#mailto:someemail@yahoo.com#
Is there a way to truncate it to remove everything between the # marks?

Also my auto signature does not appear. Is there a switch for this? It works when I use FollowHyperlink.
 

Kiwiman

Registered User
Local time
Today, 14:28
Joined
Apr 27, 2008
Messages
799
Howzit

Sorry I don't know a lot about the outlook library (so I can't help with the signature part), but to extract the email address you can try something like

Code:
'Get everything after first #
Email = mid(Me!Email,InStr(1, Me!Email, "#")+1)
'Remove the last #
Email = left(Email,InStr(1, Email, "#")-1)
 

Douglas Post

Registered User.
Local time
Today, 09:28
Joined
Feb 10, 2012
Messages
22
Cheers Kiwiman!

The code works but I also had to remove the "mailto:" so I modified the code as follows:

Code:
'Get everything after first :
Email = mid(Me!Email,InStr(1, Me!Email, ":")+1)
'Remove the last #
Email = left(Email,InStr(1, Email, "#")-1)
Should I be saving emails as Hyperlinks or just plain text?
 

Kiwiman

Registered User
Local time
Today, 14:28
Joined
Apr 27, 2008
Messages
799
Howzit

Nice one. I think it is a personal preference and what you want to do with the email address. I alternate myself - but don't have any real reason why I do so. I just like trying new ways of doing the same thing.

This would also work based on your example.

Code:
Email = left(Me!Email,InStr(1, Me!Email, "#")-1)
 

Danick

Registered User.
Local time
Today, 09:28
Joined
Sep 23, 2008
Messages
351
Hello,
I found this old post in placing a button on a form to send a single email to the person in the form and it works great for when the email field has something in it. But I get a run-time error 94 when the field is empty. I've tried various code using If Not IsNull and If IsNull but can't get it working.

Anyone have a simple fix for this? Thanks
 

Douglas Post

Registered User.
Local time
Today, 09:28
Joined
Feb 10, 2012
Messages
22
I had the same error. I just placed my email in there and it works fine. I then delete the email prior to sending. I think the problem stems from the fact the code is expecting an email to be there.
 

Danick

Registered User.
Local time
Today, 09:28
Joined
Sep 23, 2008
Messages
351
Yes I too just add a fictitious email. Actually, I only put the letter "a" and that will work as well.

I was just hoping to find a better way with error checking. I have some co-workers who like my database and want to use it for themselves. So now I have to think about how to make it work for others so they don't get the run-time errors.

BTW -thanks for your code - works really well for me.
I've got another post in this forum for a similar issue but trying to populate a bunch of emails from a filtered form. Haven't got that working yet but I'm not giving up yet...
 

Danick

Registered User.
Local time
Today, 09:28
Joined
Sep 23, 2008
Messages
351
I figured it out!! This seems to be working for me now. Thought I'd post it for anyone else who was having this same issue.

Thanks again...

Code:
Private Sub cmdSendEmail_Click()

If Not IsNull(email) Then


   Dim strEmail As String
  '**create variables for Outlook
  Dim objOutlook As Object
  Dim objEmail As Object
   
  '**gathers information from your form.  this sets the string variable to your fields
  strEmail = Me!email
   
  '***creates an instance of Outlook
  Set objOutlook = CreateObject("Outlook.Application")
  Set objEmail = objOutlook.CreateItem(olMailItem)
   
  '***creates and sends email
  With objEmail
      .To = email
      '.Body = "TESTING INSERTING TEXT IN BODY OF EMAIL"
      .Display 'SEND sends the email in Outlook.  Change to DISPLAY if you want to be able to modify or see what you have created before sending the email
  End With
   
  Exit Sub
End If
MsgBox "No email address to send to"
Exit Sub

  '****end code****
End Sub
 

Douglas Post

Registered User.
Local time
Today, 09:28
Joined
Feb 10, 2012
Messages
22
I haven't looked at this in a long time but I think you need to have some error trap coding which basically says, if no email in CC field then run a code where the inclusion of CC in the code is eliminated.
 

Users who are viewing this thread

Top Bottom