Avatar of Glenn Stearns
Glenn Stearns
Flag for United States of America asked on

Outlook 2007 Error in Excel VBA Script

I am using this code, which works fine, to email a worksheet from within an Excel VBA script...
-------------------------------------------------------------------------------------
Dim outlookApp As Object
Dim outlookMail As Object
Dim wbTemp As Workbook
Dim strFileName As String

Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)

ActiveSheet.Copy

Set wbTemp = ActiveWorkbook

wbTemp.SaveAs "C:\Temp\" & wbTemp.Sheets(1).Name & ".xlsx", xlOpenXMLWorkbook

strFileName = wbTemp.FullName

wbTemp.Close False

With outlookMail
    .To = "test@yourco.com"
    .Subject = "Requested Spreadsheet"
    .BodyFormat = 2
    .HTMLBody = "Good morning!,<p>Attached is the spreadsheet you requested."
    .Attachments.Add strFileName
    .Importance = olImportanceHigh
    .Send
End With

Set outlookMail = Nothing
Set outlookApp = Nothing

Kill strFileName
-----------------------------------------------------------------------------------------------
As noted, the script works fine and runs to completion, but does generate an Outlook 2007 error message as shown in the attached file. I suspect this has to do with the version of Outlook being 2007, as I do not get the error when test-running the code on a machine with Outlook 2013. If any of you know how to adapt this code so as to eliminate the error, please post!

Thanks!

Glenn
Outlook-Error.JPG
OutlookVBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Glenn Stearns

8/22/2022 - Mon
Subodh Tiwari (Neeraj)

What if you replace the line .Importance = olImportanceHigh with .Importance = 2.
Does it work then?
Glenn Stearns

ASKER
Subodh...

No - same error occurs with your recommended change.

Thanks!

Glenn
Subodh Tiwari (Neeraj)

Sorry! No idea.
See if this link gives you an idea.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Norie

Which version of Excel are you using and which version of Excel was used to create the workbook you want to attach a sheet from?
Glenn Stearns

ASKER
Norie...
The version of Office on the machine running the code is Office 2007. Spreadsheet and VBA are in that version of Excel. The VBA script only runs in Excel 2007. It is extensive and has several commands in it which are not compatible with Excel 2013. I have tested the email part of the script in a similar situation on another machine running Office 2013 and I do not get the error. From what I'm reading online, this is likely an Outlook 2007 error and not an Excel VBA problem - something having to do with conflicting add-ins to Outlook 2007, which is what I am checking now. I believe this because I get the error when starting Outlook 2007 alone from the start menu.