Search code examples
vbaexcel-2016ms-access-2016outlook-2016

My attachments lose their original name and are show as ProjectStatus.xlsx


In windows 7 and Office 2007 I have been using a code which opens a new email in Outlook, attach a file and send it. The code it's not mine, I found it somewhere in the internet. The problem is that now I use Windows 10 and Office 2016, and using the same code produce different results as:

  • The original name of the file, let's say for example "Products.xlsx", is changed to "ProjectStatus.xlsx" (any file name is always changed to "ProjectStatus.xlsx")
  • If I open the file then Excel opens it and shows the original name of the file ("Products.xlsx")
  • If I send it, sometimes the recipients see the attached file as "ProjectStatus.xlsx" and sometimes see it as "Products.xlsx". But what always happens is that if they open the file, in excel is seen as "Products.xlsx"

I need the file name always be shown with the original name. How can I do this?

This is the code I use it and is executed from both access 2016 and excel 2016.

Sub MandaMailA(destinatarios As String, copia As String, subject As String, strbody As String, attachment1 As String, Optional attachment2 As String = "", Optional CO As String = "")

    Dim OutApp As Object
    Dim OutMail As Object
    Dim SigString As String
    Dim Signature As String

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



    'Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Firmas\VBA.htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    On Error Resume Next

    With OutMail
        .To = destinatarios
        .CC = copia
        .BCC = CO
        .subject = subject
        .HTMLBody = strbody & "<br>" & Signature
        .Display    'or use .Display
        .Attachments.Add attachment1, olByValue, 1, "ProjectStatus"
        .Attachments.Add attachment2, olByValue, 1, "ProjectStatus"

    End With

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

I notice that this code includes the word "ProjectStatus" but honestly I have not a deep knowledge of VBA.

Thanks in advance!!


Solution

  • A simple read of the Attachments.Add documentation is all you need, specifically the section on the optional DisplayName parameter:

    This parameter applies only if the mail item is in Rich Text format and Type is set to olByValue : the name is displayed in an Inspector object for the attachment or when viewing the properties of the attachment. If the mail item is in Plain Text or HTML format, then the attachment is displayed using the file name in the Source parameter.

    So if you always want to always use the original file name, simply delete the instances of , "ProjectStatus".