PDA

View Full Version : problems with running of auto attachments saving macro



yairk
01-14-2009, 03:16 AM
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:

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

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 Sub GetAttachments(Item As Outlook.MailItem)
to Sub GetAttachments()
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 : Dim obj1 As Project1.Class1
Set obj1 = New Project1.Class1
but it didnt work.

any ideas from here?

thanks
yair

JP2112
01-19-2009, 10:07 AM
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


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:

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
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.

yairk
01-20-2009, 02:04 AM
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:

Public WithEvents myOlApp As Outlook.Application

Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub

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

JP2112
01-20-2009, 05:36 AM
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:

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




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:

Public WithEvents myOlApp As Outlook.Application

Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
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

yairk
01-20-2009, 07:03 AM
should i still use the code lines
Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub


or it replaced and handled by your previous code lines?

yair

JP2112
01-20-2009, 03:46 PM
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


should i still use the code lines
Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub

or it replaced and handled by your previous code lines?

yair