Macro to attach sheet clled Movement

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have VBA Code to create an email in outlook and attach the sheet called "Movement." The file to be attached to be named Group Movement.XLS and to be valued i.e.. no formulas to be shown on the attached sheet. I have VBA code below which I need amended to comply with my request

Your assistance is most appreciated


Code:
 Sub Email_Movement()


 ThisWorkbook.Activate                           'start in THIS workbook
ztext = [bodytext]                              'read in text from named cell
Zsubject = [subjectText]
  Dim OutApp As Object
Dim OutMail As Object
Dim File As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
File = ThisWorkbook.Path & "\" & ThisWorkbook.Name ' use the current workbook

With OutMail
    .To = Join(Application.Transpose(Sheets("Email").Range("AA1:AA3").Value), ";")
    .CC = Join(Application.Transpose(Range("AB1:AB7").Value), ";")
    .BCC = ""
    .Subject = Zsubject
    .Body = ztext
    
    'Attach the sheet "Movement"
    Sheets("Movement").Copy
    .Attachments.Add File:=ActiveWorkbook.FullName, _
                      Type:=olByValue, _
                      Name:="Movement.xlsm"
    
    .Display
End With

On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
To modify your code to save the "Movement" sheet as a separate file with the name "Group Movement.xls" and attach it to the email as a value-only worksheet, you can follow these steps:

  1. Save the "Movement" sheet as a new workbook in XLS format with only values and no formulas.
  2. Attach the new workbook to the email.
  3. Delete the temporary new workbook after attaching it to the email.
Here's the modified code:

VBA Code:
Sub Email_Movement()

    ThisWorkbook.Activate                           'start in THIS workbook
    ztext = [bodytext]                              'read in text from named cell
    Zsubject = [subjectText]
    Dim OutApp As Object
    Dim OutMail As Object
    Dim File As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    File = ThisWorkbook.Path & "\" & "Group Movement.xls"

    ' Save the sheet "Movement" as a new workbook with values only
    Sheets("Movement").Copy
    With ActiveSheet.UsedRange
        .Value = .Value
    End With
    ActiveWorkbook.SaveAs Filename:=File, FileFormat:=xlExcel8
    ActiveWorkbook.Close SaveChanges:=False

    With OutMail
        .To = Join(Application.Transpose(Sheets("Email").Range("AA1:AA3").Value), ";")
        .CC = Join(Application.Transpose(Range("AB1:AB7").Value), ";")
        .BCC = ""
        .Subject = Zsubject
        .Body = ztext

        ' Attach the new workbook
        .Attachments.Add Source:=File

        .Display
    End With

    On Error GoTo 0

    ' Delete the temporary new workbook
    Kill File

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

this code saves the "Movement" sheet as a new workbook in XLS format with only values and attaches it to the email. After attaching the new workbook to the email, the temporary file is deleted.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top