Yes it is possible, but you have not supplied sufficient information. For a start, if this is to validate a mail merge, you might be better running the mail merge from a tool which will add the correct attachments in the first place e.g http://www.gmayor.com/ManyToOne.htm .
In order to do this as you envisage, you are going to have to search the message body for the required name, then open the attachment in Word, and search it for the name. The following should set you on the right path:
Option Explicit
Sub ProcessMessage()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
If CheckAttachment(olMsg) = True Then
MsgBox "Attachment name matches message"
Else
MsgBox "Attachment name does not match message"
End If
lbl_Exit:
Exit Sub
End Sub
Private Function CheckAttachment(olItem As MailItem) As Boolean
Dim olInsp As Inspector
Dim olAttach As Attachment
Dim wdApp As Object
Dim wdDoc As Object
Dim oRng As Object
Dim oFind As Object
Dim oTempDoc As Object
Dim strName As String
Dim strFname As String
Dim bFound As Boolean
Const strTempFldr As String = "C:\Path\Temp\"
CreateFolders strTempFldr
On Error GoTo Err_Handler
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
If olAttach.Filename Like "*.docx" Then
strFname = olAttach.Filename
olAttach.SaveAsFile strTempFldr & strFname
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
wdApp.Visible = True
On Error GoTo Err_Handler
With olItem
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oFind = wdDoc.Range
.Display
With oFind.Find
Do While .Execute(FindText:="Dear ")
oFind.End = oFind.Paragraphs(1).Range.End
oFind.End = oFind.End - 1
strName = oFind.Text
strName = Trim(Replace(strName, "Dear ", ""))
bFound = True
Exit Do
Loop
End With
End With
'MsgBox strName & vbCr & bFound
If Not bFound Then
GoTo CleanUp
End If
'MsgBox strTempFldr & strFname
Set oTempDoc = wdApp.Documents.Open(strTempFldr & strFname)
Set oRng = oTempDoc.Range
With oRng.Find
Do While .Execute(FindText:=strName)
CheckAttachment = True
Exit Do
Loop
End With
oTempDoc.Close 0
End If
Exit For
Next olAttach
End If
CleanUp:
Set oTempDoc = Nothing
Set wdApp = Nothing
Set olAttach = Nothing
Set olItem = Nothing
Exit Function
Err_Handler:
CheckAttachment = False
Resume CleanUp
End Function
Private Function CreateFolders(strPath As String)
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngPath = 1 To UBound(vPath)
strPath = strPath & vPath(lngPath) & "\"
If Not FolderExists(strPath) Then MkDir strPath
Next lngPath
lbl_Exit:
Exit Function
End Function
Private Function FolderExists(ByVal PathName As String) As Boolean
Dim lngAttr As Long
On Error GoTo NoFolder
lngAttr = GetAttr(PathName)
If (lngAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
NoFolder:
Exit Function
End Function