Referencing a filepath using a cell for email attachments

k10riley

New Member
Joined
Dec 1, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
In a previous thread we determined how to use a cell as a reference for a filepath rather than hardcoding the filepath. I wanted to add this same concept to another macro.
In this macro, the code attaches specific workbooks to specific individuals, with the workbooks being pulled from the file path. I referenced the cell as I did in my other code, but it is only attaching one workbook to an email and not attaching the others. I have checked the file path etc.

Note, I commented out the original code that contains the hardcoded file path (this code works). I appreciate any input!
I also did some testing with adding a *\* but that makes the emails not attach anything, without it I am able to attach one.

VBA Code:
Sub AttachmentEmails()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim SigString As String
Dim Signature As String
Dim count, i As Integer
Dim attachPath As String
Dim attachDoc As String

'set location of saved Outlook signature
'note it requires the desired signature to be labaled "New"
SigString = Environ("appdata") & "\Microsoft\Signatures\New.htm"

'use custom function (detailed below) to fix image paths in the htm file
Signature = FixHtmlBody(SigString)

'open tracking sheet
Sheets("2021 Email + Tracking").Activate

'determine length of loop
'function counts the number of non-blank rows (starting with header currently in row 7) and adds number of prior rows (currently 6) to get the appropriate final row number
'to customize: replace B7 reference with cell starting the relevant table and replace 6 with the appropriate number of rows (before table header)
count = WorksheetFunction.CountA(Range("B7", Range("B7").End(xlDown))) + 6

'set starting point for loop (should be first row with a name)
i = 8

'start of loop
Do While i <= count

'call on Outlook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

'determine email body
'to customize: enter in desired font and size
    'replace "2021 Email + Tracking" with appropriate sheet name (in all locations)
    'replace 15 with column number (A = 1, B = 2, etc.) for First Name
    'replace 16 with column number for Paragraph 1
    'replace 17 with column number for Due Date
    'replace 18 with column number for Paragraph 2
    'update static text as appropriate using HTML for customizing font presentation or adding links or line spaces.'
    'each line must end with "& _" to signify that you have additional text to write. References to Excel cells should be outside of quotes and separated by & to concatenate.
strbody = "<font face =""Calibri"" style = ""font-size:11pt;"">" & _
            "Hi " & Sheets("2021 Email + Tracking").Cells(i, 15) & ",<br><br>" & _
            Sheets("2021 Email + Tracking").Cells(i, 16) & _
            " We ask that you assist us by completing the attached spreadsheet on behalf of you and your direct reports by <font color = ""red""><b>" & Format(Sheets("2021 Email + Tracking").Cells(i, 17), "[$-x-sysdate]dddd, mmmm dd, yyyy") & "</b></font> to ensure that the Model Inventory remains accurate and complete.<br><br>" & _
            Sheets("2021 Email + Tracking").Cells(i, 18) & "<br><br>" & _
            "Please do not hesitate to reach out with any questions. I am also happy to set up a call to further discuss what we are trying to accomplish with this exercise, or provide more information about the Model Risk Management program in general.<br><br>" & _
            "Thank you in advance for your cooperation! <br>"

*******************************************************
'this is the section with the problematic code, the original is not commented out'
'define attachment
'replace 14 with column number of file name
attachPath = "C:\2021 Initial Surveys"
attachDoc = attachPath & "\" & Sheets("2021 Email + Tracking").Cells(i, 14)

'new code test (only attaches one email does not work)
'attachPath = Range("C9").Value
'attachDoc = attachPath & Sheets("2021 Email + Tracking").Cells(i, 14)

