Log in

View Full Version : Read the Subject-Feld and do action depends on the subject



memo_star
09-05-2015, 08:05 AM
Hello everyone,

at the work i must send almost 60 pdfs a day in 6 emails ... that means in every Email 10 PDF-Files.

i thought i could spare time when i write in the subject how to finde the 10 Pdfs and then the PDFs will be automatically attached to the Email that i am about to send.

Example :

My Subject is going to look like this

Subject: 01-02-03-04-05-06-07-08-09-10
and that would mean there is a pdf that called 01 in a certain path that i have to set in the vba code like C:\bla\bla1\01.pdf and then the same for 02.pdf ...etc

is that possible?

Thanks a lot in advance :)

PS: I could also use the Pdfs name ( 01, 02, 03..) also as an Excel cells that can be read and then all these cells as a subject for the email plus the pdfs attach

gmayor
09-05-2015, 10:06 PM
It is possible. Add the following code to the Outlook VBA module 'ThisOutlookSession'

Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Const strPath As String = "C:\Path\ 'The path where the pdf files are stored"
Dim vSubject As Variant
Dim i As Long
If Len(Item.Subject) - Len(Replace(Item.Subject, "-", "")) > 1 Then
vSubject = Split(Item.Subject, "-")
For i = LBound(vSubject) To UBound(vSubject)
If FileExists(strPath & Trim(vSubject(i)) & ".pdf") Then
Item.Attachments.Add strPath & Trim(vSubject(i)) & ".pdf"
End If
Next i
End If
lbl_Exit:
Exit Sub
End Sub

Private Function FileExists(filespec) As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filespec) Then
FileExists = True
Else
FileExists = False
End If
lbl_Exit:
Exit Function
End Function



If you then create a message with a subject that looks like 01-03-04, then provided there is at least one hyphen (and that if you have additional text it is separated from the numbers by hyphens e.g. 'Leading Text -01-03-04- Trailing text') then the subject is split at the hyphens and if there are matching PDF names to the split numbers/texts, they are added to the message when you click Send. If the PDF names don't match the numbers, you would have to use conditional statements to cross reference the names with the numbers, but this should give you the bones of a process.

14326