Kenneth Hobson
Active Member
I'll have to think about post #25. It would be more simple if you just made a list of unique group names. You can export addresses from Outlook. You could then manage it.
I guess an On Error could gracefully skip missing group names. Recipients.ResolveAll might help but might show more issues if one email in the group is invalid.
So, decide how you want to handle, Outlook Missing Group Name (pf). (a) Skip without notice, (b) MsgBox notification and skip, (c) don't Send, Display, (d) etc.
It should be easy to test how a Send works if duplicate Outlook Group Names exist. I find it surprising that Outlook even allows that. Microsoft normally watches out for the user closely.
Ok, now for the main course. It was just a few tweaks to change the order of the WordEditor body. Change the final built "S" strings to suit.
I guess an On Error could gracefully skip missing group names. Recipients.ResolveAll might help but might show more issues if one email in the group is invalid.
So, decide how you want to handle, Outlook Missing Group Name (pf). (a) Skip without notice, (b) MsgBox notification and skip, (c) don't Send, Display, (d) etc.
It should be easy to test how a Send works if duplicate Outlook Group Names exist. I find it surprising that Outlook even allows that. Microsoft normally watches out for the user closely.
Ok, now for the main course. It was just a few tweaks to change the order of the WordEditor body. Change the final built "S" strings to suit.
Code:
Sub Main()
Dim T$, p$, a, e, r As Range, c As Range, pF$, S$, sig$
Dim fso As Object, wb As Workbook, ws As Worksheet
'Tools > References > Microsoft Outlook xx.0 Object Library > OK
Dim olApp As Outlook.Application, olMail As Outlook.MailItem
'Tools > References > Microsoft Word xx.0 Object Library > OK
Dim Word As Document, wr As Word.Range
'INPUTS to change if needed...........................................................
T = "ken@gmail.com"
'p = "H:\WINT-17\1-W-17-ALL ORIGINAL FROM MILL\W-17-CONFIRMATION\*.*"
p = "C:\Users\lenovo1\Dropbox\Excel\Outlook\emailPDFinvoices\*.*"
Set wb = Workbooks.Open(ThisWorkbook.Path & "\SUITING-BUYER MASTER.xlsx", False, True)
Set ws = wb.Worksheets("BUY MASTER")
'File to copy content as signature for body of email.
sig = ThisWorkbook.Path & "\sig.rtf"
'End INPUTS...........................................................................
Set fso = CreateObject("Scripting.FileSystemObject")
'Batch to get all filenames.
a = aFFs(p)
If Not IsArray(a) Then Exit Sub
'Get Outlook application
Set olApp = New Outlook.Application
'Iterate all elements in a, filenames to attach.
For Each e In a
'Get 5-digit prefix of base filename.
pF = fso.GetBaseName(e)
If Len(pF) < 5 Then GoTo NextE
pF = Left(pF, 5)
'Find matching prefix "number" in usedrange G:H
Set r = ws.Range("G1:H" & ws.Cells(ws.Rows.Count, "G").End(xlUp).Row).Find _
(Val(pF), ws.[G1], , xlWhole)
If r Is Nothing Then GoTo NextE
'Set subject value:
S = pF & "-" & ws.Cells(r.Row, "I").Value & ", (" & ws.Cells(r.Row, "J").Value & ")"
'Copy content from sig.rtf as signature for body of email to clipboard.
GetObject(sig).Range.Copy
'Make email, attach file, and send/display.
'Set olMail = olApp.CreateItem(olMailItem)
With olApp.CreateItem(olMailItem)
.Importance = olImportanceNormal
.To = pF 'T
.Subject = S
'Add copy of sig.rtf to body.
.GetInspector.Display
Set Word = .GetInspector.WordEditor
Set wr = Word.Content
wr.Collapse Direction:=wdCollapseEnd
wr.Paste
'Add body text after sig.rtf.
'Build string for body.
S = S & "," & vbCrLf & vbCrLf & _
"Your Confirmation Attached" & vbCrLf & vbCrLf & _
"Regards," & vbCrLf & "My name," & vbCrLf & _
"Firm name, & Contact detail.."
'set up for adding S at end.
wr.Collapse Direction:=wdCollapseEnd
wr = S
.Attachments.Add e
'https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.deferreddeliverytime.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
'.DeferredDeliveryTime = Now + TimeValue("00:10:00")
.Display
'.Send
End With
NextE:
Next e
TheEnd:
Set olMail = Nothing
Set olApp = Nothing
Set ws = Nothing
wb.Close False
Set wb = Nothing
End Sub