I have never used VBA for Outlook, and was very surprised to see it did not have a Macro recorder.
Would anybody have a code that uses an InputBox, to enter a Job Number, the Job Number is also the same name as the folder name, and it is also the same name as the .PDF file required to be inserted into the message.
Not entirely sure I know what you want but perhaps along the lines of below ?
obviously the above can be "beefed up" to do more than the above but it might be heading in the right direction if nothing else ?Code:Sub CreatePDFMail() Dim OLMsg As MailItem, vJobNo As Variant vJobNo = InputBox("Enter Job Number", 1234) Set OLMsg = CreateItem(olMailItem) With OLMsg On Error Resume Next .Attachments.Add "C:\yourfilepath\" & vJobNo & "\" & vJobNo & ".pdf" On Error GoTo 0 If .Attachments.Count = 0 Then If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then .Delete GoTo ExitPoint End If End If .Display End With ExitPoint: Set OLMsg = Nothing End Sub
For good resource on OL VBA see link in my sig.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Apparently, the Outlook forum is no longer accepting new threads so I will continue with this one.
With DonkeyOte's reply I was able to get one issue working.
I then made a Userform, found out I had to make it Modal=false or it would not work.
In another folder with the same name as "vJobNo", in another directory, I want to attach all .pdf files to the same e-mail.
Unfortunately, the error says that it does not recognize the FileSearch Application.Code:Private Sub CommandButton1_Click() Dim OLMsg As MailItem, vJobNo As Variant Dim FileSearch As Application 'As Outlook.Application Set FileSearch = New Outlook.Application vJobNo = ID_Number Set OLMsg = CreateItem(olMailItem) With OLMsg On Error Resume Next .Attachments.Add "C:\Excel\" & vJobNo & "\" & vJobNo & ".pdf" On Error GoTo 0 If .Attachments.Count = 0 Then If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then .Delete GoTo ExitPoint End If End If 'this next bit of code is to search another folder and attach all the .pdf files from that folder With Application.FileSearch .NewSearch .LookIn = "C:\TestMail\" & vJobNo & "\" .SearchSubFolders = True .FileName = "*.pdf" .Execute filesToProcess = .FoundFiles.Count For i = 1 To .FoundFiles.Count PdfFound = .FoundFiles(i) .Attachments.Add Next i End With '-------------------------------------------- .To = "someone@somewhere.com" .Subject = "This is the PDF Files for , " & Address & " ,Job# " & JobNumber .Display End With ExitPoint: Set OLMsg = Nothing Unload Me End Sub
UF aside (I don't have one setup) in basic terms would the below work for you ?
Code:Dim vDir As Variant, strPath As String .... With OLMsg On Error Resume Next .Attachments.Add "C:\Excel\" & vJobNo & "\" & vJobNo & ".pdf" On Error GoTo ExitPoint strPath = "C:\TestMail\" & vJobNo & "\*.pdf" vDir = Dir(strPath) Do While Len(vDir) > 0 .Attachments.Add Replace(strPath, "*.pdf", vDir) vDir = Dir() Loop If .Attachments.Count = 0 Then If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then .Delete GoTo ExitPoint End If End If .To = "someone@somewhere.com" .Subject = "This is the PDF Files for , " & Address & " ,Job# " & vJobNo .Display End With
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks