Outlook: Attach a file with a dynamic name - vba

Argh, Object doesn't support this property or method error 438!
I haven't tried this but, I think it might work:
objFS.System.IO.Path.GetFileName(fileName)
if objFS.System.IO.Path.GetFileName(fileName) = "VS12_WID1" Then
fileName = AFile.Name
getFileName = filePath & "/" & fileName
I should simplfy it more then try to rename the file.
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFile, fileName, filePath As String
Set filePath = "F:\"
Set fileName = "V_W_*_*_.pdf"
Set getFile = "filePath" & "fileName"
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
With myItem
.To = "email#mail.com"
.CC = ""
.Subject = "test"
myAttachments.Add getFile
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub
I'm getting a compile error: Object required highlighting both Sub AddAttachment() and Set filePath. I feel so close to making this work!
UPDATED CODE:
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFileName, fileName, filePath As String
Dim objFS: Set objFS = CreateObject("Scripting.FileSystemObject")
Set filePath = "F:\"
Set getFileName = filePath & fileName
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
For Each fileName In filePath
If fcase(objFS.GetExtensionName(fileName)) = "VS111111_WID111A" Then
fileName = "VS111111_WID111A.pdf"
Exit For
End If
Next
With myItem
.To = "email#mail.com"
.CC = ""
.Subject = ""
myAttachments.Add getFileName
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub
I have enough knowledge to read the script to understand what is going on. The code I made can only find a fixed file name. How can the file name be made dynamic?
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
With myItem
.To = "email#address.com"
.CC = "email#address.com"
.Subject = ""
myAttachments.Add "F:\constantFilenameHas8char_constantFilenameHas7char_variableHas5Int_todaysModifiedDate_variableHas6Int.pdf"
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub

Your query is not clear on how you want to get the filename.Think of using
a variable and pass the filepath and name as you required.
dim FileToAttach as string
FileToAttach ="FilePath" & "Filename"
myAttachments.Add FileToAttach
For your updated code
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFileName, filename
Dim filePath As Object
Dim objFS As FileSystemObject
Set objFS = New FileSystemObject
Set filePath = objFS.GetFolder("C:\Users\Dinesh\Desktop\")
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
For Each AFile In filePath.Files
Debug.Print UCase(objFS.GetExtensionName(fileName))
If UCase(objFS.GetExtensionName(AFile)) = "PDF" Then
fileName = AFile.Name
getFileName = filePath & "/" & fileName
Exit For
End If
Next
With myItem
.To = ""
.CC = ""
.Subject = ""
myAttachments.Add getFileName
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
.Display
End With
'myItem.Send
End Sub

Related

vba outlook adding newline between content and signature

Hi trying to add a newline between my body content after paste a table and signature,codes are below:
dim FileName As String
Dim filepath As String
Dim rng As Range
Dim OutlookApp As Object
Dim Outlookmail As Object
Dim lastrowo As Integer
Application.ScreenUpdating = False
Set OutlookApp = CreateObject("Outlook.Application")
Set Outlookmail = OutlookApp.CreateItem(0)
lastrowo = Worksheets("Price And Accrued Info").Range("K550").End(xlUp).row
Set rng = Worksheets("Price And Accrued Info").Range("K2:y" & lastrowo)
rng.Copy
Dim vInspector As Object
Set vInspector = Outlookmail.GetInspector
Dim wEditor As Object
Set wEditor = vInspector.WordEditor
With Outlookmail
.To = ""
.cc=""
.Subject = "UNCONFIRMED TRADES AS OF " & Format$(Date, "YYYY.MM.DD")
wEditor.Paragraphs(1).Range.Text = "Hi The following trades are unconfirmed trades."
wEditor.Paragraphs(2).Range.Paste
wEditor.Paragraphs(4).Range.Text = vbNewLine & "<br>"
.display
' .attachments.Add drWorkbook.FullName
' .attachments.Add crWorkbook.FullName
'
End With
Set Outlookmail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
Try this:
With Outlookmail
.To = ""
.cc = ""
.Subject = "UNCONFIRMED TRADES AS OF " & Format$(Date, "YYYY.MM.DD")
wEditor.Paragraphs(1).Range.Text = "Hi The following trades are unconfirmed trades." _
& String(5, vbNewLine)
wEditor.Paragraphs(5).Range.Text = "This is is the last line." _
& vbNewLine & vbNewLine
wEditor.Paragraphs(3).Range.Paste
.display
End With

Change new mail subject line to name of the attached file

So far I have been able to attach the file programmatically with this:
Private Sub AttachmentFile()
Dim oLook As Object
Dim oMail As Object
Dim FD As Object
Dim vrtSelectedItem As Variant
Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
Set FD = Excel.Application.FileDialog(3)
With oMail
FD.AllowMultiSelect = True
FD.Filters.Clear
FD.Filters.Add "All Files", "*.*"
FD.InitialFileName = "\\ad\dfs\Shared Data\"
If FD.Show = True Then
For Each vrtSelectedItem In FD.SelectedItems
.Attachments.Add vrtSelectedItem
Next
End If
.Display
End With
Set FD = Nothing
Set oMail = Nothing
Set oLook = Nothing
End Sub
Now that the email is created and I can select the file from a specified folder, I am trying to have the change the mail item's subject to the name of the attached file, also replacing the _ (underscore) with a | (pipe symbol).
Here is the now Working code I have. I also added a signature to the email with correct fonts and image of my default signature in outlook.
Private Sub SubNBodyNTo()
Dim myInspector As Outlook.Inspector
Dim MItem As MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set MItem = myInspector.CurrentItem
Set myAttachments = MItem.Attachments
MItem.Subject = myAttachments.Item(1).DisplayName
MItem.Subject = Replace(MItem.Subject, "_", " | ")
MItem..HTMLBody = MItem.Subject & " Is attached for your approval." & "<br>" & MItem.HTMLBody
MItem.To = "person#email.com"
MItem.CC = "OtherPeople#email.com"
End If
End If
End Sub
Private Sub SubNBodyNTo()
Dim myInspector As Outlook.Inspector
Dim MItem As MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set MItem = myInspector.CurrentItem
Set myAttachments = MItem.Attachments
MItem.Subject = myAttachments.Item(1).DisplayName
MItem.Subject = Replace(MItem.Subject, "_", " | ")
MItem..HTMLBody = MItem.Subject & " Is attached for your approval."
& "<br>" & MItem.HTMLBody
MItem.To = "person#email.com"
MItem.CC = "OtherPeople#email.com"
End If
End If
End Sub

Difficulties sending worksheet through email via SendWorksheet button

I have an Excel 2016 worksheet with a "Send Worksheet" button purposed to email the worksheet to all the designated recipients. When I run the following code (most of which came from another program and tweaked), I receive the following errors:
Runtime Error 429: ActiveX component can't create object.
at Set OutlookApp = CreateObject("Outlook.Application")
as well as
Runtime Error 91: Object variable or With block variable not set.
in the With block at .To = "email address".
Option Explicit
Private Sub cmdSendWorksheet_Click()
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
'On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbook
End If
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "Worksheet Attached"
.Body = "Please see attached worksheet"
.cmdSendWorksheet.Enabled = True
.Attachments.Add Wb2.FullName
.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
this code should do the job you need. But you need to go in Tools / References and check the following reference :
Microsoft Scripting Runtime
Microsoft Outlook 14.0 Object Library
Private Sub cmdSendWorksheet_Click()
Dim Wb As Workbook
Dim FilePath As String
Dim FileName As String
Dim FileExtensionName As String
Dim FileFullPath As String
Dim OutlookApp As New Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim fso As New FileSystemObject
'On Error Resume Next
Application.ScreenUpdating = False
Set Wb = ThisWorkbook
FilePath = Environ$("temp") & "\"
FileName = fso.GetBaseName(Wb.Path & "\" & Wb.Name) & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtensionName = fso.GetExtensionName(Wb.Path & "\" & Wb.Name)
FileFullPath = FilePath & FileName & "." & FileExtensionName
fso.CopyFile Wb.Path & "\" & Wb.Name, FileFullPath
'Sending the email
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "Worksheet Attached"
.Body = "Please see attached worksheet"
.Attachments.Add FileFullPath
.Display
'.Send You can chose .Send or .Display, as you wish
End With
Kill FileFullPath
'Free the memory
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Application.Quit
End Sub

Outlook reply macro not displaying images

I have a macro that will open a reply to a selected email with a template. However, the rest of the images in the email machine now just showe a red cross.
Can anyone see why this might be happening?
Sub TacReply()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveExplorer.Selection(1)
Set replyEmail = Application.CreateItemFromTemplate("S:\Share\TWGeneral.oft")
replyEmail.To = origEmail.SenderEmailAddress
replyEmail.Subject = origEmail.Subject
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
replyEmail.SentOnBehalfOfName = "email#address.com"
replyEmail.Display
End Sub
Thanks :)
Just in case anyone has the same problem, here was the solution I used:
Sub Forward_Mail_Outlook_With_Signature_Html_2()
Dim MyItem As Object
Dim MyFwdItem As MailItem
Dim SigString As String
Dim Signature As String
Set MyItem = ActiveExplorer.Selection(1).reply
If MyItem.Class = olMail Then
Set MyFwdItem = MyItem.Forward
'Change only Mysig.htm to the name of the signature
SigString = Environ("appdata") & _
"\Microsoft\Signatures\Your Signature.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
With MyFwdItem
.To = MyFwdItem.SenderEmailAddress
.subject = MyFwdItem.subject
.HTMLBody = "<br>" & Signature & .HTMLBody
.SentOnBehalfOfName = "youremail#address.com"
.Display
End With
Else
MsgBox "Select a mailitem."
End If
ExitRoutine:
Set MyItem = Nothing
Set MyFwdItem = Nothing
End Sub
Private Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function

Batch file code for a vba program

I have a vba code which works fine, but I want to create the same code as batch file which can do the same thing the vba code is doing.
I have created the code which sends all files in a folder to a specified email address and after sending delete the file.
Can anyone help me in creating the same thing with a batch file which can do the same thing.
Below is the VBA code:
Private Sub Click()
Dim mess_body As String, StrFile As String, StrPath As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
'~~> Change path here
StrPath = "\Project\New folder\New folder\"
With MailOutLook
.BodyFormat = olFormatRichText
.To = "test#sdm.com"
.Subject = "test"
.HTMLBody = "test"
'~~> *.* for all files
StrFile = Dir(StrPath & "*.*")
Do While Len(StrFile) > 0
.Attachments.Add StrPath & StrFile
StrFile = Dir
Loop
'.DeleteAfterSubmit = True
.Send
End With
Kill "\Project\New folder\New folder\*.*"
MsgBox "Reports have been sent", vbOKOnly
End Sub
U can use cell ("A1") value as folder reference.
Dim objFolder As Object
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
'~~> Change path here
VAR1 = Range("A1").Value
If VAR1 = False Then MsgBox "Cell is empty"
If VAR1 = False Then Exit Sub
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(VAR1)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "test#test.com"
.Subject = "test"
.HTMLBody = "test"
'~~> *.* for all files
StrFile = Dir(objFolder & "*.*")
...
'.DeleteAfterSubmit = True
.Send
End With
'delete files
Kill objFolder & "\*.*"