Hi
I've previously used code without issue, but in a different employer and in Office 2016, now Office 365. I'm getting a runtime 91 error "Object variable or with block variable not set". The code is set out below, I assume its causing an error at the line "Set OutApp = CreateObject("Outlook.Application")" and that the Office 365 is now not recognising that or something relating to the different IT environment?
I've previously used code without issue, but in a different employer and in Office 2016, now Office 365. I'm getting a runtime 91 error "Object variable or with block variable not set". The code is set out below, I assume its causing an error at the line "Set OutApp = CreateObject("Outlook.Application")" and that the Office 365 is now not recognising that or something relating to the different IT environment?
VBA Code:
Sub Email_Open_WB_as_Attachment()
Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wbl = ActiveWorkbook
'Make copy of file, open oit,mail it, delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wbl.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wbl.Name, Len(wb1.Name) - InStrRev(wbl.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "DPO"
.CC = ""
.BCC = ""
.Subject = "Internal Inventory/Retention Exercise"
.Body = "Completed form attached"
.Attachments.Add TempFilePath & TempFileName & FileExtStr
.SEND
End With
On Error GoTo 0
'delete file
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub