Outlook 2007 & E-mails (1 Viewer)

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
Ahh ok.
I guess I will just have to educate the users about this then.

Thanks.
 

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
Ghudson: just found this. I know it states Winnt and 2000 but.. well ... I dunno.

Quote from: http://www.granite.ab.ca/access/email/outlook.htm

If you are using Windows NT or Windows 2000 as an OS then you can use CDONTS. It uses the SMTP Service to send mail directly. However the SMTP service must be running on the machine and the machine must have internet access. You add a reference to Microsoft CDO for NTS 1.2 Library in Access.:
Code:
Dim oEMail As New CDONTS.EMail

oEMail.From  "anyname@anydomain.com"
oEMail.To "youremail@yourdomain.com"
oEMail.BodyFormat  = CdoBodyFormatText
oEMail.Body = "Insert some useful text here"
oEMail.Importance  = CdoHigh
oEMail.AttachFile "C:\filename.txt"
oEMail.Send
Seems that you used to be able to use a "From" attribute.


Also found this : http://www.w3schools.com/asp/asp_send_email.asp
 
Last edited:

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
How do you introduce a "from" field

I do not believe that Outlook will allow you to automate a FROM since Outlook tries to prevent spoofing. If I am wrong, I would love to see the code on how to do it. ;-)

I dont think you are wrong. But if you have more than one inbox set up then I think you need to create your EmailItem from the right inbox then the FROM will change.

Thus the key to doing this is you need to get someone elses folder. Obviously if that "someone else" does not let you send emails from their email address then the outFolder will not return an object.
Code:
Set outRecipient = outNameSpace.CreateRecipient("emailaddress")
Set outFolder = outNameSpace.GetSharedDefaultFolder(outRecipient, olFolderInbox)
    Set outItem = outFolder.Items.Add("IPM.NOTE")

I have only tried with with the calendar but the principle should be the same for emails.
 

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
Darbid,

I am trying to get this working and in it's current form I get the error that an object is missing when I get to the line: outItem = outFolder.Items.Add("IPM.NOTE")

any ideas.

Code:
Public Function SetupOutlookEmail(ByVal sTo As String, ByVal sSubject As String, ByVal sBody As String, ParamArray sAttachmentList() As Variant) As Boolean
On Error GoTo Err_SetupOutlookEmail
    
    Dim objOLApp As Object
    Dim outItem As Object
    Dim outFolder As Object
    Dim DestFolder As Object
    Dim outNameSpace As Object
    Dim lngAttachment As Long

    Set objOLApp = CreateObject("Outlook.Application")
    Set outNameSpace = objOLApp.GetNamespace("MAPI")
    Set outFolder = outNameSpace.GetDefaultFolder(6)
    Set outItem = objOLApp.CreateItem(0)
    
    Set outRecipient = outNameSpace.CreateRecipient("[COLOR=Blue][I]myemail[/I][/COLOR]")
    Set outFolder = outNameSpace.GetSharedDefaultFolder(outRecipient, olFolderInbox)
    Set outItem = outFolder.Items.Add("IPM.NOTE")

    outItem.To = sTo
    outItem.Subject = sSubject
    outItem.HTMLBody = sBody
    outItem.ReadReceiptRequested = False
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
I'd be guessing that a previous object is failing.

Either recipiet or folder is not being set.

I am about to do something like this so I will then know more about it myself.

Whose email are you using? You must be allowed to use it otherwise the folder object will not be set and thus you would get your error message.

I think you can try the code with your own email address and it should work.

Maybe tomorrow (i am in europe) I will have a look at it closer as I recently got a second email address that I can check to see if this works.
 

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
Thanks darbid.

I will be utilising this on my machine at work which uses MS exchange software. I have multiple inboxes and some inboxes are "multiuser". When I place my own email in, it shows the "outRecipient" correctly but fails at the line I mentioned.

Have a play around, I am in no rush so I'll check back later.
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
Ok this is not as simple as I thought.

My code should not error if you are allowed to have access to the the email address you are trying.

BUT - it still sends from the default email. I now have to fix this so I will get back to you when I work it out.
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
Ok I was wrong you cannot do it with the Outlook object model. Seems a pretty simple thing to me for M$ to implement but I sometimes feel they are getting more of an expert about making excuse than doing some real work.

In case the below link fails;
You can't do that using the Outlook object model. You would have to use
Extended MAPI (C++ or Delphi code only), CDO 1.21 (optional installation
with Outlook 2000 and later and subject to the security prompts) or
Redemption (3rd party library at www.dimastr.com/redemption) to do that.

See the code example for sending with a different From address at
www.cdolive.com/cdo5.htm for a CDO 1.21 way of doing it.



http://www.pcreview.co.uk/forums/showpost.php?p=5448309&postcount=3
 

ghudson

Registered User.
Local time
Yesterday, 23:20
Joined
Jun 8, 2002
Messages
6,195
Ok I was wrong you cannot do it with the Outlook object model. Seems a pretty simple thing to me for M$ to implement but I sometimes feel they are getting more of an expert about making excuse than doing some real work.

In case the below link fails;



