Hi Can anyone help with the following code which is triggered by a command button to save the excel file and create an email in outlook
The saving of the excel works fine
When the code then tries to create the email I get the following message
Object Reference not set to an instance of the object
I click the okay button below the message and the email is created
It's probably something obvious but I'm struggling to see what I am doing wrong
Many Thanks
Here is the code
Sub Close_CR()
Dim OutApp As Object
Dim OutMail As Object
Dim exptrknumber As String
Dim claimantemail As String
Dim ccemail As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
StartDate = [C11]
StartTime = [E11]
EndDate = [G11]
EndTime = [I11]
ActualStartDate = [C41]
ActualStartTime = [E41]
ActualEndDate = [G41]
ActualEndTime = [I41]
ActualStartDate = Format(ActualStartDate, "Long Date")
ActualEndDate = Format(ActualEndDate, "Long Date")
ActualStartTime = Format(ActualStartTime, "hh:mm")
ActualEndTime = Format(ActualEndTime, "hh:mm")
ActualStartDayName = Format(ActualStartDate, "dddd")
ActualEndDayName = Format(ActualEndDate, "dddd")
Title = [I13]
CompletionStatus_State = [C43]
ClosureNotes = [C45]
Application.ScreenUpdating = False ' Prevents screen refreshing.
Name = ActualStartDate & " - CR COMPLETION - " & Title & ".xlsm"
CurrentFile = ThisWorkbook.Name
NewFileType = "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm,"
NewFile = Application.GetSaveAsFilename(InitialFileName:=Name, fileFilter:=NewFileType)
If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
Application.ScreenUpdating = True
createemail:
On Error Resume Next
With OutMail
.to = ""
.CC = ""
.BCC = ""
.Subject = ActualStartDate & " - CR COMPLETION - " & Title
.BodyFormat = olFormatHTML
.htmlBody = "Please see details of the Change Request Completion Below "
.htmlBody = .htmlBody & "<br/><br/><b>Actual Start Date: </b>" & ActualStartDayName & ", " & ActualStartDate
.htmlBody = .htmlBody & "<br/><b>Actual Start Time: </b>" & ActualStartTime
.htmlBody = .htmlBody & "<br/><b>Actual End Date: </b>" & ActualEndDayName & ", " & ActualEndDate
.htmlBody = .htmlBody & "<br/><b>Actual End Time: </b>" & ActualEndTime
.htmlBody = .htmlBody & "<br/><b>Completion Status: </b>" & CompletionStatus_State
.htmlBody = .htmlBody & "<br/><b>Closure Notes: </b>" & ClosureNotes
.htmlBody = .htmlBody & "<br/><br/><br/><br/><br/><br/>"
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
The saving of the excel works fine
When the code then tries to create the email I get the following message
Object Reference not set to an instance of the object
I click the okay button below the message and the email is created
It's probably something obvious but I'm struggling to see what I am doing wrong
Many Thanks
Here is the code
Sub Close_CR()
Dim OutApp As Object
Dim OutMail As Object
Dim exptrknumber As String
Dim claimantemail As String
Dim ccemail As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
StartDate = [C11]
StartTime = [E11]
EndDate = [G11]
EndTime = [I11]
ActualStartDate = [C41]
ActualStartTime = [E41]
ActualEndDate = [G41]
ActualEndTime = [I41]
ActualStartDate = Format(ActualStartDate, "Long Date")
ActualEndDate = Format(ActualEndDate, "Long Date")
ActualStartTime = Format(ActualStartTime, "hh:mm")
ActualEndTime = Format(ActualEndTime, "hh:mm")
ActualStartDayName = Format(ActualStartDate, "dddd")
ActualEndDayName = Format(ActualEndDate, "dddd")
Title = [I13]
CompletionStatus_State = [C43]
ClosureNotes = [C45]
Application.ScreenUpdating = False ' Prevents screen refreshing.
Name = ActualStartDate & " - CR COMPLETION - " & Title & ".xlsm"
CurrentFile = ThisWorkbook.Name
NewFileType = "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm,"
NewFile = Application.GetSaveAsFilename(InitialFileName:=Name, fileFilter:=NewFileType)
If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
Application.ScreenUpdating = True
createemail:
On Error Resume Next
With OutMail
.to = ""
.CC = ""
.BCC = ""
.Subject = ActualStartDate & " - CR COMPLETION - " & Title
.BodyFormat = olFormatHTML
.htmlBody = "Please see details of the Change Request Completion Below "
.htmlBody = .htmlBody & "<br/><br/><b>Actual Start Date: </b>" & ActualStartDayName & ", " & ActualStartDate
.htmlBody = .htmlBody & "<br/><b>Actual Start Time: </b>" & ActualStartTime
.htmlBody = .htmlBody & "<br/><b>Actual End Date: </b>" & ActualEndDayName & ", " & ActualEndDate
.htmlBody = .htmlBody & "<br/><b>Actual End Time: </b>" & ActualEndTime
.htmlBody = .htmlBody & "<br/><b>Completion Status: </b>" & CompletionStatus_State
.htmlBody = .htmlBody & "<br/><b>Closure Notes: </b>" & ClosureNotes
.htmlBody = .htmlBody & "<br/><br/><br/><br/><br/><br/>"
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub