Excel/VBA adjustment

Advertisement


Question
Hi Tom

I found this piece of code.  It will send an email to the address embedded in it; in this case:

In my workbook I have a column with a list of email addresses.  I would like to be able to click on any email address and send a copy of the worksheet that is on view.

So the list of email addresses is on “sheet1” in column “H” as well as a chart and other pieces of information.

When I click on an email address in column “H” I would like to send “Sheet1” to it.  Can you adapt this code to do this?

Thanks in advance


Chris Mitchell


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

   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-2013
         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 = "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 = ""
         .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

Answer
Christopher Mitchell,

Normally this would go in the selection Change event of the sheet where you are clicking, but then the code would be included with the sheet when you send it.  

so this will be a workbook level event and must be placed in the Thisworkbook module with the other workbook level events.

I assume if you click on a non-empty cell in column H on Sheet1 that it is an email address and you want to send sheet1 to that address

This is untested but I believe it is what you want.


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

If LCase(Sh.Name) <> LCase("Sheet1") Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Column <> 8 Then Exit Sub
If Len(Trim(Target.Value)) = 0 Then Exit Sub

   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 sAddr As String

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

   Set Sourcewb = ActiveWorkbook
   sAddr = cell.Text
   '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-2013
         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 = "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 = sAddr
         .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

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.