PDA

View Full Version : [SOLVED:] Outlook, new message composed, then add a specific file



wdg1
05-17-2020, 04:53 AM
When I composed an outgoing email message, THEN I want ta add a specific file (C:\path\file1.pdf) to this message.
Ho do I solve this issue?
Most of the VBA codes uses a new message + file. I want to add 1 fixed file.

gmayor
05-17-2020, 08:04 PM
The following will add the named file to the currently open message

Sub AddAttachment()
'Graham Mayor - https://www.gmayor.com - Last updated - 18 May 2020
Const strPath As String = "C:\path\file1.pdf"
Dim olMsg As MailItem
On Error Resume Next
Select Case Outlook.Application.ActiveWindow.Class
Case olInspector
Set olMsg = ActiveInspector.currentItem
Case Else
End Select
If TypeName(olMsg) = "MailItem" Then
If FileExists(strPath) Then
olMsg.Attachments.Add strPath
Else
Beep
MsgBox "Attachment file not forund"
End If
Else
Beep
MsgBox "No mail item selected"
End If
lbl_Exit:
Exit Sub
End Sub

Private Function FileExists(strFullName As String) As Boolean
'Graham Mayor
'strFullName is the name with path of the file to check
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(strFullName) Then
FileExists = True
Else
FileExists = False
End If
lbl_Exit:
Set FSO = Nothing
Exit Function
End Function