'end section'
************************************
On Error Resume Next
    With OutMail
    'to customize: replace 8 with column corresponding to Scenario, adjust the IF statement to capture desired scenarios/batch;
    '__replace 9 with column corresponding to Batch and update "Cover" and Cells(12,3) as necessary.
    'replace 10 with column number for To Address
    'replace 12 with column number for CC Address
    'replace 13 with column number for Subject
    If Sheets("2021 Email + Tracking").Cells(i, 8).Value > 0 And Sheets("2021 Email + Tracking").Cells(i, 9).Value = Sheets("Cover").Cells(12, 3).Value Then
        .Display
        'send to Person 1
        .To = Sheets("2021 Email + Tracking").Cells(i, 10)
        .CC = Sheets("2021 Email + Tracking").Cells(i, 12)
        .Subject = Sheets("2021 Email + Tracking").Cells(i, 13)
        .Attachments.Add attachDoc
        .HTMLBody = "<html><body>" & strbody & "<br>" & Signature
    Else
    End If
    End With
    
    On Error GoTo 0
    
Set OutMail = Nothing
Set OutApp = Nothing

i = i + 1

Loop

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
'attachPath = Range("C9").Value

On which sheet do you have the folder?

Try this:

VBA Code:
    attachPath = Sheets("Cover").Range("C9").Value   'Which sheet? I guess from the sheet "Cover"
    If Right(attachPath, 1) <> "\" Then attachPath = attachPath & "\"
    attachDoc = attachPath & sh.Range("N" & i).Value

I also recommend the following:

- Eliminate excess comments in the code.
- Use Range instead of Cells to identify the letter of the column.
- Eliminate On Error to test and identify what the error could be.
- Set the sheet to use in an object.

Try the following code:

VBA Code:
Sub AttachmentEmails()
  Dim OutApp As Object, OutMail As Object
  Dim strbody As String, SigString As String, Signature As String
  Dim attachPath As String, attachDoc As String
  Dim i As Long
  Dim sh As Worksheet
  
  'set location of saved Outlook signature.'note it requires the desired signature to be labaled "New"
  SigString = Environ("appdata") & "\Microsoft\Signatures\New.htm"
  Signature = FixHtmlBody(SigString) 'use custom function (detailed below) to fix image paths in the htm file
  
  'open tracking sheet
  Set sh = Sheets("2021 Email + Tracking")
  
  For i = 8 To sh.Range("B" & Rows.count).End(3).Row 'start of loop
    Set OutApp = CreateObject("Outlook.Application")  'call on Outlook
    Set OutMail = OutApp.CreateItem(0)
    
    strbody = "<font face =""Calibri"" style = ""font-size:11pt;"">" & _
      "Hi " & sh.Cells(i, 15) & ",<br><br>" & sh.Cells(i, 16) & _
      " We ask that you assist us by completing the attached spreadsheet on behalf of you and your direct reports by " & _
      "<font color = ""red""><b>" & Format(sh.Cells(i, 17), "[$-x-sysdate]dddd, mmmm dd, yyyy") & "</b></font> " & _
      "to ensure that the Model Inventory remains accurate and complete.<br><br>" & _
      sh.Cells(i, 18) & "<br><br>" & _
      "Please do not hesitate to reach out with any questions. " & _
      "I am also happy to set up a call to further discuss what we are trying to " & _
      "accomplish with this exercise, or provide more information about the Model Risk Management program in general.<br><br>" & _
      "Thank you in advance for your cooperation! <br>"
    
    'new code test (only attaches one email does not work)
    attachPath = Sheets("Cover").Range("C9").Value   'Which sheet? I guess from the sheet "Cover"
    If Right(attachPath, 1) <> "\" Then attachPath = attachPath & "\"
    attachDoc = attachPath & sh.Range("N" & i).Value
    
    'On Error Resume Next
    With OutMail
      If sh.Range("H" & i).Value > 0 And sh.Range("I" & i).Value = Sheets("Cover").Range("C12").Value Then
        .to = sh.Range("J" & i).Value
        .CC = sh.Range("L" & i).Value
        .Subject = sh.Range("M" & i).Value
        .Attachments.Add attachDoc
        .HTMLBody = "<html><body>" & strbody & "<br>" & Signature
        .Display
        'send to Person 1
      End If
    End With
    'On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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