Option Explicit
Dim subject As String
Dim body As String
Dim email As String
Dim emailcc As String
Dim id As String
Dim folder As String
Dim month As String
Dim day As String
Dim fileformat As String
Dim attachment As String
Dim fileNameToFind As String
Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'If Range(Target.SubAddress).Column = 2 Then
'email = Range("D" & Range(Target.SubAddress).Row).Value
'subject = Range("I1") & Range("K1")
'body = Range("K2")
'fileformat = "*.docx"
'id = Range("H" & Range(Target.SubAddress).Row).Value
'folder = Range("E" & Range(Target.SubAddress).Row).Value
'folder = Range("E1")
'month = Range("F" & Range(Target.SubAddress).Row).Value
'month = Range("F1")
'day = Range("G" & Range(Target.SubAddress).Row).Value
'day = Range("G1")
'attachment = Dir(folder & month & day & id & "*")
fileNameToFind = Dir("C:\test\report*.docx")
Do While Len(fileNameToFind) > 0
Debug.Print fileNameToFind
fileNameToFind = Dir()
Loop
'Call Send_The_Emails
'End If
End Sub
This works
works.jpg
And inside the email script (and I removed the ' for email, subject and body from the previous code)
Sub Send_The_Emails()
'Dim emailRange As Range
Dim WordDoc As Word.Document
Dim para As Long, paraTotal As Long
Dim OApp As Object
Dim OMail As Object
'Excel range to be copied and pasted into the email body
'Set emailRange = ThisWorkbook.Worksheets("email").Range("K3:P9")
'Get active Outlook instance, if any
On Error Resume Next
Set OApp = CreateObject("Outlook.Application")
If Err.Number = 429 Then
'Not found, so create new Outlook instance
Err.Clear
Set OApp = New Outlook.Application
End If
On Error GoTo 0
'Create new email
Set OMail = OApp.CreateItem(0)
With OMail
.SentOnBehalfOfName = "me@me.com"
.To = email
.subject = subject
.Attachments.Add fileNameToFind
.Display
Set WordDoc = .GetInspector.WordEditor
paraTotal = WordDoc.Paragraphs.Count
para = 0
'Insert paragraph(s) above the Excel range
With WordDoc.Paragraphs(1)
.Range.InsertBefore "Hello," & vbCr & vbCr & _
body & vbCr & vbCr
End With
para = para + 1 + WordDoc.Paragraphs.Count - paraTotal
paraTotal = WordDoc.Paragraphs.Count
'Copy and paste Excel range into email body
'With WordDoc.Paragraphs(para)
' emailRange.Copy
'.Range.Paste 'as editable table
'.Range.PasteAndFormat Type:=wdChartPicture 'or as image
'.Range.InsertParagraphAfter
'End With
para = para + 1 + WordDoc.Paragraphs.Count - paraTotal
paraTotal = WordDoc.Paragraphs.Count
'Insert paragraph(s) below the Excel range
'With WordDoc.Paragraphs(para)
' .Range.InsertBefore Range("S3") & vbCr
'End With
'.Send 'send the email immediately
End With
Application.CutCopyMode = False
End Sub
So .Attachments.Add fileNameToFind , seems correct, yes ?
In this case, this is error message that I get :
notworking.jpg
I know I made a mistake somewhere, but I can't find it.
If I had the loop code under Send_the_emails... I get
notworking2.jpg
So, although it does find the files, I still get an error. Outlook does not even open whenever I get an error.