+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Select folder from an Input box and insert .PDF files from that folder

    I have never used VBA for Outlook, and was very surprised to see it did not have a Macro recorder.

    Would anybody have a code that uses an InputBox, to enter a Job Number, the Job Number is also the same name as the folder name, and it is also the same name as the .PDF file required to be inserted into the message.
    Last edited by davesexcel; 12-12-2009 at 02:47 PM.
    Dave


  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Select folder from an Input box and insert .PDF files from that folder

    Not entirely sure I know what you want but perhaps along the lines of below ?

    Code:
    Sub CreatePDFMail()
    Dim OLMsg As MailItem, vJobNo As Variant
    vJobNo = InputBox("Enter Job Number", 1234)
    Set OLMsg = CreateItem(olMailItem)
    With OLMsg
        On Error Resume Next
        .Attachments.Add "C:\yourfilepath\" & vJobNo & "\" & vJobNo & ".pdf"
        On Error GoTo 0
        If .Attachments.Count = 0 Then
            If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then
                .Delete
                GoTo ExitPoint
            End If
        End If
        .Display
    End With
    ExitPoint:
    Set OLMsg = Nothing
    End Sub
    obviously the above can be "beefed up" to do more than the above but it might be heading in the right direction if nothing else ?

    For good resource on OL VBA see link in my sig.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Re: Select folder from an Input box and insert .PDF files from that folder

    I will take the code to where I work, and see what happens,
    Thanks.
    Dave


  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Re: Select folder from an Input box and insert .PDF files from that folder

    Thanks it works just as you said it would.
    Dave


  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Re: Select folder from an Input box and insert .PDF files from that folder

    Apparently, the Outlook forum is no longer accepting new threads so I will continue with this one.
    With DonkeyOte's reply I was able to get one issue working.

    I then made a Userform, found out I had to make it Modal=false or it would not work.

    In another folder with the same name as "vJobNo", in another directory, I want to attach all .pdf files to the same e-mail.

    Unfortunately, the error says that it does not recognize the FileSearch Application.
    Code:
    Private Sub CommandButton1_Click()
        Dim OLMsg As MailItem, vJobNo As Variant
        Dim FileSearch As Application    'As Outlook.Application
        Set FileSearch = New Outlook.Application
            vJobNo = ID_Number
            Set OLMsg = CreateItem(olMailItem)
    
    
            With OLMsg
                On Error Resume Next
                .Attachments.Add "C:\Excel\" & vJobNo & "\" & vJobNo & ".pdf"
                On Error GoTo 0
                If .Attachments.Count = 0 Then
                    If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then
                        .Delete
                        GoTo ExitPoint
                    End If
                End If
                'this next bit of code is to search another folder and attach all the .pdf files from that folder
                With Application.FileSearch
                    .NewSearch
                    .LookIn = "C:\TestMail\" & vJobNo & "\"
                    .SearchSubFolders = True
                    .FileName = "*.pdf"
                    .Execute
                    filesToProcess = .FoundFiles.Count
                    For i = 1 To .FoundFiles.Count
                        PdfFound = .FoundFiles(i)
                        .Attachments.Add
                    Next i
                End With
    
                '--------------------------------------------
                .To = "someone@somewhere.com"
                .Subject = "This is the PDF Files for , " & Address & " ,Job# " & JobNumber
                .Display
            End With
    ExitPoint:
            Set OLMsg = Nothing
            Unload Me
        End Sub
    Last edited by DonkeyOte; 12-12-2009 at 08:39 AM. Reason: revised address...
    Dave


  6. #6
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Select folder from an Input box and insert .PDF files from that folder

    UF aside (I don't have one setup) in basic terms would the below work for you ?

    Code:
    Dim vDir As Variant, strPath As String
    ....
    With OLMsg
        On Error Resume Next
        .Attachments.Add "C:\Excel\" & vJobNo & "\" & vJobNo & ".pdf"
        On Error GoTo ExitPoint
        strPath = "C:\TestMail\" & vJobNo & "\*.pdf"
        vDir = Dir(strPath)
        Do While Len(vDir) > 0
            .Attachments.Add Replace(strPath, "*.pdf", vDir)
            vDir = Dir()
        Loop
        If .Attachments.Count = 0 Then
            If MsgBox("Invalid Path - Cancel Mail ?", vbYesNo, "Cancel") = vbYes Then
                .Delete
                GoTo ExitPoint
            End If
        End If
        .To = "someone@somewhere.com"
        .Subject = "This is the PDF Files for , " & Address & " ,Job# " & vJobNo
        .Display
    End With

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Re: Select folder from an Input box and insert .PDF files from that folder

    It certainly does work, thanks, and thanks for Editing the original code.
    Dave


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0