473,799 Members | 3,029 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
12 9058
forrestgump
34 New Member
I get the following message:-

compile error:
Expected:=
Feb 18 '08 #11
MMcCarthy
14,534 Recognized Expert Moderator MVP
I get the following message:-

compile error:
Expected:=
Try this ...

Destwb.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), _
"This is the Subject line"
Feb 18 '08 #12
forrestgump
34 New Member
Try this ...

Destwb.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), _
"This is the Subject line"
Unfortunately it still does not work. Whenever I try and refer to a cell on the workbook it does not connect to outlook. Strange!

Regards,

Forrest
Feb 19 '08 #13

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

Similar topics

8
8362
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
17420
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
3229
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
14552
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
12077
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
9538
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
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
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
10025
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
9068
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...
0
6804
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
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.