Consulting

Results 1 to 8 of 8

Thread: Information on the outlook attachment.

  1. #1
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post Information on the outlook attachment.

    Hi Experts

    I would like to know is it possible to check the name within the attached document and match it with the name available in the body of the mail.

    In case if the mismatch occurs then the outlook should throw a pop-up msg that the name in the attached doc is not matching with the name available in the body of the mail. Ideally the name in the body would be the person name whom we address the mail.

    Ex - Dear Jagdev

    Please let me know if it is possible or not.

    Regards,
    JD

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    Thanks for sharing the above information. The above question just cameup in my mind last week. I was thinking is it possible for the outlook to check the name in the body (addressing person) and the same name in the attached PDF to avoid sending incorrect file to the customer.

    Could you please elobrate the above code like what to do here and where to add the above code.

    Happy New Year!

    Regards,
    JD

  4. #4
    You didn't say the attachment was a PDF, but a 'document'. The macro will work with Word documents, but you can't open a PDF in any Word version before 2013, and frankly I am not sure whether it would work, but if you have Word 2013 it might be worth trying it. At the very least you would have to change the line

    If olAttach.Filename Like "*.docx" Then
    to

    If olAttach.Filename Like "*.pdf" Then
    Word 2013 uses a form of OCR to convert the PDF to document (whch takes some time) and there will be a warning message relating to the conversion. It is not going to work if the PDF is of a graphic as that will not contain editable text.

    Beyond that select the message and run the macro 'ProcessMessage'. The code all goes in a single Outlook VBA module and assumes that the message has the line 'Dear [Name]'
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    Sorry for the confusion. We ideally sent PDF version of the document. The MS office version we are using is 2010 and not 2013. Please let me know if we can do anything with 2010 version to fullfill the above need.

    Regards,
    JD

  6. #6
    You could update it to 2013, or send Word documents! Word 2010 cannot open PDF files.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Graham

    Thanks for the above information. I will check with my colleague if it is feasible to update the MS office version.

    Regards,
    JD

  8. #8
    I was being a bit flippant. I think that updating to 2013 might be a frying pan to fire approach to the issue. I would address it by ensuring that the correct attachment was attached in the first place thus negating the requirement for complex, and possibly unreliable, checking after the fact.

    Why not create the message from the process that creates the PDF and then attach it?. That way there would be no need to additionally validate it.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •