I would like to create a numbers spreadsheet. In that spreadsheet I want to have COL A Email Col B City Name COL C Other City Name and so on. I will manually enter that data in.
What I would like to do next is have applescript go through line by line in the numbers spreadsheet and send an email to each person in the column. I would like to use the other variables in the subject or body of the email.
Any help would be appreciated! I am a novice at best programmer. I know a small bit of python but am new to applescript.
Pseudo code (maybe?)
tell numbers to open spreadsheet
repeat for each line of spreadsheet
tell mail to create email
for address COL A
for subject "Hello (COL B) I need the following Information (COL C)
for email Body "Blah Blah Blah (Col D) blah blah."
send email
end repeat
Email Function
on email(a, b, c, d)
set recipientName to a
set recipientAddress to b
set theSubject to c & " --> " & d & "Shipment"
set theContent to a & ", if you would like assistance with your shipment moving from " & c & " to " & d & " , or any other shipment you may have, please let me know. We have drivers available in your Area of Operations ready for pick up."
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
end email
my email(COL A, COL B, COL C, COL D)
This is what I hacked together! I'm a newbie so be gentle. Improvements and critiques are greatly appreciated!
on email(emails, names, origin, destination)
set recipientName to names
set recipientAddress to emails
set theSubject to "c" & " --> " & "d" & "Shipment"
set theContent to names & ", if you would like assistance with your shipment moving from " & "c" & " to " & "d" & " , or any other shipment you may have, please let me know. We have drivers available in your Area of Operations ready for pick up."
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
end email
on recipientNames()
set y to 1 as string
tell application "Numbers"
activate
open alias "Macintosh HD:Users:TYPKRFT:Desktop:Form.numbers"
tell table 1 of sheet 1 of document 1
set myNames to {}
repeat with i from 1 to the count of rows
set x to "B" & y
set myNames to myNames & the value of the cell x
set y to y as integer
set y to y + 1
set y to y as string
end repeat
end tell
end tell
set itemsToDelete to {"missing value", "Name", missing value, "stop"}
set cleanNames to {}
repeat with i from 1 to count myNames
if {myNames's item i} is not in itemsToDelete then set cleanNames's end to myNames's item i
end repeat
return cleanNames
end recipientNames
on emailAddress()
set y to 1 as string
tell application "Numbers"
activate
open alias "Macintosh HD:Users:TYPKRFT:Desktop:Form.numbers"
tell table 1 of sheet 1 of document 1
set myEmails to {}
repeat with i from 1 to the count of rows
set x to "C" & y
set myEmails to myEmails & the value of the cell x
set y to y as integer
set y to y + 1
set y to y as string
end repeat
end tell
end tell
set itemsToDelete to {"missing value", "Email", missing value, "stop"}
set cleanEmails to {}
repeat with i from 1 to count myEmails
if {myEmails's item i} is not in itemsToDelete then set cleanEmails's end to myEmails's item i
end repeat
return cleanEmails
end emailAddress
set a to emailAddress()
set b to my recipientNames()
set reps to count of a
set x to 1
repeat reps times
set emails to item x of a
set names to item x of b
my email(emails, names)
set x to x + 1
end repeat
Related
I am relatively new to writing VB scripts. Essentially, I need to get a functioning VB script to send an email to multiple recipients which vary each email. I need it to have Subject Line, Email Body, Attachment and flexibility to add multiple recipients in the TO, CC and BCC fields without adding individual lines of Add.Recipient for each email address in the TO field. Does anyone have any suggestions or know of any resources to find this information?
I have looked myself and coming up somewhat blank on it. I have the arguments set in a seperate.txt file. These will vary constantly. I am trying to do this as quickly but efficiently as possible.
I also have not had much luck with the Add.CC command so I took it out for this example...Below is what I currently have written out,
Set args = WScript.Arguments
arg1 = args.Item(0)
arg2 = args.Item(1)
arg3 = args.Item(2)
ToAddress = ""&arg1&""
CCAddress = ""&arg2&""
MessageSubject = "Your Order"
MessageBody = "Please find your Order Attached"
MessageAttachment = ""&arg3&""
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send
I have the arguments written out as:
cscript //nologo test1.vbs email1#email.com email2#email.com y:\folder\test.txt
'Get email addresses, the -2 ignores the attachment
For x = 0 to WScript.Arguments.Count - 2
Msgbox Wscript.Arguments(x) & "Count " & x
Next
Msgbox Wscript.Arguments(wscript.arguments.count - 1) & " Attachment"
In programming we mostly use procedural steps, as you do. However very frequently we need to do loops. Because we wanted all but the last and then the last I used a For x = loop. For Each loops are usually better and cleaner code.
This prints out all arguments
For each Ag in WScript.Arguments
Msgbox Ag
Next
I created a query and a form in Microsoft Access 2010. The form, named TEST, looks as follows:
Field1 Field2
a 200
b 400
In VBA I tried to access the different fields in the form:
Form_TEST.Field1....
I want to save the values 200 and 400 in an integer variable (Dim a As Integer) and print it using MsgBox. How can i achieve that??
You can use the Me as the open form and assign the variable if you know the name of the text box.
Dim intValue as Integer
'If text box name = TextBox1
intValue = Me.TextBox1.Value
I'll try to help you.
I understood you created the form with the wizard putting the 2 fields on the form.
What is not clear is the View that you are using.
Well, your form can be displayed in different ways:
- Single form
- Continuous forms
- Datasheet
This is defined by the Default View property.
To see the properties of you form press F4 to see properties and select "Form" as the object that you want to see.
If your form is Single Form or Continuous form you can access the two fields you put on it simply addressing them.
Click on the controls you put on the form and press F4 to see the control name.
CASE 1 - SINGLE FORM VIEW
Let's assume that your controls are named Text1 (200) and Text2 (400) and for convenience your form is a single form.
So you can refer to values in the 2 controls writing
Dim intText1 As Integer, intText2 As Integer
intText1 = Me.Text1.Value
intText2 = Me.Text2.Value
The .Value property is not mandatory cause it's the default property.
At this point you can print out intText1,2 with a MsgBox
MsgBox "Text1 = " & CStr(intText1)+ " - Text2 = " & CStr(intText2)
This will show Text1 = 200 - Text2 = 400
CASE 2 - CONTINUOUS FORMS VIEW
Let's now assume that your view is Continuous form.
So the field that contains 200 and 400 is just one but each record (row) is a form repeated as many times as the number of records.
In this case to access all the records and store them to an array you can use this in the Form_Load event (you can access it by the Control Properties Window - F4)
Option Explicit
Option Base 1 ' Set the base index for vectors to 1
Dim rst as DAO.Recordset ' Define a recordset to allocate all query records
Dim Values as Variant ' Define a variant to allocate all the values
set rst = me.RecordsetClone ' Copy all records in rst
rst.MoveLast ' Go to last record
intNumRecords = rst.RecordCount ' Count records
rst.MoveFirst ' Go back to recordset beginning
ReDim Values(intNumRecords) ' Resize Values to allocate all values
i = 1
Do While Not rst.EOF ' Cycle over all records
Values(i) = rst!FieldName ' FieldName is the name of the field of
' the query that stores 200 and 400
i = i + 1 ' Move to next array element
rst.MoveNext ' Move to next record
Loop
rst.Close ' Close recordset
set rst = Nothing ' Release memory allocated to rst
for i = 1 To intNumRecords ' Show easch value as message box
MsgBox Values(i)
next i
NOTES
Please NOte that this solution works if you have less than 32767 records to show (the maximum integer with sign that you can store).
The msgbox obliges you to press OK at each value. It's not so comfortable.
Please tell me if it's what you were looking for.
Bye,
Wiz
I have two worksheets. I have to copy values to first worksheet from second worksheet but on basis of column name.
For coping data from column G to C I am using-
If Wks2.Range("C" & I) <> Wks.Range("G" & J).Value Then
Wks2.Range("C" & I).Value = Wks.Range("G" & J)
End If
But the problem here is that column sequence keeps on changing in secong one. So mapping cannot be hardcoded on column alphabet.
I not sure how to map them using column headers.
Thank you in advance.
You can search ColumnName in Header. After find the matching item ,retrieve its row as reference ,
Dim cellRef as integer
Set e = Worksheets(2).Rows(1).find(ColumnName)
If not e is nothing then
cellRef = e. column
End if
OK.
So , assume that your columnnames are in Row#1 of worksheets(2) = wks2
Yoh have to find the letter of columnName ( here is "C" )
SearchColumn : wanted columnname
Dim e As Object
Set e = Nothing
Set e = wks2.Range("1:1").Find(SearchColumn)
If not e is nothing then
Dim colAddress As String
colAddress = e.Address
Dim colLetter As String
colLetter = Mid(colAddress, 2, InStr(2, colAddress, "$", vbTextCompare) - 2)
If Wks2.Range(colLetter & I) <> Wks.Range("G" & J).Value Then
Wks2.Range(colLetter & I).Value = Wks.Range("G" & J)
End
Else
msgbox("Can not find " & SearchColumn)
End if
End If
First of All , we find the column ( e )
e.address returns the found column address
then colLetter store the column letter of it.
Please inform me for any question
C
Have not been able to find anything that fits my needs.
I have two columns of values (L and U). Column L contains a file names that includes a date in MM-DD-YYYY format (example yadayadayada thru (03-15-2015).pdf) column U contains a date. What I need to do is have a macro compare the date within the file name to the date in the column U. Other dates may appear within the text in column L but the date I need to compare against is always after "thru" and in parentheses followed by the file extension. If they do not match, I need the value in column U highlighted and replaced with the text "FAIL". I'm going to continue searching but any help is greatly appreciated. Thanks!
Does it have to be VBA? This can be accomplished with Conditional Formatting.
Apply this conditional format formula to column U:
=AND(U1<>"",L1<>"",U1<>--TRIM(MID(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(L1," ",REPT(" ",255)),255)),")",REPT(" ",255)),2,255)))
And set the number format to Custom "FAIL" with yellow (or the highlight color of your choice) fill.
EDIT
If it has to be VBA, then this should work for you:
Sub tgr()
Const HeaderRow As Long = 1 'Change to your actual header row
Dim ws As Worksheet
Dim rngFail As Range
Dim rngFiles As Range
Dim FileCell As Range
Dim dDate As Double
Set ws = ActiveWorkbook.ActiveSheet
Set rngFiles = ws.Range("L" & HeaderRow + 1, ws.Cells(Rows.Count, "L").End(xlUp))
If rngFiles.Row < HeaderRow + 1 Then Exit Sub 'No data
For Each FileCell In rngFiles.Cells
If Len(Trim(FileCell.Text)) > 0 Then
dDate = 0
On Error Resume Next
dDate = CDbl(CDate(Trim(Mid(Replace(Trim(Right(Replace(FileCell.Text, " ", String(255, " ")), 255)), ")", String(255, " ")), 2, 255))))
On Error GoTo 0
If dDate <> ws.Cells(FileCell.Row, "U").Value2 Then
Select Case (rngFail Is Nothing)
Case True: Set rngFail = ws.Cells(FileCell.Row, "U")
Case Else: Set rngFail = Union(rngFail, ws.Cells(FileCell.Row, "U"))
End Select
End If
End If
Next FileCell
If Not rngFail Is Nothing Then
rngFail.Value = "FAIL"
rngFail.Interior.ColorIndex = 6
End If
End Sub
Please can you check this script? I can not get it to work - I would like to use this to send the same e-mail to different addresses but with a personalised greeting.
— Script requires two files placed in the same folder as the script.
— “Email Addresses.txt" should contain email addresses separated by a carriage return.
— “Email Message.txt" should contain the Subject on the first line then the message body below (seperated by a carriage return).
— Get path to script where the support files should be stored.
set myPath to (path to me) as text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set textChunks to text items of myPath
if last item of textChunks is "" then set textChunks to _chopLast(tectChunks)
set myFolderPath to _chopLast(textChunks) as text
set AppleScript's text item delimiters to oldDelims
log myPath
log myFolderPath
tell application "Finder"
-- Get the list of recipients
set recFile to (myFolderPath & ":Email Addresses.txt")
set recList to ""
set recFileID to (open for access (recFile as alias))
-- Extract text from the file
try
set fileLength to (get eof recFileID)
set recList to (read file recFile from 1 to (fileLength))
on error error_message number error_number
display alert "Error number: " & (error_number as string) & return ¬
& ("Message: ") & error_message
close access recFileID
end try
log recList
-- Get the email subject and body
set msgFile to (myFolderPath & ":Email Message.txt")
set msgFileID to (open for access (msgFile as alias))
-- Extract text from the file
try
set fileLength to (get eof msgFileID)
set emailBody to (read file msgFile from 1 to (fileLength))
on error error_message number error_number
display alert "Error number: " & (error_number as string) & return ¬
& ("Message: ") & error_message
close access msgFileID
end try
log emailBody
-- Seperate Subject from Body
set emailSubject to the first paragraph of emailBody
log emailSubject
set emailMsg to paragraphs 2 thru (count of paragraphs in emailBody) of emailBody
log emailMsg
set recListList to paragraphs in recList
-- Loop for each address.
repeat with eachAddress in (recListList)
set txtURL to ("mailto:" & eachAddress & "?subject=" & emailSubject & "&body=" & emailMsg as string)
open location txtURL
tell application "Mail"
activate
-- Uncomment the following line if you want to automatically send messages
-- send newMessage
end tell
end repeat
end tell
on _chopLast(theList)
return reverse of (rest of (reverse of theList))
end _chopLast
set addresses to "aa#example.com
bb#example.com"
set title to "title"
set body to "body"
--set body to read "/Users/username/Documents/body.txt" as «class utf8»
repeat with a in paragraphs of addresses
tell application "Mail"
activate
tell (make new outgoing message)
set visible to true
make new recipient at end of to recipients with properties {address:a}
set subject to title
set content to body
--send
end tell
end tell
end repeat