Marius Nielsen
New Member
- Joined
- Feb 12, 2014
- Messages
- 10
I'm currently working on a Ron deBruin VBA email code and wonder if there is a way to protect the copied workbook being sent off??
This is the code that I'm working with, and the copied workbook being sent off is not protected - hence the various formulas is accessible to the person receiving the email.
Sub Mail_ActiveSheet()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
strbody = "Dear " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E3").Value & (",") & vbNewLine & vbNewLine & _
"Attached is our quote as per your request." & vbNewLine & _
"Please refer to our quote number in order to ensure correct billing." & vbNewLine & _
"" & vbNewLine & _
"Best Regards," & vbNewLine & _
""
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
With Destwb
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.to = ThisWorkbook.Sheets("Ocean Export FCL").Range("E4").Value
.CC = ""
.BCC = ""
.Subject = "Your Ref/ " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E1").Value & "/ " & " " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E6").Value
.Body = strbody
.Attachments.Add Destwb.FullName
.SentOnBehalfOfName = """"" <>"
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
This is the code that I'm working with, and the copied workbook being sent off is not protected - hence the various formulas is accessible to the person receiving the email.
Sub Mail_ActiveSheet()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
strbody = "Dear " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E3").Value & (",") & vbNewLine & vbNewLine & _
"Attached is our quote as per your request." & vbNewLine & _
"Please refer to our quote number in order to ensure correct billing." & vbNewLine & _
"" & vbNewLine & _
"Best Regards," & vbNewLine & _
""
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
With Destwb
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.to = ThisWorkbook.Sheets("Ocean Export FCL").Range("E4").Value
.CC = ""
.BCC = ""
.Subject = "Your Ref/ " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E1").Value & "/ " & " " & ThisWorkbook.Sheets("Ocean Export FCL").Range("E6").Value
.Body = strbody
.Attachments.Add Destwb.FullName
.SentOnBehalfOfName = """"" <>"
.Send
End With
On Error GoTo 0
.Close savechanges:=False
End With
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub