473,786 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Send Attached excel sheet to varied email addresses

34 New Member
I am currently trying to create VBA to send a specified excel sheet to varied email sources. I currently have the code below which sends the attachment in an email to a specified source e.g. mrqwe@hotmails. com, however I want to change this so the address is chosen from a specified cell on a worksheet. I have tried to reference a cell e.g. .sendmail Sheets("sheet3" ).range("A1"),_ but this does not work. Any ideas anyone?

Expand|Select|Wrap|Line Numbers
  1.  Sub Mail_ActiveSheet() 
  2. 'Working in 97-2007
  3. Dim FileExtStr As String
  4. Dim FileFormatNum As Long
  5. Dim Sourcewb As Workbook
  6. Dim Destwb As Workbook
  7. Dim TempFilePath As String
  8. Dim TempFileName As String
  9.  
  10. With Application
  11. .ScreenUpdating = False
  12. .EnableEvents = False
  13. End With
  14.  
  15. Set Sourcewb = ActiveWorkbook
  16.  
  17. 'Copy the sheet to a new workbook
  18. Sheets("Sheet2").Copy
  19. Set Destwb = ActiveWorkbook
  20.  
  21. 'Determine the Excel version and file extension/format
  22. With Destwb
  23. If Val(Application.Version) < 12 Then
  24. 'You use Excel 97-2003
  25. FileExtStr = ".xls": FileFormatNum = -4143
  26. Else
  27. 'You use Excel 2007
  28. 'We exit the sub when your answer is NO in the security dialog that you only
  29. 'see when you copy a sheet from a xlsm file with macro's disabled.
  30. If Sourcewb.Name = .Name Then
  31. With Application
  32. .ScreenUpdating = True
  33. .EnableEvents = True
  34. End With
  35. MsgBox "Your answer is NO in the security dialog"
  36. Exit Sub
  37. Else
  38. Select Case Sourcewb.FileFormat
  39. Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
  40. Case 52:
  41. If .HasVBProject Then
  42. FileExtStr = ".xlsm": FileFormatNum = 52
  43. Else
  44. FileExtStr = ".xlsx": FileFormatNum = 51
  45. End If
  46. Case 56: FileExtStr = ".xls": FileFormatNum = 56
  47. Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
  48. End Select
  49. End If
  50. End If
  51. End With
  52.  
  53. ' 'Change all cells in the worksheet to values if you want
  54. ' With Destwb.Sheets(1).UsedRange
  55. ' .Cells.Copy
  56. ' .Cells.PasteSpecial xlPasteValues
  57. ' .Cells(1).Select
  58. ' End With
  59. ' Application.CutCopyMode = False
  60.  
  61. 'Save the new workbook/Mail it/Delete it
  62. TempFilePath = Environ$("temp") & "\"
  63. TempFileName = "Headcount " & Format(Now, "dd-mmm-yy h-mm-ss")
  64.  
  65. With Destwb
  66. .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
  67. On Error Resume Next
  68. .SendMail "mrqwe@hotmails.com", _
  69. "Headcount&Turnover"
  70. On Error GoTo 0
  71. .Close SaveChanges:=False
  72. End With
  73.  
  74. 'Delete the file you have send
  75. Kill TempFilePath & TempFileName & FileExtStr
  76.  
  77. With Application
  78. .ScreenUpdating = True
  79. .EnableEvents = True
  80. End With
  81. End Sub
  82.  
Feb 7 '08 #1
12 9055
MMcCarthy
14,534 Recognized Expert Moderator MVP
Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...

ActiveWorkbook. Worksheets("She et3").Cells(2, 1)
Feb 10 '08 #2
forrestgump
34 New Member
Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...

ActiveWorkbook. Worksheets("She et3").Cells(2, 1)
Unfortunately this did not work but thanks for your reply.
Feb 11 '08 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Unfortunately this did not work but thanks for your reply.
Why didn't it work? The syntax for referencing the value in the cell is correct.
Feb 11 '08 #4
forrestgump
34 New Member
Why didn't it work? The syntax for referencing the value in the cell is correct.
I don't know why this does not work I don't get an error messgae but the code does not connect to outlook. below is the VBA I now use with your suggestion:-
.SendMail ActiveWorkbook. Worksheets("She et3").Cells(2, 1), _
full code below. Am I doing something wrong?

Expand|Select|Wrap|Line Numbers
  1. Sub Mail_ActiveSheet()
  2. 'Working in 97-2007
  3.     Dim FileExtStr As String
  4.     Dim FileFormatNum As Long
  5.     Dim Sourcewb As Workbook
  6.     Dim Destwb As Workbook
  7.     Dim TempFilePath As String
  8.     Dim TempFileName As String
  9.  
  10.     With Application
  11.         .ScreenUpdating = False
  12.         .EnableEvents = False
  13.     End With
  14.  
  15.     Set Sourcewb = ActiveWorkbook
  16.  
  17.     'Copy the sheet to a new workbook
  18.     Sheets("Sheet2").Copy
  19.     Set Destwb = ActiveWorkbook
  20.  
  21.     'Determine the Excel version and file extension/format
  22.     With Destwb
  23.         If Val(Application.Version) < 12 Then
  24.             'You use Excel 97-2003
  25.             FileExtStr = ".xls": FileFormatNum = -4143
  26.         Else
  27.             'You use Excel 2007
  28.             'We exit the sub when your answer is NO in the security dialog that you only
  29.             'see  when you copy a sheet from a xlsm file with macro's disabled.
  30.             If Sourcewb.Name = .Name Then
  31.                 With Application
  32.                     .ScreenUpdating = True
  33.                     .EnableEvents = True
  34.                 End With
  35.                 MsgBox "Your answer is NO in the security dialog"
  36.                 Exit Sub
  37.             Else
  38.                 Select Case Sourcewb.FileFormat
  39.                 Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
  40.                 Case 52:
  41.                     If .HasVBProject Then
  42.                         FileExtStr = ".xlsm": FileFormatNum = 52
  43.                     Else
  44.                         FileExtStr = ".xlsx": FileFormatNum = 51
  45.                     End If
  46.                 Case 56: FileExtStr = ".xls": FileFormatNum = 56
  47.                 Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
  48.                 End Select
  49.             End If
  50.         End If
  51.     End With
  52.  
  53.     '    'Change all cells in the worksheet to values if you want
  54.     '    With Destwb.Sheets(1).UsedRange
  55.     '        .Cells.Copy
  56.     '        .Cells.PasteSpecial xlPasteValues
  57.     '        .Cells(1).Select
  58.     '    End With
  59.     '    Application.CutCopyMode = False
  60.  
  61.     'Save the new workbook/Mail it/Delete it
  62.     TempFilePath = Environ$("temp") & "\"
  63.     TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
  64.  
  65.     With Destwb
  66.         .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
  67.         On Error Resume Next
  68.         .SendMail ActiveWorkbook.Worksheets("Sheet3").Cells(2, 1), _
  69.                   "This is the Subject line"
  70.         On Error GoTo 0
  71.         .Close SaveChanges:=False
  72.     End With
  73.  
  74.     'Delete the file you have send
  75.     Kill TempFilePath & TempFileName & FileExtStr
Feb 12 '08 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
If Destwb is the Workbook where this cell is then change the code to ...

.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), "This is the Subject line"
Feb 12 '08 #6
forrestgump
34 New Member
If Destwb is the Workbook where this cell is then change the code to ...

.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), "This is the Subject line"
Unfortunately i still can get this to work. Do you know if you can definiently reference a cell with the .Send mail function?
Feb 12 '08 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Unfortunately i still can get this to work. Do you know if you can definiently reference a cell with the .Send mail function?
1. Test what this is returning

Destwb.Workshee ts("Sheet3").Ce lls(2, 1)

2. If it is returning the email address then declare a string variable and pass the value to that first. Then use the variable in the sendmail function.
Feb 12 '08 #8
forrestgump
34 New Member
1. Test what this is returning

Destwb.Workshee ts("Sheet3").Ce lls(2, 1)

2. If it is returning the email address then declare a string variable and pass the value to that first. Then use the variable in the sendmail function.
I'm sorry I am not very strong with VBA I am not quite sure what I need to do?
Feb 15 '08 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
I'm sorry I am not very strong with VBA I am not quite sure what I need to do?
Put it in the immediate window and press return.
Feb 15 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
8360
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though the records are sorted by row numbers. (I had to split the fields to different sheets because Excel has a limit of 256 fields in each sheet) My sheets are quite large (~55,000 rows and 200 columns each) and I'll have to repeat this action many...
1
17418
by: Steven Stewart | last post by:
I have a user who has been using Excel for a while to keep statistics and print reports. She finds using it cumbersome because of long formulas and a lot of copying and pasting. I have designed a database for her which is intended to make things a lot easier; however, I don't have a lot of experience with Access and I find that designing the reports in Access is tedious. I want to be able to print the reports (which are simply based on...
3
8925
by: Conrad F | last post by:
Hello All, I know how to import a specific named excel sheet into a datagrid using ADO.NET by setting up a JET connection and then SELECTing data from the sheet. However, for a real world application, I would typically not know what the names of the sheets are going to be before I open the Excel file. I am wondering if there is a SELECT statement that would get me the list of sheet names so that I can place them in a combobox and
12
3228
by: elziko | last post by:
I'm using late binding (I must) to automate Excel. My code opens Excel after createing and poulating some sheets. My problem is that when the user finally decides to close Excel its process is left running until my application closes. I have tried setting my Excel.Application object to Nothing. I have tried to then fore the GC into action using:
4
14551
by: e.h.doxtator | last post by:
All I'm a Python newbie, and I'm just getting to the wonders of COM programming. I am trying to programmatically do the following: 1. Activate Excel 2. Add a Workbook 3. Add a Worksheet 4. Populate the new Worksheet 5. Repeat steps 3,4 while there is data.
5
5774
by: cdun2 | last post by:
Hello, I'm a non_VBA coder who has been asked to update the following Function: ************************** Function EMAILER_REV_BY_ACCTCODE_MACRO() On Error GoTo RA_EMAILER_Err DoCmd.SetWarnings False
7
12075
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
2
3211
by: ravir81 | last post by:
Hi, I am working on excel reporting using Perl. I am facing problem with writing the header part only once for all the excels created using Perl. Here is the code : #!/usr/local/bin/perl use strict; use Spreadsheet::ParseExcel;
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.