VBA code failing in older versions

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I use the below code without issue on the machines at work to generate emails from vba and paste various bits from spreadsheets into them. All but two of the machines are using Excel/Outlook 2016 or later and the code runs no problem on them

2 of the machines were swapped out and replaced with ones running 2013 versions of both and the code now fails on the line " signature = newEmail.htmlBody"
Can anybody tell me why or point me in the direction of a solution

VBA Code:
Dim Outlook As Object, newEmail As Object
Dim signature As String

Set Outlook = CreateObject("Outlook.Application")
Set newEmail = Outlook.CreateItem(0)
Who = 'takes names from the spreadsheet


Set newEmail = Outlook.CreateItem(0)
                
            With newEmail
              
              .display
               signature = newEmail.htmlBody
              .To = Who
              
              .subject = ThisWorkbook.Name
              .ReadReceiptRequested = False
                        
               Application.CutCopyMode = False
              .Attachments.Add ThisWorkbook.FullName
    
            End With
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
What's the error? I suspect you're hitting the object model guard since htmlbody was one of the protected properties. Also not sure why you display the email before it's populated?
 
Upvote 0
RoryA thanks for your reply.

The error is shown as runtime 287 application-defined or object-defined error
I display before populating as I was advised that this was the only way to capture the individual signature of each user. Is that not correct?
 
Upvote 0
That does sound like the object model guard. Unfortunately, that wasn't that easy to work around - you need something like the redemption library, or vbMAPI.
 
Upvote 0
That does sound like the object model guard. Unfortunately, that wasn't that easy to work around - you need something like the redemption library, or vbMAPI.
so as a workaround if I remove the signature capture line would the rest of the code run or is the protection for the message body in its entirity.?
Is there a way to perhaps detect which version of excel/outlook is being used ?
 
Upvote 0
Not removed, but they were changed as I recall so that they would recognise installed AV software and reduce security accordingly. They could also be turned off via Group Policy I think.
 
Upvote 0
RoryA thanks for your suggestions so far. Could the problem be an early / late binding issue.? I dont understand the difference exactly so is my code early or late
 
Upvote 0
No, I don’t think so. Yours is late bound.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top