VBA Code to email (outlook) - Fine in 2016(32bit) / Broken in 365(64bit) (1 Viewer)

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39
I found this one too which might be specific to your situation...


No matter which way you go, please report back here what solution you went with!
Another example that looks amazing but uses the password part. I doubt they will ever allow that but i have no idea of the password for a shared mailbox on 365. if there is one - its not something i see - everything just appears to use my microsoft account (sorry not very technical)
 

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39

NauticalGent

Ignore List Poster Boy
Local time
Today, 09:40
Joined
Apr 27, 2015
Messages
6,280
i have asked some of my IT dept buddies if they can assist.
Good deal...have you tried DBG's idea regarding the late-binding? I didnt even think about it but I would not be surprised id that turns out to be the culprit.

Even if it doesn't fix this issue, it will pay dividends in the long run because the references you select will have no bearing going forward.
 

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39
Hi @NauticalGent. That's exactly right!

Here's some more examples:
Code:
Dim app As Object 'Outlook.Application
Dim msg As Object 'Outlook.MailItem
Dim recipTO As Object 'Outlook.Recipient
Dim recipCC As Object 'Outlook.Recipient
Dim Att As Object 'Outlook.Attachments

olMailItem = 0

Set app = CreateObject("outlook.application")
Set msg = app.CreateItem(olMailItem)
Hope that helps...
Getting:
Compile Error
Invalid or unqualified reference
 

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39
Good deal...have you tried DBG's idea regarding the late-binding? I didnt even think about it but I would not be surprised id that turns out to be the culprit.

Even if it doesn't fix this issue, it will pay dividends in the long run because the references you select will have no bearing going forward.
just did - reckon i have implemented it wrongly....

Code:
Public Sub SendMailboxHTMLMessage(DisplayMsg As Boolean, WhoTo As String, WhoCC As String, Mailbox As String, TheSubject As String, TheBody, Optional AttachmentPath)
Dim objOutlook As Object 'Outlook.Application
Dim objOutlookMsg As Object ' Outlook.MailItem
Dim objOutlookRecip As Object 'Outlook.Recipient
Dim objOutlookAttach As Object 'Outlook.Attachment


        Const olMailItem = 0
          ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")


          ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)


        With objOutlookMsg
              ' Add the To recipient(s) to the message.
              .To = WhoTo
             
              If WhoCC = "" Then GoTo NoCC
              ' Add the CC recipient(s) to the message.
              .CC = WhoCC
             
NoCC:
             
             ' Set the Subject, Body, and Importance of the message.
             .Subject = TheSubject
             .HTMLBody = TheBody & vbCrLf & vbCrLf
             .Importance = olImportanceHigh  'High importance


             ' Add attachments to the message.
             If Not IsMissing(AttachmentPath) Then
                 Set objOutlookAttach = .Attachments.Add(AttachmentPath)
             End If


            


             ' Should we display the message before sending?
             If DisplayMsg Then
                 .Display
             Else
                .Save
                .Send
             End If
          End With
          Set objOutlook = Nothing
         
End Sub

this is a function that i would call a lot where i have been implementing the ideas
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 09:40
Joined
Apr 27, 2015
Messages
6,280
What kind of error are you getting?

Edit:
Getting:
Compile Error
Invalid or unqualified reference
Apologies, completely missed that!

Also, try adding Const olMailItem = 0 to you declare area. I have seen this fix some issues in the past.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 06:40
Joined
Oct 29, 2018
Messages
21,357
Getting:
Compile Error
Invalid or unqualified reference
Sorry, I made a mistake. You can't declare a constant inside a function. I edited my post above. Or, as @NauticalGent said, move the Const line outside of the procedure.

You can also just replace olMailItem with 0, as in:

.CreateItem(0)

Cheers!
 

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39
ok - still getting the crash out at the .send stage.

here is the current code

Const olMailItem = 0


