0

Big but maybe simple help is needed!

I am using excel on Windows 10 with Outlook to send emails.

Worksheet

My worksheet looks as per image, where I have some inputs (example email, item, company...) and a cell called "Trigger Column (templates)" from where I set a macro in which each time I select a new template from the dropdown list, the "Status" column will change from "sent!" in "to be sent".

What I want to do not is to send ONLY the templates that have in status "to be sent!".

Other than "Worksheet", I have more sheets called "Sender" and "Templates".

Sender

Templates

As you can see on "Templates" sheet I have various columns. Under the column "Template content" I have some variables that can be found in the sheet "Worksheet":

Hi <Name>,
 
I hope you are well. 

We would like to order from you <Quantity> <Color> <Item> at the agreed price of <Price> EUR.

We need to receive the order no later than <Deadline>.

<Signature>

What I would really love to do is to be able to send only to the rows marked as "to be sent!" the email as per the template, replacing any variable with the correct data.

What currently have in the Marco is:

Sub CommandButton1_Click()
ActiveWorkbook.Save
 Dim sh As Worksheet
Dim rw As Range
Set sh = ActiveSheet
 
Dim oShell
Dim oExec
Set oShell = CreateObject("WScript.Shell")
 
rowIndex = 0
 
statusIndex = getColIndexByTitle("Status")
 
 script2 = "javaw -jar " + """" + Application.ActiveWorkbook.Path + "/SupplierMailSender.jar" + """ " + """" + Application.ActiveWorkbook.FullName + """"
Set oExec = oShell.Exec(script2)
Debug.Print script2
x = oExec.StdOut.ReadLine
 
 
Set ws = Application.ActiveSheet
 
Dim WrdArray() As String
 
WrdArray() = Split(x)
For i = LBound(WrdArray) To UBound(WrdArray)
  rowIndex = CInt(WrdArray(i))
 
  ws.Cells(rowIndex, CInt(statusIndex)).Value = "sent!"
 
Next i
 
Debug.Print x
 ActiveWorkbook.Save
End Sub
 
Public Function getColIndexByTitle(rowName As String) As Integer
 
  titleRowIndex = 1
  col = 1
 
  Set ws = Application.ActiveSheet
 
  Do While True
 
   If ws.Cells(titleRowIndex, col).Value = "" Then
    Exit Do
   End If
 
   If ws.Cells(titleRowIndex, col).Value = rowName Then
    getColIndexByTitle = col
    Exit Do
   End If
 
  col = col + 1
 
  Loop
End Function
 
 
Private Sub Worksheet_Change(ByVal Target As Range)
    statusCol = getColIndexByTitle("Status")
    templateCol = getColIndexByTitle("Trigger Column (templates)")
 
    If (templateCol = Target.Column) Then
   
     For Each cel In Target ' do the next steps for each cell that was changed
        Set ws = Application.ActiveSheet
        ws.Cells(cel.Row, statusCol).Value = "to be sent!"
     Next cel
    
    End If
   
End Sub

The problem is that I took from an example this but I do not have:

script2 = "javaw -jar " + """" + Application.ActiveWorkbook.Path + "/SupplierMailSender.jar" + """ " + """" + Application.ActiveWorkbook.FullName + """"
Set oExec = oShell.Exec(script2)
Debug.Print script2
x = oExec.StdOut.ReadLine

I guess this is the part that should be replaced to create the Outlook email. I am unsure if I can send it directly from VBA or if the email needs a Java file. In that case, I do not know Java code and I need some help to send the email correctly. I really hope an Angel can help!!!

Alessio
  • 13
  • 1
  • 6
  • 2
    Do you need to use Java here? Why not use VBA to send the mails? – Tim Williams Feb 04 '23 at 19:07
  • Look at the "related" posts in the side bar: [this one](https://stackoverflow.com/questions/8469960/send-an-email-from-excel-2007-vba-using-an-outlook-template-set-variables?rq=1) looks like a good starting point? – Mathieu Guindon Feb 04 '23 at 19:21
  • Tim: I might be able to only use VBA, but I do not know how to write that part of the code. Note that I wish to send only the emails marked as "to be sent!" and have the subject and body of the email on a cell in excel. – Alessio Feb 04 '23 at 21:39
  • Mathieu: I am new to this so I do not know well how to take from a "good starting point", but seeing it, the code there is written in a way that seems like the template is not taken from a cell in excel, but it is written in the code, so it is not changeable. – Alessio Feb 04 '23 at 21:41

0 Answers0