Below is the code for the main macro. When it opens up a copy of the new excel under a time stamped name. When that workbook is active, I want to remove the macros right before I send it in the email. I execute the code you supplied at that point. The problem that I stated before is that I protect the code in the original workbook so the copied workbook prevents me from deleting the code due to that protection.
Sub SendEmail()
Sheets("EFRP Form").Select
'Working in 2000-2010
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Set rng = Nothing
Set rng = Sheets("EFRP Form").Range("A1:O4")
If Application.WorksheetFunction.CountBlank(Range("A3:N3")) > 0 Then
MsgBox "You Left a required field in the form blank. Please fill in all required fields"
GoTo FinishAtEnd
End If
Range("R4").Select
ThisWorkbook.Save
If IsEmpty(ActiveCell) = True Then
MsgBox "Please put at least your own email in the CC field"
GoTo FinishAtEnd
End If
Set wb1 = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb1.FileFormat = 51 And wb1.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file, there will" & vbNewLine & _
"be no VBA code in the file you send. Save the" & vbNewLine & _
"file first as xlsm and then try the macro again.", vbInformation
Exit Sub
End If
End If
With Application
.ScreenUpdating = True
.EnableEvents = False
End With
'Make a copy of the file/Open it/Mail it/Delete it
'If you want to change the file name then change only TempFileName
TempFilePath = Environ$("temp") & "\"
TempFileName = wb1.Name & " Sent " & Format(Now, "dd-mmm-yy hh-mm-ss AM/PM")
FileExtStr = "." & LCase(Right(wb1.Name, _
Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Application.Run "RemoveAllMacros"
On Error Resume Next
With OutMail
.To = Range("R2").Value
.CC = Range("R4").Value
.Subject = Range("R3").Value
.HTMLBody = RangetoHTML(rng)
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
.Attachments.Add (Range("R5").Value)
.Send 'or use .Display
End With
On Error GoTo 0
wb2.Close SaveChanges:=False
'Delete the file
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Range("A3:O3").ClearContents
Range("R4:R5").ClearContents
Range("M3").Value = Date
MsgBox "The Form has been submitted. Please check your sent box in outlook to verify that it went through properly.", vbExclamation, "Verify Sent Email"
ThisWorkbook.Save
FinishAtEnd:
End Sub
'Written: March 08, 2008
'Author: Leith Ross
'Summary: Removes all macro code and modules from the Active Workbook.
Sub RemoveAllMacros()
Dim VBcomp As Object
Dim VBproj As Object
Set VBproj = Application.VBE.ActiveVBProject
For Each VBcomp In VBproj.VBComponents
Select Case VBcomp.Type
Case Is = 1, 2, 3 'vbext_ct_StdModule, vbext_ct_ClassModule, vbext_ct_MSForm
VBproj.VBComponents.Remove VBcomp
Case Is = 100 'vbext_ct_Document
With VBcomp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBcomp
End Sub
Bookmarks