Consulting

Results 1 to 12 of 12

Thread: Solved: Autosave file with SUBJECT as filename

  1. #1

    Solved: Autosave file with SUBJECT as filename

    First, thanks in advance. Either for the code or the links for help...
    Using Outlook 2010 on Exchange. I'll have scans emailed with PDF attachments. Subjects will be unique to email. I'd like to include a script in the rule to save the file using the SUBJECT of the email. After reading many sites and fiddling with code (obvious noob here), it's simply not working. See below for where I am. Laughter allowed.

    [vba]
    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim strFile As String
    Dim strFolderpath As String
    Dim strFileName As String
    Dim objSubject As String


    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File"
    objSubject = objMsg.subject
    strFileName = objSubject & ".pdf"
    strFile = strFolderpath & strFileName

    For Each objAtt In itm.Attachments

    objAtt.SaveAsFile strFile

    Set objAtt = Nothing

    Next

    End Sub[/vba]

  2. #2
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Try adding changing your path to "C:\Users\kmurphy\Documents\Dropbox\Public File\"

    I'm guessing if you looked in C:\Users\kmurphy\Documents\Dropbox there are a bunch of files with Public File appended to the name.

    Also keep in mind if there are multiple attachments it will overwrite all but the last.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  3. #3
    I was hopeful.... but no files exist in the parent either. Searched entire computer (including attached servers) for PDFs created in the past two days, and there are none. Seems that it simply isn't 'firing'? No error messages though...

  4. #4
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    How are you firing it?

    Also set option explicit to on. You have used an undeclared variable.

    [VBA]
    objSubject = objMsg.Subject
    [/VBA]

    Needs to be

    [VBA]
    objSubject = itm.Subject
    [/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  5. #5
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Just tested this with an outlook rule on my system and it worked as expected (with the correct variable).
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  6. #6
    EUREKA! LOVE it when the answer is simple! Thank you so much BrianMH!

    And at the risk of looking a gift-horse in the mouth.... Any thoughts on this:
    Expanding on the same issue above, have the code search the subject for text and then match that with a folder containing same, to save to that folder?
    For example... subject has "Black123456invoice-1" and corresponding sub-folder for attachments on HDD contains the word "Black". So code would 'see' "black" within subject and save to the folder containing the word "Black".
    If I should post this as new query to forum, please let me know.
    Either way, very grateful for your help!

  7. #7
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    NP. Make sure to update your system to always use option explicit. That makes things much easier to debug. This should do what you want. Note I changed objSubject to strSubject as it makes it easier to see the data type.
    [VBA]Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim strFile As String
    Dim strFolderpath As String
    Dim strFileName As String
    Dim strSubject As String



    strSubject = objMsg.subject
    if ucase(strSubject) like "*BLACK*" then
    strFolderPath = "C:\Black\"
    elseif ucase(strSubject) like "*BLUE*" then
    strFolderPath = "C:\Blue\"
    elseif ucase(strSubject) like "*RED*" then
    strFolderPath = "C:\Red\"
    else strFolderPath = "C:\Default\"
    end if
    strFileName = strSubject & ".pdf"
    strFile = strFolderpath & strFileName

    For Each objAtt In itm.Attachments

    objAtt.SaveAsFile strFile

    Set objAtt = Nothing

    Next

    End Sub [/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  8. #8
    FANTASTIC! Tested and works like a charm. Just one question... "UCase" seems to be having some affect. Is there a way to make this not case-sensitive? All attachments went to default folder except those whose folder name was all caps.

    Again, thank you for your time.

  9. #9
    Oh, and for reference, here's my code. Note that CFEIG is the only one that went to the appropriate folder.
    [VBA]Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim strFile As String
    Dim strFolderpath As String
    Dim strFileName As String
    Dim strSubject As String



    strSubject = itm.subject
    If UCase(strSubject) Like "*Black*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Black\"
    ElseIf UCase(strSubject) Like "*Desjarlais*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Desjarlais\"
    ElseIf UCase(strSubject) Like "*Maggart*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Maggart\"
    ElseIf UCase(strSubject) Like "*Corker*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Corker\"
    ElseIf UCase(strSubject) Like "*Congelecpac*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Congelecpac\"
    ElseIf UCase(strSubject) Like "*CFEIG*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\CFEIG\"
    ElseIf UCase(strSubject) Like "*Zelenik*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Zelenik\"
    ElseIf UCase(strSubject) Like "*Sargent*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Sargent\"
    ElseIf UCase(strSubject) Like "*Williamson*" Then
    strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Williamson\"
    Else: strFolderpath = "C:\Users\kmurphy\Desktop\RandomPoliticalScans\"
    End If
    strFileName = strSubject & ".pdf"
    strFile = strFolderpath & strFileName

    For Each objAtt In itm.Attachments

    objAtt.SaveAsFile strFile

    Set objAtt = Nothing

    Next

    End Sub[/VBA]

  10. #10
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    The ucase is there to make it not case sensitive. Make sure to put your criteria in all caps. Otherwise if files come in with different cases they won't meet the criteria.

    so
    [VBA]x = a[/VBA]
    or
    [VBA]x = A[/VBA]
    [VBA]
    if ucase(x) = "A" 'this statement is true either way above.

    if x = "A" 'this statement is only true in once of those cases.
    if x = "a" 'this statement is only true in once of those cases also.[/VBA]

    it is a good habit when dealing with strings that may have varying cases to test the string with the ucase() function and type your criteria as caps (or you could ucase() your criteria too.

    So change your criteria ie "*Desjarlais*" should be "*DESJARLAIS*"
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  11. #11
    Very instructive, and it is a thing of beauty now. Thank you!

    Cheers!

  12. #12
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Glad I could help!
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

Posting Permissions

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