Using if statements in the body of email Excel to Outlook - excel

I am lost on how to tackle this part of the project I have undertaken for my company. I have created thus far, code that will filter, search for count of times an item is mentioned, and store it into a variable.
I have 20 of these items and I need to send an email out with the amount of times an item was mentioned.
I would rather just send an email that shows only the items that were mentioned and leave the rest out.
Doing this in Excel is the hard part as I am unsure how to code it in the body. Any help on sending me in the right direction would be greatly appreciated.
EX.
Item 1,
Item 1,
Item 2,
Item 3.
VarForItem1 = 2
VarForItem2 = 1
VarForItem3 = 1
VarForItem4 = 0
VarForItem5 = 0
etc.
I want the email to only show the Vars that have greater than 0.

I figured it out, with a combination of if statements underneath the calculation and a strmessage string. I just added the line with the amount if it was above 0.
If item1 > 0 then
strmessage = strmessage & "Item 1 - " & strmessage & vbnewline
End if

Related

For each item in ListBox.list is returning 10 times the actual number of entries in the listbox (additional items are all null) in VBA

I am trying to access each item in a list box with a for each...loop
I am getting back 10 times the number of items in the list box - I get the actual items in the list box plus 9 times that of null
I wrote a stupid little program to see what was happening:
itemCount = 0
For i = 1 To 3
CSESelector.CSESelectedBox.AddItem "argh"
Next i
For Each myItem In CSESelector.CSESelectedBox.List
itemCount = itemCount + 1
Next myItem
myListCount = CSESelector.CSESelectedBox.ListCount
the results are myListCount = 3 and itemCount = 30. when I used .columncount, it was 1. if I put in 5 items, I get 50; if I put in 2, I get 20
I know I can do a workaround (for i= 0 to .listcount-1), but I'm trying to get the for each...loop to work. Any ideas of where I went wrong?
I'm using excel365 on windows 10, if that matters
If you set a variable to the List property and take a look at it in the watch window, you will see it's a 2-d array with the same number of "rows" as your added items, but the "columns" size is always 10 (max. possible number of columns in a listbox), regardless of the listbox ColumnCount property:

Set Task's Work Complete Field using values in Excel sheet

I'm using VBA to set each Work Complete field of a Project task using values from the Work Complete column in my Excel sheet.
When I set the Work Complete Field to 100 sometimes it sets the Project task's work complete to 0. This does not occur with any other value.
Here is my code:
For Each row In tasksDict.Keys
Dim t As task
' Get unique id in this row
UID = Range("A" & row)
workCompl = Val(Range("F" & row)) * 100
Debug.Print ("ID: " & UID & "Work complete: ") & workCompl ' This prints out the correct values
Set t = prj.tasks.uniqueID(UID) ' Get task by unique ID
If Not t.Summary Then
t.SetField FieldID:=188743713, Value:=workCompl ' Set task work complete
End If
Next row
Your code is not setting the work complete field to 100, it is multiplying the existing value in the column F by 100.
workCompl = Val(Range("F" & row)) * 100
Since you are using the above formula, you will get a 100 only when column F has a value of 1. If your objective is to set everything to 100 then use:
workCompl = 100
Hope this helps.
Edit1:
Can you try this instead of the if statement you have:
If Not t.Summary Then
projectField = FieldNameToFieldConstant("PercentComplete")
t.SetField FieldID:=projectField, Value:=workCompl ' Set task work complete
End If

VBA/Excel - Group by value in table and send e-mail