Code:
Public Sub SendMailboxHTMLMessage(DisplayMsg As Boolean, WhoTo As String, WhoCC As String, Mailbox As String, TheSubject As String, TheBody, Optional AttachmentPath)
Dim objOutlook As Object 'Outlook.Application
Dim objOutlookMsg As Object ' Outlook.MailItem
Dim objOutlookRecip As Object 'Outlook.Recipient
Dim objOutlookAttach As Object 'Outlook.Attachment
'Const olMailItem = 0
        
          ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")


          ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(0)


        With objOutlookMsg
              ' Add the To recipient(s) to the message.
              .To = WhoTo
              
              If WhoCC = "" Then GoTo NoCC
              ' Add the CC recipient(s) to the message.
              .CC = WhoCC
              
NoCC:
              
             ' Set the Subject, Body, and Importance of the message.
             .Subject = TheSubject
             .HTMLBody = TheBody & vbCrLf & vbCrLf
             .Importance = olImportanceHigh  'High importance
             '.SentOnBehalfOfName = ""
             ' Add attachments to the message.
             If Not IsMissing(AttachmentPath) Then
                 Set objOutlookAttach = .Attachments.Add(AttachmentPath)
             End If


             ' Resolve each Recipient's name.
             'For Each objOutlookRecip In .Recipients
             '    objOutlookRecip.Resolve
             'Next


             ' Should we display the message before sending?
             If DisplayMsg Then
                 .Display
             Else
                .Save
                .Send
             End If
          End With
          Set objOutlook = Nothing
          
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:40
Joined
Oct 29, 2018
Messages
21,357
Did you remove the reference to the Outlook library?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:40
Joined
Oct 29, 2018
Messages
21,357
fail:
it struggled at this
.Importance = olImportanceHigh 'High importance
so i disabled
but still failed at the .send
When using late binding, any constant needs to be replaced with its integer value. I guessed I missed that one.

I actually helped someone just this weekend having the same issue of moving from using Outlook 2010 to Outlook 365. All I did was removed the reference to Outlook 14.0 library and replaced it with a reference to Outlook 16.0 library. That's basically the first option I posted earlier.

Now, just to check if Outlook is working, you could try using SendObject. If that works, you could try the steps I mentioned above, or we could create a small function to try to send out a simple email, just for testing. Something like:
Code:
Public Function TestOutlook() As Byte
Dim olApp As Object
Dim olMail As Object

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
With olMail
    .To = "testaddress@domain.com
    .Subject = "TEST"
    .Body = "This is a test..."
    .Send
End olMail

Set olMail = Nothing
Set olApp = Nothing

End Function
 

Dave1975

Member
Local time
Today, 13:40
Joined
Oct 21, 2015
Messages
39
When using late binding, any constant needs to be replaced with its integer value. I guessed I missed that one.

I actually helped someone just this weekend having the same issue of moving from using Outlook 2010 to Outlook 365. All I did was removed the reference to Outlook 14.0 library and replaced it with a reference to Outlook 16.0 library. That's basically the first option I posted earlier.

Now, just to check if Outlook is working, you could try using SendObject. If that works, you could try the steps I mentioned above, or we could create a small function to try to send out a simple email, just for testing. Something like:
Code:
Public Function TestOutlook() As Byte
Dim olApp As Object
Dim olMail As Object

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
With olMail
    .To = "testaddress@domain.com
    .Subject = "TEST"
    .Body = "This is a test..."
    .Send
End olMail

Set olMail = Nothing
Set olApp = Nothing

End Function
copied code to new module. changed the end olmail to end with as was giving me syntax error
fails at same point - .send

really starting to wonder if our security settings in 365 are preventing .send
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:40
Joined
Oct 29, 2018
Messages
21,357
copied code to new module. changed the end olmail to end with as was giving me syntax error
fails at same point - .send

really starting to wonder if our security settings in 365 are preventing .send
If you change .Send to .Display, are you able to manually send?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:40
Joined
Oct 29, 2018
Messages
21,357
copied code to new module. changed the end olmail to end with as was giving me syntax error
fails at same point - .send

really starting to wonder if our security settings in 365 are preventing .send
Did SendObject work?

Sent from phone...
 

Users who are viewing this thread

Top Bottom