Share via

OBJECT MailItem

Anonymous
2018-07-16T08:21:13+00:00

Hi. For the method "CreateItem(olMailItem)"........ is there a way to send the e-mail directly without asking so that we can specify the source (e-mail) of the sender:

In this code .Display is used but I'm looking for something like .Send. Thanks.

Sub CreateHTMLMail() 'Creates a new e-mail item and modifies its properties Dim objMail As Outlook.MailItem 'Create e-mail item Set objMail = Application.CreateItem(olMailItem) With objMail 'Set body format to HTML .BodyFormat = olFormatHTML .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>" .Display <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<------------------------------------------------------------------HERE I WANTO TO SEND IT End With End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2018-07-19T13:52:07+00:00

    You can use

            .SentOnBehalfOfName = "emailaddress"

    somewhere below With objMail and above .Send

    Thanks Hans

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2018-07-19T13:27:08+00:00

    You can use

            .SentOnBehalfOfName = "emailaddress"

    somewhere below With objMail and above .Send

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-07-19T12:04:45+00:00

    You have to specify the recipient and preferably the subject:

    Sub CreateHTMLMail()

        'Creates a new e-mail item and modifies its properties

        Dim objMail As Outlook.MailItem

        'Create e-mail item

        Set objMail = Application.CreateItem(olMailItem)

        With objMail

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><BODY>>p><H2>The body of this message will appear in HTML.</H2></p>" & _

                "<p>Please enter the message text here.</p></BODY></HTML>"

            .To = "emailaddress"

            .Subject = "my subject"

            .Send

        End With

    End Sub

    Remark: the above code should work in Outlook. You posted this in the Access forum; the code won't work in Access without modification. For example:

    Sub CreateHTMLMail()

        'Creates a new e-mail item and modifies its properties

        Dim objOutlook As Outlook.Application

        Dim objMail As Outlook.MailItem

        On Error Resume Next

        Set objOutlook = GetObject(Class:="Outlook.Application")

        If objOutlook Is Nothing Then

            Set objOutlook = CreateObject(Class:="Outlook.Application")

            objOutlook.Session.Logon

        End If

        On Error GoTo 0

        'Create e-mail item

        Set objMail = Application.CreateItem(olMailItem)

        With objMail

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><BODY>>p><H2>The body of this message will appear in HTML.</H2></p>" & _

                "<p>Please enter the message text here.</p></BODY></HTML>"

            .To = "emailaddress"

            .Subject = "my subject"

            .Send

        End With

    End Sub

    Sending directly works best if Outlook is already running; otherwise you run the risk that the message will remain in the Outbox until the next time you start Outlook interactively.

    Hi. Thanks. But where do you specify the e-mail (source) ? the person who sends the e-mail.

    Thanks.

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2018-07-16T10:17:06+00:00

    You have to specify the recipient and preferably the subject:

    Sub CreateHTMLMail()

        'Creates a new e-mail item and modifies its properties

        Dim objMail As Outlook.MailItem

        'Create e-mail item

        Set objMail = Application.CreateItem(olMailItem)

        With objMail

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><BODY>>p><H2>The body of this message will appear in HTML.</H2></p>" & _

                "<p>Please enter the message text here.</p></BODY></HTML>"

            .To = "emailaddress"

            .Subject = "my subject"

            .Send

        End With

    End Sub

    Remark: the above code should work in Outlook. You posted this in the Access forum; the code won't work in Access without modification. For example:

    Sub CreateHTMLMail()

        'Creates a new e-mail item and modifies its properties

        Dim objOutlook As Outlook.Application

        Dim objMail As Outlook.MailItem

        On Error Resume Next

        Set objOutlook = GetObject(Class:="Outlook.Application")

        If objOutlook Is Nothing Then

            Set objOutlook = CreateObject(Class:="Outlook.Application")

            objOutlook.Session.Logon

        End If

        On Error GoTo 0

        'Create e-mail item

        Set objMail = Application.CreateItem(olMailItem)

        With objMail

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><BODY>>p><H2>The body of this message will appear in HTML.</H2></p>" & _

                "<p>Please enter the message text here.</p></BODY></HTML>"

            .To = "emailaddress"

            .Subject = "my subject"

            .Send

        End With

    End Sub

    Sending directly works best if Outlook is already running; otherwise you run the risk that the message will remain in the Outbox until the next time you start Outlook interactively.

    Was this answer helpful?

    0 comments No comments