I have a large list where I have different companies and organisations. My current VBA codes iterates through each table row, and send out an e-mail for each line.
However, what I need to achieve is that there might be companies which have the same parent organisation - in this case, I don't want to send out an e-mail for each company, but would rather send out one e-mail to the organisation, including all the associated companies.
I have tried to illustrate it in below table.
So for record #1, I would just send out an e-mail as always. However for [2-3], I would like to send out one e-mail, which includes company name 2 and 3.
Same goes for record [4-6], which should also send out one e-mail, but include company name #4, #5 and #6.
Example:
Hi Organisation B
Please check below companies in your organisation:
Company 2
Company 3
My current code:
Below is my current code, which iterates through all table rows and send out mails:
'Count table rows
Set tbl = oWorksheet.ListObjects("DataTable")
TableRows = tbl.ListRows.Count
TableRows = TableRows + 1 'Skips the header
iRow = 2 'Skip the header
Do While iRow <= TableRows
Set oNotesMail = oMailDB.CreateDocument
oNotesMail.Form = "Memo"
oNotesMail.SendTo = oWorksheet.Cells(iRow, 5).Value
oNotesMail.Subject = "Company check"
oNotesMail.Body = "Hi " & oWorksheet.Cells(iRow, 2) & "Please check"
oNotesMail.SaveMessageOnSend = True
oNotesMail.PostedDate = Now()
oNotesMail.Send 0, oWorksheet.Cells(iRow, 5).Value
Set oNotesMail = Nothing
Loop
So above code send out an email (in Lotus Notes) for each line.
How can I achieve my example above?
Please note
The table will always be sorted by 'Organisation' - hopefully this will help when the code is iterating through the rows.

VBA Like Function

Basically my code works in such a way that an Incoming email subject from Outlook is compared to a list column in Excel - supposedly those existing email. If the Incoming email is matched with any records then it Shows the value in Excel. However, I noticed that, if the data in Excel contains the hashtag (#) value, even if the incoming email is not the same as the one in Excel- the code marks it as matched.
If emlSubj Like "*" & strSubj & "*" Then
Debug.Print strSubj
Is there something wrong with the code or is it the limitation of the LIKE function?
Use the InStr function instead. It returns an integer specifying the start position of the first occurrence of one string within another.
If InStr(emlSubj , strSubj) > 0 Then
Debug.Print strSubj
End If

AppleScript for sending emails from a list in Excel

I don’t know AppleScript at all so thanks in advance for any help offered on this question.
I’m on my Macbook Pro laptop with the latest version of OSX installed. I have an Excel spreadsheet (I could use numbers if that makes it easier) with two columns.
FirstName Email
------------ -----------------
Ken blah#blah.com
Mike blahblah#blahblah.com
This is my customer list and I want to send them an email. Unfortunately I don’t have this list in an autoresponder so I have to send the emails one by one.
I know that I could whip up a PHP script to send the emails, however there are issues with email deliverability when doing it this way.
I want to write an AppleScript that processes my spreadsheet one row at a time and sends a message.
The message would be something like this:
Subject: How’s it going?
Hi Ken
It’s been a while since I sold you that defective widget from China.
If you need more defective elctronics I’m here for you. Just give me
a call at xxx-xxx-xxxx.
Sincerely
Ken
The AppleScript would read the name and email address from one row of the spreadsheet and send this email, filling in name and email address, using the standard apple mail program.
After sending the message I want the script to wait 60 seconds. Then send another email.
This needs to happen until a blank row is encountered.
My first question… Is this possible? If possible how do I do it?
Also is there a better way to do what I’m trying to do?
Thanks
There's probably a better way to do this, but couldn't you just copy the addresses as TSV or CSV?
set addresses to "Ken;blah#blah.com
Mike;blahblah#blahblah.com"
set text item delimiters to ";"
repeat with l in paragraphs of addresses
tell application "Mail"
tell (make new outgoing message)
set subject to "subject"
set content to "Hi " & text item 1 of l & linefeed & linefeed & "..."
make new to recipient at end of to recipients with properties {name:text item 1 of l, address:text item 2 of l}
send
delay (random number) * 100
end tell
end tell
end repeat
Try:
set {firstName, eAddress} to getData()
repeat with i from 1 to count firstName
tell application "Mail"
activate
set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"How’s it going?"}
tell mymail
make new to recipient at beginning of to recipients with properties {address:item i of eAddress}
set content to "Hi " & item i of firstName & "
It’s been a while since I sold you that defective widget from China.
If you need more defective elctronics I’m here for you. Just give me
a call at xxx-xxx-xxxx.
Sincerely
Ken"
end tell
--show message window (otherwise it's hidden)
set visible of mymail to true
--bring Mail to front
activate
send mymail
end tell
end repeat
on getData()
set colA to {}
set colB to {}
tell application "Microsoft Excel"
activate
tell active sheet
set lastRow to first row index of (get end (last cell of column 1) direction toward the top)
repeat with i from 3 to lastRow
set end of colA to (value of range ("A" & i))
set end of colB to (value of range ("B" & i))
end repeat
end tell
end tell
return {colA, colB}
end getData

Resources