Consulting

Results 1 to 6 of 6

Thread: problems with running of auto attachments saving macro

  1. #1

    problems with running of auto attachments saving macro

    Hey all!

    I have installed and compiled a macro code with script that helps to automaticly extract attachments coming with new mail and preform a saving of the attachd file/s on a specific dir on the hard drive.
    my machine is outlook 2002, running on windows xp.

    it's goes like this:

    [VBA]Sub GetAttachments(Item As Outlook.MailItem)
    On Error GoTo GetAttachments_err

    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0


    For Each Atmt In Item.Attachments
    FileName = "DIR_PATH" & Atmt.FileName
    Atmt.SaveAsFile FileName
    i = i + 1
    Next Atmt


    GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub

    GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
    & vbCrLf & "Please note and report the following information." _
    & vbCrLf & "Macro Name: GetAttachments" _
    & vbCrLf & "Error Number: " & Err.Number _
    & vbCrLf & "Error Description: " & Err.Description _
    , vbCritical, "Error!"
    Resume GetAttachments_exit

    Exit Sub
    End Sub[/VBA]

    In order to use this macro i have defined a rule that preformed every time that a new mail with attachment is recieved (and then it run the above script.).

    My problem is that it doesnt work properly.
    sometimes its work ok , but in many cases i get an
    "The operation failed. An object could not be found" message.

    i try to deal with it (including define a new profile - it didnt work.)
    and try to run this script manually from the outlook by change the code line [VBA]Sub GetAttachments(Item As Outlook.MailItem)[/VBA]
    to [VBA]Sub GetAttachments()[/VBA]
    and define the variable item as an object in diferent line.
    i tried to preform it on unread attachments messages in my inbox.

    when i have tried this i got an "Object variable or With block variable not set" message.

    As suggested in microsoft article i have read i try to redefine variable "item" as sugested : [VBA]Dim obj1 As Project1.Class1
    Set obj1 = New Project1.Class1[/VBA]
    but it didnt work.

    any ideas from here?

    thanks
    yair

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Check out the Application.NewMail Event or the Items.ItemAdd Event.

    http://msdn.microsoft.com/en-us/library/bb147645.aspx
    http://msdn.microsoft.com/en-us/library/aa171270.aspx

    HTH

    Quote Originally Posted by yairk
    Hey all!

    I have installed and compiled a macro code with script that helps to automaticly extract attachments coming with new mail and preform a saving of the attachd file/s on a specific dir on the hard drive.
    my machine is outlook 2002, running on windows xp.

    it's goes like this:

    [vba]Sub GetAttachments(Item As Outlook.MailItem)
    On Error GoTo GetAttachments_err

    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0


    For Each Atmt In Item.Attachments
    FileName = "DIR_PATH" & Atmt.FileName
    Atmt.SaveAsFile FileName
    i = i + 1
    Next Atmt


    GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub

    GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
    & vbCrLf & "Please note and report the following information." _
    & vbCrLf & "Macro Name: GetAttachments" _
    & vbCrLf & "Error Number: " & Err.Number _
    & vbCrLf & "Error Description: " & Err.Description _
    , vbCritical, "Error!"
    Resume GetAttachments_exit

    Exit Sub
    End Sub[/vba]
    In order to use this macro i have defined a rule that preformed every time that a new mail with attachment is recieved (and then it run the above script.).

    My problem is that it doesnt work properly.
    sometimes its work ok , but in many cases i get an
    "The operation failed. An object could not be found" message.

  3. #3
    hey,

    i have made my best to combine between my macro described here previously , and the method that described at the Application.NewMail Event article.
    to be more specificly , i took from that article the code lines:

    [vba]Public WithEvents myOlApp As Outlook.Application

    Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub[/vba]

    i'm also compiled all into a class module (originally i use just a module).

    unfortunately it didnt work.

    if i didnt mentioned before , i'm not a vb / vba developer and have a lack of experience with this stuff.

    can you please be so kind and show me how should i recreate or combined
    the 2 scripts / methodes?
    (my script, with Application.NewMail Event).

    thanks,
    yair

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    You have to actually initialize the event handler. Usually you do that in Application_MAPILogonComplete or Application_Startup, either of which would need to be included in your ThisOutlookSession module.

    For example:

    [vba]Private WithEvents myItems As Outlook.Items
    Private Sub Application_Startup()
    Set myItems = olNS.GetDefaultFolder(olFolderInbox).Items
    End Sub

    Private Sub myItems_ItemAdd(ByVal Item As Object)
    Dim FileName As String
    Dim Atmt As Attachment
    Dim Msg As Outlook.MailItem
    Set Msg = Item

    If Msg.Attachments.Count = 0 Then Goto ExitProc

    For Each Atmt In Msg.Attachments
    FileName = "DIR_PATH" & Atmt.FileName
    Atmt.SaveAsFile FileName
    i = i + 1
    Next Atmt

    ExitProc:
    Set Msg = Nothing
    End Sub
    [/vba]


    Quote Originally Posted by yairk
    hey,

    i have made my best to combine between my macro described here previously , and the method that described at the Application.NewMail Event article.
    to be more specificly , i took from that article the code lines:

    [vba]Public WithEvents myOlApp As Outlook.Application

    Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub[/vba]
    i'm also compiled all into a class module (originally i use just a module).

    unfortunately it didnt work.

    if i didnt mentioned before , i'm not a vb / vba developer and have a lack of experience with this stuff.

    can you please be so kind and show me how should i recreate or combined
    the 2 scripts / methodes?
    (my script, with Application.NewMail Event).

    thanks,
    yair

  5. #5
    should i still use the code lines
    [vba]Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub
    [/vba]

    or it replaced and handled by your previous code lines?

    yair

  6. #6
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    The code I posted should be a full replacement for yours (minus the two Goto sections). But definitely test it out. You'll need to paste in the code, restart Outlook, then set a breakpoint in the code (F9) and wait for an email to come through.

    HTH

    Quote Originally Posted by yairk
    should i still use the code lines
    [vba]Sub Initialize_handler()
    Set myOlApp = Outlook.Application
    End Sub
    [/vba]
    or it replaced and handled by your previous code lines?

    yair

Posting Permissions

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