VBA Help- not selecting the set range

NeoSez

Board Regular
Joined
Aug 14, 2020
Messages
210
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I am trying to send Sheet1 as an attachment but it will not select the range A1:I23 , but sends the entire sheet, which I don't want.
Also, how do I change it to paste special and not include formulas? I tried adding it but it kept giving errors.
What am I missing?
Thanking you in advance for any suggestions.

VBA Code:
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
    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 rng As Range

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    '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-2016
            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

   '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "New SO Request " & 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
       
        'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want


    Set rng = Sheets("Sheet1").Range("A1:I23").SpecialCells(xlCellTypeVisible)
   
    On Error GoTo 0


   
       
        With OutMail
            .to = "TES@EMIAL.COM
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line"
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send   'or use .Display
        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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
HI Dave. I was trying to send the specified range from that sheet as an excel attachment in an email. I do not want to use UsedRange as it picks up the entire sheet. I want a specific range as a paste special to remove formulas.
 
Upvote 0
You would have to add a new workbook, then copy the preferred range into the new workbook, save it and then add as an attachment.

--------------------------------------------------
It looks like you are just wanting to send information.
You can PDF that specific range and send the PDF as an attachment.
VBA Code:
Sub PDFrng()
    Dim oApp As Object
    Dim oMail As Object
    Dim wb As Workbook
    Dim rng As Range
    fname = Environ("Temp") & "\copySheet.pdf"

    'Turn off screen updating
    Application.ScreenUpdating = False
    'Make a copy of the active sheet and save it to a temporary file
    Set wb = ActiveWorkbook
    Set rng = wb.Sheets("Sheet1").Range("A1:I23")
    
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fname, Quality:=xlQualityStandard, _
                            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
                            False

    'Create and show the outlook mail item
    Set oMail = CreateObject("Outlook.Application").CreateItem(0)
    With oMail                                   'Add a recipient
        .To = "Marcus.Small@YourEmail.com.au"
        .Subject = "Add a PDF"
        .Attachments.Add fname
        .Display
    End With

    Kill fname
    Application.ScreenUpdating = True
    Set oMail = Nothing
End Sub
 
Upvote 0
You would have to add a new workbook, then copy the preferred range into the new workbook, save it and then add as an attachment.

--------------------------------------------------
It looks like you are just wanting to send information.
You can PDF that specific range and send the PDF as an attachment.
VBA Code:
Sub PDFrng()
    Dim oApp As Object
    Dim oMail As Object
    Dim wb As Workbook
    Dim rng As Range
    fname = Environ("Temp") & "\copySheet.pdf"

    'Turn off screen updating
    Application.ScreenUpdating = False
    'Make a copy of the active sheet and save it to a temporary file
    Set wb = ActiveWorkbook
    Set rng = wb.Sheets("Sheet1").Range("A1:I23")
   
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fname, Quality:=xlQualityStandard, _
                            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
                            False

    'Create and show the outlook mail item
    Set oMail = CreateObject("Outlook.Application").CreateItem(0)
    With oMail                                   'Add a recipient
        .To = "Marcus.Small@YourEmail.com.au"
        .Subject = "Add a PDF"
        .Attachments.Add fname
        .Display
    End With

    Kill fname
    Application.ScreenUpdating = True
    Set oMail = Nothing
End Sub
I really appreciate the work and the code you have put together, however, as previously stated, it needs to be an excel attachment as it needs to be modified and sent out again. I will save this PDF code for future reference.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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