Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Preventing macros in attachment
Hello All:
I am running into an issue that I can't seem to figure out no matter what I try. I have the below automated email macro that runs on selection, but when attaching the attachment the rest of the Macros are present creating too large of an attachment. How can I keep the macros from moving into the attachment in the programming below? Sub FIELDWORK95EMAILSUBMISSION() 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 sh As Worksheet With Application .ScreenUpdating = False .EnableEvents = False End With Set Sourcewb = ActiveWorkbook 'Copy the sheets to a new workbook Sheets("Sheet1").Select Application.DisplayAlerts = False ActiveWindow.SelectedSheets.Delete Application.DisplayAlerts = True Sheets("Final Form").Visible = True Sheets("Final Form").Select Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Set Destwb = ActiveWorkbook Set sh = Sheets("Final Form") 'Determine the Excel version and file extension/format With Destwb If Val(Application.Version) < 12 Then 'You use Excel 97-2003 FileExtStr = ".xls": FileFormatNum = -4143 Else 'You use Excel 2007 'We exit the sub when your answer is NO in the security dialog that you only 'see when you copy a sheet from a xlsm file with macro's disabled. If Sourcewb.Name = .Name Then With Application .ScreenUpdating = True .EnableEvents = True End With MsgBox "Your answer is NO in the security dialog" Exit Sub 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 If End With ' 'Change all cells in the worksheets to values if you want 'For Each sh In Destwb.Worksheets Sheets("Final Form").Select With Sheets("Final Form").UsedRange .Cells.Copy .Cells.PasteSpecial xlPasteValues .Cells(1).Select End With Application.CutCopyMode = False Destwb.Sheets("Final Form").Select 'Next sh With Application.ReplaceFormat.Font .Subscript = False .ColorIndex = 2 End With Cells.Replace What:="0", Replacement:="0", LookAt:=xlWhole, SearchOrder _ :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True Sheets("Final Form").Select 'Save the new workbook/Mail it/Delete it TempFilePath = Environ$("temp") & "\" TempFileName = Range("F5") Sheets("Final Form").Select Set OutApp = CreateObject("Outlook.Application") OutApp.Session.Logon Set OutMail = OutApp.CreateItem(0) With Destwb .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum On Error Resume Next With OutMail .To = "" .CC = "" .BCC = "" .Subject = Range("F5") .Body = "Text goes here" .Attachments.Add Destwb.FullName 'You can add other files also like this '.Attachments.Add ("C:\test.txt") .Display 'or use .send End With On Error GoTo 0 .Close SaveChanges:=False End With 'Delete the file you have send Kill TempFilePath & TempFileName & FileExtStr Set OutMail = Nothing Set OutApp = Nothing With Application .ScreenUpdating = True .EnableEvents = True End With End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Preventing macros in attachment
I have had some success doing the same thing by copying the sheet/s I want
to email into a new workbook and e-mailing that new workbook. The code you quoted doesn't seem to be creating a new workbook, just replacing all the formulas with values then e-mailing the whole thing. Here's what I've used to create the new workbook: ========================== Sub CreateSalesFile() ActiveSheet.Range("statustext") = "Creating Sales File" ' make sure the user has entered required info If Range("date") = "" Then NoDate Exit Sub Else End If ' make it so the user can't see the routing running Application.ScreenUpdating = False 'select the sheet I want then copy it to a new book Sheets("Sales").Select Sheets("Sales").Copy Workbooks(Workbooks.Count).Activate ActiveSheet.Unprotect 'overwrite all the formulas with values Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False Range("A1").Select ' save the new book ChDir "I:\department\Accounting\Daily Tonnes\DailyReports" ActiveWorkbook.SaveAs Filename:= _ "I:\department\Accounting\Daily Tonnes\DailyReports\" & Range("Date") & "-Sales.xls" 'restore the environment and provide feedback ActiveWindow.Close 'MsgBox "File: I:\department\Accouting\Daily Tonnes\DailyReports\" & Range("Date") & "-Sales.xls has been created", , "Daily Tonnes" Workbooks("Daily Tonnes Model.xls").Activate Sheets("Main").Activate 'Range("E28") = "Done" Range("Date").Select Application.ScreenUpdating = True ActiveSheet.Range("statustext") = "Sales File " & Range("Date") & "-Sales.xls Created" End Sub ======================= To e-mail it, I use Outlook Redemption (http://www.dimastr.com/redemption/) to avoid all the silly questions that Outlook asks every time you want to send an e-mail with code from excel. ======================= Sub EmailOutSales() On Error GoTo OhFark Dim SafeItem As Object 'dim oitem as Set SafeItem = CreateObject("Redemption.SafeMailItem") Set oItem = Outlook.CreateItem(0) SafeItem.Item = oItem 'adds recipients from a list of recipients on a worksheet For Each c In Range("emailsales") If c.Text < "" Then SafeItem.Recipients.Add c.Text Else Exit For End If Next c SafeItem.Recipients.ResolveAll SafeItem.Subject = "Haulage for " & Range("date") SafeItem.Attachments.Add ("I:\department\Accounting\Daily Tonnes\DailyReports\" & Range("date") & "-sales.xls") SafeItem.Send Set SafeItem = Nothing Set oItem = Nothing gtfo: Exit Sub OhFark: MsgBox "You need to have a program called Outlook Redemption installed for this to work" & Chr(13) & _ "Outlook redemption is a nice little VB add in which gets around the sendmail macro protection in Outlook" _ , , "Daily Tonnes" Resume gtfo End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Preventing users from changing macros | Excel Discussion (Misc queries) | |||
Programmatically preventing macros issues | Excel Programming | |||
preventing macros from clearing the undo list | Excel Programming | |||
preventing disabling of macros | Excel Programming | |||
Parametrized Macros preventing other macros | Excel Programming |