Consulting

Results 1 to 5 of 5

Thread: Excel VBA help....please :)

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location

    Excel VBA help....please :)

    Hi, I am running this script along with a rule. When an email is received from a specified person and it contains an attachment, it will run the script. The script saves a copy of the attachment and renames it as the subject. The format of the attachment received in the email is in .XLS format and I want to keep that format. The script is saving the file to my path and renaming it as the subject but I can't open the file, I get "the file format and extension of file name don't match. The file could be corrupt or unsafe"

    Can anyone help me?

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "Y:\accounts\success fee bills"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "" & itm.Subject & ".XLS"
    Set objAtt = Nothing
    Next
    End Sub
    Last edited by Paul_Hossler; 02-23-2018 at 03:50 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Your title is very not specific about the problem

    2. You can use the [#] icon to insert CODE tags and paste the macro between them -- I did it for you this time

    3. Seems more of an Outlook question and maybe the Outlook gurus can help - let me know if you want it moved

    4. Try running this and see how Outlook wants to name the file

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
        Dim objAtt As Outlook.Attachment
        Dim saveFolder As String
    
        saveFolder = "Y:\accounts\success fee bills"
    
        For Each objAtt In itm.Attachments
            objAtt.SaveAsFile saveFolder & "" & itm.Subject & ".XLS"
    
           MsgBox saveFolder & "\" & itm.Subject & ".XLS" ' I added the \
    
        Next
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location
    thanks for your reply, I have ran this script and it no longer saves any files to my selected location. I also get a pop up which I hadn't received previously, saying Y:\accounts\success fee bills\459232.XLS

    Do you have any other suggestions? your help is very much appreciated

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. The pop up was from the MsgBox line - just to verify the file path and name

    2. Are you sure that the attachment is an Excel file. If you save it manually, can Excel open it?
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location
    Hi, yes its an excel file, XLS format. I can open the attachment and if I save and rename the attachment manually I am still able to open. I just can't work out why, if outlook is only saving and renaming the attachment and keeping XLS format why it won't open. I thought it would have been pretty straightforward but its causing me a bit of a headache!

Posting Permissions

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