you can extract the data of all cells of the used range with one line
tell application "Microsoft Excel" to set theData to value of used range of active sheet
the result is a list of lists. Each inner list contains the value of all cells of a row
For example assuming there are three columns name, email and some other information
this code creates a new mail in Mail.app with name, address and “some other information” as subject
property theSender : "my@email.com"
tell application "Microsoft Excel" to set theData to value of used range of active sheet
repeat with aRow in theData
set {theName, theEmail, someOtherInformation} to aRow
tell application "Mail"
set newMessage to make new outgoing message with properties {sender:theSender, visible:true, subject:someOtherInformation}
tell newMessage
set content to "This is the body of the message"
make new to recipient at end of to recipients with properties {name:theName, address:theEmail}
end tell
end tell
end repeat
Try this, I commented out the line to send the mail(s).
The post code information seems to be unused.
The script assumes that the Excel sheet contains only the 7 columns with the appropriate data
property theSender : "my@email.com"
property theSubject : "The same for every message"
property theSignature : "mySignature"
tell application "Mail"
try
set mySignature to signature theSignature
on error
set mySignature to missing value
end try
end tell
tell application "Microsoft Excel" to set theData to value of used range of active sheet
repeat with aRow in theData
set {theName, address1, address2, address3, address4, postcode, theEmail} to aRow
set mailBody to "Dear " & theName & return & return
set mailBody to mailBody & "Some blurb" & return & return
set mailBody to mailBody & "I'm writing to confirm that the following contact details are correct." & return & return
set mailBody to mailBody & address1 & return
set mailBody to mailBody & address2 & return
set mailBody to mailBody & address3 & return
set mailBody to mailBody & address4 & return & return & "Thank you" & return & return
tell application "Mail"
set newMessage to make new outgoing message with properties {sender:theSender, visible:true, subject:theSubject}
tell newMessage
set content to mailBody
make new to recipient at end of to recipients with properties {address:theEmail}
if mySignature is not missing value then set message signature to theSignature
end tell
-- send newMessage
end tell
end repeat
The one missing piece of functionality is that the automatic signature (which includes an image) that normally appears on any message I send out doesn’t seem to appear on the messages this code produces. Furthermore, the box next to “Signature” in the ‘compose new message’ window displays ‘none’ where normally it would display ‘mysignature’.
So, I guess I’m asking, how do I set “mySignature” to the one I normally use?
the literal string "mySignature in the property theSignature line must match the name of one of the defined signatures.
If you’re using always the same signature it might work to delete the first Mail application tell block and
the if mySignature is not. line in the second Mail tell bolck
This is almost exactly what I need. Can I ask you a few questions:
Will all this still work with MountainLion and Excel 2011?
Is there some way to attach a file to each email? In my case, it will be the same file, although ideally, it would be nice to know how to use a file whose name was in a column of the spreadsheet.
One of the columns in my spreadsheet is a number. In the spreadsheet it shows up as 20,000. In my email it shows up as 2.0E+4. Is there some way to format the number so that it shows up as the entire number with the comma?
tell application "Mail"
try
set mySignature to signature "KJ"
on error
set mySignature to missing value
end try
end tell
-- .
tell application "Mail"
-- .
tell newMessage
-- .
if mySignature is not missing value then set message signature to mySignature
end tell
end tell
the cells have a property string value which avoids scientific numeric notation
Its very weird. When I run the script, it generates the emails with the proper signature, and then a second or so later, the signature is deleted from the email.
I created a code using VBA that allows you to send a range of cells as an attachment in Excel 2011. It also allows you to send the mail from Applemail and Outlook (help on one would be enough!). This code was modified from Ron De Bruin’s code. Is there a way to utilize this function using AppleScript for Excel 2008?
A sample of my code is below.
Dim Source As Range
Dim Destwb As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
If Val(Application.Version) < 14 Then Exit Sub
Set Source = Nothing
On Error Resume Next
Set Source = Range("A96:F134").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, " & _
"please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb = ActiveWorkbook
Set Destwb = Workbooks.Add(xlWBATWorksheet)
Source.Copy
With Destwb.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With
'Save format and extension
FileExtStr = ".xlsx": FileFormatNum = 52
'Or if you want it in xls format, use:
'FileExtStr = ".xls": FileFormatNum = 57
'Save the new workbook/Mail it/Delete it
'If you want to change the file name then change only TempFileName
TempFilePath = MacScript("return (path to documents folder) as string")
TempFileName = wb.Name & " " & Format(Date, "MMM-YYYY")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
MailFromMacwithOutlook bodycontent:="Hi there", _
mailsubject:="Incidental Tracker for" & Format(Date, "MMM-YYYY"), _
toaddress:="yum.yum@mail.com", _
ccaddress:="", _
bccaddress:="", _
attachment:=.FullName, _
displaymail:=True
.Close SaveChanges:=False
End With
KillFileOnMac TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Thank you ahead of time for your assistance and help!!