tell application "Microsoft Excel"
set database_workbook to workbook "Contacts Database (for script testing).xls"
activate database_workbook
set database_sheet to sheet "database" of database_workbook
set from_tp_sheet to sheet "TP from mailout"
set from_website_sheet to sheet "TP from website"
end tell
tell application "Mail"
try
# initialise lists
set notes to {""}
set website_emails to {}
set mailout_emails to {}
set other_emails to {}
set email_selection to (get selection)
# loop thru all selected email messages and sort into three groups
repeat with a_msg in email_selection
# Determine if source is website or email
set email_subject to subject of a_msg
set email_source to {""}
if the (count of words of email_subject) > 2 then
set email_source to words 1 thru 3 of email_subject
end if
if email_source = {"Enquiry", "from", "Website"} then
copy a_msg to the end of website_emails
log ("Website: " & email_subject)
else if email_source = {"Subscribe", "Foo", "Bar"} then
copy a_msg to the end of mailout_emails
log ("TP Mailout: " & email_subject)
else
copy a_msg to the end of other_emails
end if
end repeat
# Process group 1 of emails (from website forms)
repeat with i from 1 to (count website_emails)
set theText to content of item i of website_emails
set their_name to words 2 thru -1 of paragraph 9 of theText -- for later functionality
set comments to paragraph -5 of theText -- for later functionality
set subscriber to paragraph 5 of theText -- for later functionality
# parse paras (3rd to last paras) into Excel savvy cell range object
set pasteText to (paragraphs from paragraph 5 to the last paragraph) of theText
set cellText to {}
repeat with p in pasteText
set cellText's end to {p's contents}
end repeat
repeat 50 - (count cellText) times
set cellText's end to {""}
end repeat
set r to "A1:A" & (count cellText)
# ====================================================================
tell application "Microsoft Excel" --
tell database_workbook --
activate --
tell from_website_sheet --
tell range ("A1:A50") --
set value to cellText --
end tell --
-- set value of range "A52" to first_name --
set results_row to value of range "A52:AZ52" --
set the value of range ("A" & (54 + i) & ":AZ" & (54 + i)) to results_row --
end tell --
end tell --
end tell --
# ====================================================================
end repeat
on error the error_message number the error_number
tell application "Script Debugger Demo"
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end tell
end try
end tell