http://www.pcreview.co.uk/forums/showpost.php?p=5448309&postcount=3

Which is why i previously stated in this thread...

I do not believe that Outlook will allow you to automate a FROM since Outlook tries to prevent spoofing. If I am wrong, I would love to see the code on how to do it. ;-)
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
Which is why i previously stated in this thread...
It did hurt me to write "I was wrong" but I was thinking of you and my previous comments.

You can do it. Just not with VBA or VB or C# you have to go to big school.
 

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
To get around this problem I have resorted to letting all users know about the ability to choose which profile to open Outlook with. Then just before the event I have added code to shutdown Outlook and then create a new instance of it. This then shows the "choose profile" dialog.

Not everyone will use it either through choice or ignorance but at least some people will.
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
I have to do something here so I will post my solution later...could take a while.

I am thinking of navigating the menu to show the FROM and then filling it if i can. In my case the user has to hit the send...another MS annoyance I cannot get around in my situation.
 

constantG

Registered User.
Local time
Today, 04:20
Joined
Jun 24, 2009
Messages
92
Having the email visible prior to hitting send is not a problem, in fact I have it set that way at the moment for testing purposes and I am of the opinion that, if I were the user it would give me a warm feeling that I was acheiving something if I could actually see the message.

This is clearly something that needs to be discussed by myself and the eventual users.

Thinking about my last post, it is probably not a good idea for it to automatically shut down Outlook prior to using the function.
 

darbid

Registered User.
Local time
Today, 05:20
Joined
Jun 26, 2008
Messages
1,428
OK so here is a summary of what you can do:

2003: Outlook provides no direct way to set the From value for all messages. If you are not using WordMail as the editor, however, you can set the sending account using CommandBars techniques.

SOURCE:http://www.outlookcode.com/codedetail.aspx?id=889

2007: MS was listening SendUsingAccount property Returns or sets an Account object that represents the account under which the MailItem is to be sent. Read/write.

HELP:http://msdn.microsoft.com/en-us/library/bb207787(office.12).aspx

2010: Here is some untested code which seem to do it in 2010

Code:
Public Sub CreateEmail(strFrom As String, strTo As String, strCC As String,
strBCC As String, strSubject As String, strTextOrHTML As String, strBodyText
As String, strBodyHTML As String)
On Error GoTo Err_CreateEmail
Dim OlApp As Outlook.Application
Dim olAccounts As Outlook.Accounts
Dim olAccount As Outlook.Account
Dim olAccountTemp As Outlook.Account
Dim olMail As MailItem
Dim FoundAccount As Boolean

Set OlApp = New Outlook.Application
FoundAccount = False

'loop through and find Outlook account based on from email address
Set olAccounts = OlApp.Application.Session.Accounts
For Each olAccountTemp In olAccounts
If (olAccountTemp.smtpAddress = strFrom) Then
Set olAccount = olAccountTemp
FoundAccount = True
Exit For
End If
Next


If (FoundAccount) Then
Set olMail = OlApp.CreateItem(olMailItem)
With olMail
.SendUsingAccount = olAccount
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = strSubject
If (strTextOrHTML = "HTML") Then
.BodyFormat = olFormatHTML
.Body = strBodyText
.HTMLBody = strBodyHTML
Else
.BodyFormat = olFormatPlain
.Body = strBodyText
End If
'           .Attachments.Add filename
.Display
End With
Else
MsgBox "Could not find the appropriate Outlook account for email
address: " & strFrom & ".  Contact RPT Software for assistance if needed.",
vbOKOnly, "Outlook error retrieving SMTP account"
End If

Exit_CreateEmail:
Set olMail = Nothing
Set olAccount = Nothing
Set OlApp = Nothing
Exit Sub

Err_CreateEmail:
MsgBox Err.Description
Resume Exit_CreateEmail
End Sub
SOURCE:http://www.eggheadcafe.com/software/aspnet/35990632/vba-to-send-email-via-out.aspx

I am stuck with 2003 but I have heard that we are going to 2007 soon.

PS: From reading you posts again I think that you only need to add the .SentonBehalfOf. I have worked out that is all I need to do.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:20
Joined
Feb 28, 2001
Messages
27,317
See this thread regarding driving Outlook from Access VBA, which avoids the Inspector problem.

http://www.access-programmers.co.uk/forums/showthread.php?t=195360&highlight=Outlook

(You could probably remove the &highlight section, this reference came from a search.)

The code works more or less correctly in Access 2003 and 2007, but with the following oddities:

Under Office 2003, the code works, but our security settings force it to ask you about the "sending mail via automation" action. You click the YES box and it does what it is supposed to do.

Under Office 2007, the code works and correctly encrypts and digitally signs the mail. It even sends it. BUT... even though the mail goes out, the .Send takes an error trap 287, which is some sort of Outlook error. If you search the web for error 287, you find that the error is widespread. I'll say this - with the debugger, I have verified that the objects are actually instantiated. The web articles say that the problem is something is not instantiated. Which pretty much means the error is a pile of poop. So my Ac2007 version of that trap handler tests for that specific case and ignores it (does a Resume Next).
 

Users who are viewing this thread

Top Bottom