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? - Sub Mail_ActiveSheet()
-
'Working in 97-2007
-
Dim FileExtStr As String
-
Dim FileFormatNum As Long
-
Dim Sourcewb As Workbook
-
Dim Destwb As Workbook
-
Dim TempFilePath As String
-
Dim TempFileName As String
-
-
With Application
-
.ScreenUpdating = False
-
.EnableEvents = False
-
End With
-
-
Set Sourcewb = ActiveWorkbook
-
-
'Copy the sheet to a new workbook
-
Sheets("Sheet2").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
-
'We exit the sub when your answer is NO in the security dialog that you only
-
'see when you copy a sheet from a xlsm file with macro's disabled.
-
If Sourcewb.Name = .Name Then
-
With Application
-
.ScreenUpdating = True
-
.EnableEvents = True
-
End With
-
MsgBox "Your answer is NO in the security dialog"
-
Exit Sub
-
Else
-
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 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 = "Headcount " & Format(Now, "dd-mmm-yy h-mm-ss")
-
-
With Destwb
-
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
-
On Error Resume Next
-
.SendMail "mrqwe@hotmails.com", _
-
"Headcount&Turnover"
-
On Error GoTo 0
-
.Close SaveChanges:=False
-
End With
-
-
'Delete the file you have send
-
Kill TempFilePath & TempFileName & FileExtStr
-
-
With Application
-
.ScreenUpdating = True
-
.EnableEvents = True
-
End With
-
End Sub
-
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)
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.
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.
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? - Sub Mail_ActiveSheet()
-
'Working in 97-2007
-
Dim FileExtStr As String
-
Dim FileFormatNum As Long
-
Dim Sourcewb As Workbook
-
Dim Destwb As Workbook
-
Dim TempFilePath As String
-
Dim TempFileName As String
-
-
With Application
-
.ScreenUpdating = False
-
.EnableEvents = False
-
End With
-
-
Set Sourcewb = ActiveWorkbook
-
-
'Copy the sheet to a new workbook
-
Sheets("Sheet2").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
-
'We exit the sub when your answer is NO in the security dialog that you only
-
'see when you copy a sheet from a xlsm file with macro's disabled.
-
If Sourcewb.Name = .Name Then
-
With Application
-
.ScreenUpdating = True
-
.EnableEvents = True
-
End With
-
MsgBox "Your answer is NO in the security dialog"
-
Exit Sub
-
Else
-
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 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")
-
-
With Destwb
-
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
-
On Error Resume Next
-
.SendMail ActiveWorkbook.Worksheets("Sheet3").Cells(2, 1), _
-
"This is the Subject line"
-
On Error GoTo 0
-
.Close SaveChanges:=False
-
End With
-
-
'Delete the file you have send
-
Kill TempFilePath & TempFileName & FileExtStr
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"
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?
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.
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: NotGiven |
last post by:
|
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...
|
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...
|
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
|
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:
| |
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.
|
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
|
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
|
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;
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |