Consulting

Results 1 to 4 of 4

Thread: Using Excel macro to attach files in Outlook (without using full file name)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Using Excel macro to attach files in Outlook (without using full file name)

    Hello,
    I'm working on a macro that should generate a multitude of emails each with a specific subject, BCC group, and attachments. My current macro requires the full file name for each attachment. I'm looking for a solution where the macro will search a specific folder, then grab and attach the file with only the first key words (since the name includes effective dates which change each month). Is there any way to program the Attachments.Add piece to grab a file from the first keywords (essentially having it search for Z1 DLVD (60-9) and pulling the file)?

    Thank you in advance

    Sub UMW_Pricelist_Distribution()
    Dim emailApplication As Object
    Dim emailItem As Object




    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)
    ' Now build the email
    emailItem.BCC = Worksheets("Pricelist").Range("G3").Value
    emailItem.Subject = "Pricelist"
    emailItem.Body = "Have a great weekend!"
    ' Attach any file from your computer. Use the full length location and name
    emailItem.Attachments.Add ("F:\Pricing-Retail\Monthly\UMW\Current Month\BAXTER\Z1 DLVD (60-9) Effective 7-1 to 8-1.pdf")
    ' Send or display the email. Use .Send or .Display, NEVER use both
    emailItem.Display

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,246
    Use Dir + joker characters.

  3. #3

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Posts
    7,985
    Location
    Only finds the first one

    Returns empty string if none found


    Option Explicit
    
    
    Sub drv()
    
    
        'prefix + star + ext
        MsgBox GetPathFromPartial("d:\testing", "Z1 DLVD (60-9)*.xlsm")
    
    
    End Sub
    
    
    
    
    Function GetPathFromPartial(P As String, F As String) As String
        If Right(P, 1) <> Application.PathSeparator Then P = P & Application.PathSeparator
    
    
        GetPathFromPartial = Dir(P & F)
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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