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 9017
Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...
ActiveWorkbook.Worksheets("Sheet3").Cells(2, 1)
Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...
ActiveWorkbook.Worksheets("Sheet3").Cells(2, 1)
Unfortunately this did not work but thanks for your reply.
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("Sheet3").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
If Destwb is the Workbook where this cell is then change the code to ...
.SendMail Destwb.Worksheets("Sheet3").Cells(2, 1), "This is the Subject line"
If Destwb is the Workbook where this cell is then change the code to ...
.SendMail Destwb.Worksheets("Sheet3").Cells(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?
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.Worksheets("Sheet3").Cells(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.Worksheets("Sheet3").Cells(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?
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.
I get the following message:-
compile error:
Expected:=
I get the following message:-
compile error:
Expected:=
Try this ...
Destwb.SendMail Destwb.Worksheets("Sheet3").Cells(2, 1), _
"This is the Subject line"
Try this ...
Destwb.SendMail Destwb.Worksheets("Sheet3").Cells(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
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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 :
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
| |