I wonder if anybody could just take a look at my email code. This was working just fine until today and now when it gets to the line
It just shuts excel down. I have tried this on another PC and it works just fine
Any help on this would be really appreciated
Code below
VBA Code:
Set OutlookApp = CreateObject("Outlook.Application")
Any help on this would be really appreciated
Code below
VBA Code:
Sub RunCode()
'this is running the code to copy & email material booked in sheet and colour font to black
Dim ans As Integer
ans = MsgBox("Warning this will email the Booked In list", vbOKCancel)
Select Case ans
Case vbOK
Application.ScreenUpdating = False
'ActiveSheet.Unprotect Password:="password"
Sheets("Sheet1").Activate
Call EmailMaterialBookedIn
Rows("6:105").Select
With Selection.Font
.Color = vbBlack
End With
Range("B6").Select
' ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, AllowInsertingRows:=True, AllowDeletingRows:=True, AllowFiltering:=True, Password:="password"
ActiveWorkbook.Save
Application.ScreenUpdating = True
Case vbCancel
End Select
End Sub
VBA Code:
Sub EmailMaterialBookedIn()
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
'On Error Resume Next
Sheets("Sheet1").Activate
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = Range("M3").Value
.CC = Range("M4").Value
.BCC = ""
.Subject = "Material booked in"
.Body = "Please find attached material booked in spreadsheet."
.Attachments.Add Wb2.FullName
.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub