VBA Outlook mail attachment

PhysicsGeek2022

New Member
Joined
Nov 11, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi All:

I am creating a spreadsheet that will send automatic email reminders to clients using a VBA Macros code. The code uses outlook. I have created the code and spreadsheet to do everything I'd like except attach individual files and keep my signature in my outlook e-mail. I know it can be done (attach files) as I have attached files this way in a previous version of the code. However, now with this new advanced code I can't get the files to attach. If I add the Attachment.Add function, no e-mail are sent at all. If I take the Attachment.Add function out then my e-mails send fine but without the attachment. I'm hoping someone can help me with this. Below is my code. Thank you!



Public Sub Send_Email_Automatically2()
Dim rngD, rngS, rngT, rngU As Range
Dim ob1, ob2 As Object
Dim LRow, x As Long
Dim l, strbody, rSendValue, mSub As String
On Error Resume Next
Set rngD = Range("P2", Range("p2").End(xlDown))
If rngD Is Nothing Then Exit Sub
Set rngS = Range("I2", Range("i2").End(xlDown))
If rngS Is Nothing Then Exit Sub
Set rngT = Range("S2", Range("s2").End(xlDown))
If rngT Is Nothing Then Exit Sub
Set rngU = Range("U2", Range("u2").End(xlDown))
If rngU Is Nothing Then Exit Sub
strFolder = "X:\Nuclear Medicine\NEW LEAD APRON EXCEL REPORTS\New Lead Apron Logs 11.2022"
LRow = rngD.Rows.Count
Set rngD = rngD(1)
Set rngS = rngS(1)
Set rngT = rngT(1)
Set rngU = rngU(1)
Set ob1 = CreateObject("Outlook.Application")
For x = 1 To LRow
rngDValue = ""
rngDValue = rngD.Offset(x - 1).Value
If rngDValue <> "" Then
If CDate(rngDValue) - Date < 0 Then
rngSValue = rngS.Offset(x - 1).Value
mSub = rngT.Offset(0, -1).Value
l = ""
strbody = ""
strbody = strbody & rngT.Offset(x - 1).Value & l
strbody = strbody
Set ob2 = ob1.CreateItem(0)
With ob2
.Subject = mSub
.To = rngS
.Body = strbody
'.Attachments.Add strFolder & "\" & rngU
.display
End With
Set ob2 = Nothing
End If
End If
Next
Set ob1 = Nothing
End Sub
 
I tried that as well as that is what I thought it should be but I get the same error message. I tried it a bunch of different ways.
Maybe it is because I am working on my network from home. I'll try it when I go in to the office tomorrow and see if I get a different outcome unless you have any other suggestions.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You mean that drive X:\ is available both from home and from the office?
 
Upvote 0
You mean that drive X:\ is available both from home and from the office?
I can get into the files on the X: Drive but it doesn't appear as an option from my PC when I go into the documents on my computer. When I do click on the link to the document in my spreadsheet I get the following message before I can access the file. So maybe there is some sort of firewall or something? I work in healthcare so it may work tomorrow in the office.

1668368269590.png
 
Upvote 0
Try from the office, and be sure that those files are mapped on the "X:" drive
 
Upvote 0
Try from the office, and be sure that those files are mapped on the "X:" drive
Thanks. I'll let you know tomorrow afternoon. I know for sure they are mapped on the X: drive.

BTW. Do you know why my outlook signature disappears? It's not that big of a deal but I thought it might be an easy fix.
 
Upvote 0
For the signature you have to use the html format. I thought you was using l = "SomeTextHere" for the signature:
VBA Code:
l = vbCrLf & "Signature line #1" & vbCrLf & "Signature line #2"
strbody = ""                                                        'This is useless
strbody = strbody & rngT.Offset(x - 1).Value & l                    'This append the signature
strbody = strbody                                                   'This is useless

Please use tags to make your code more readable: select the code, press the VBA icon
 
Upvote 0
For the signature you have to use the html format. I thought you was using l = "SomeTextHere" for the signature:
VBA Code:
l = vbCrLf & "Signature line #1" & vbCrLf & "Signature line #2"
strbody = ""                                                        'This is useless
strbody = strbody & rngT.Offset(x - 1).Value & l                    'This append the signature
strbody = strbody                                                   'This is useless

Please use tags to make your code more readable: select the code, press the VBA icon
 
Upvote 0
Ok thank you. I'm a novice at this. I'm teaching myself. This needs to get done and surprisingly there is no one in my network that can do it. I've gotten myself this far before I got stuck and need to reach out for help. So I really appreciate you.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top