save attachment macro w/ "run a script" rule

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

hi,

I have the following code i'm having trouble with. it appears to be in the
objects set up. any help is appreciated.

---------------------------------------------
Sub SaveAttachment(MyMail As MailItem)

Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim strPath As String, strFile As String

strPath = "\\ad\gbs$\Download\HM\"

strID = MyMail.EntryID
Set objNS = Application.GetNamespace("MAPI")
Set objMail = olNS.GetItemFromID(strID)
Set objAttachments = objMail.Attachments

lngCounter = objAttachments.Count

If lngCounter > 0 Then

'iterate the attachment object collection
For i = lngCounter To 1 Step -1

strFile = objAttachments.Item(i).FileName

strFile = strPath & strFile

Debug.Print strFile

' save the attachment file
objAttachments.Item(i).SaveAsFile strFile

Next i
End If

Set objMail = Nothing
Set objNS = Nothing
Set objAttachments = Nothing

End Sub
 
You declared "objNS" as the Namespace but you are setting objMail to
"olNS" -- do you see the spelling error there?

Using "Option Explicit" at the top of every module will prevent simple
syntax errors like this.


HTH,
JP


hi,

I have the following code i'm having trouble with. it appears to be in the
objects set up.  any help is appreciated.

---------------------------------------------
Sub SaveAttachment(MyMail As MailItem)

    Dim strID As String
    Dim objNS As Outlook.NameSpace
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim strPath As String, strFile As String

   strPath = "\\ad\gbs$\Download\HM\"

    strID = MyMail.EntryID
    Set objNS = Application.GetNamespace("MAPI")
    Set objMail = olNS.GetItemFromID(strID)
    Set objAttachments = objMail.Attachments
- snip -
 
thank you. will use Option Explicit going forward.

My next question is how to call this macro from the other procedure?
I need several of them for several rules and the only difference will be the
file path.
-----------------------
Sub Save2HM(MyMail As MailItem)

strPath = "\\ad\gbs$\Download\HM\"
Call SaveAttachment

End Sub

then

Sub Save2BLP(MyMail As MailItem)

strPath = "\\ad\gbs$\Download\BLP\"
Call SaveAttachment

End Sub
etc.

Thank you
 
I'm finding it difficult to help you because in your first post,
SaveAttachment is accepting a MailItem as an argument, then in your
second post, you ask something completely different and you want to
pass a string, not a MailItem, as an argument.

If you want to use the same macro with different paths, you have to
pass the path to the SaveAttachment macro as an argument. Currently
you have a MailItem (not a string) as the argument for SaveAttachment.

Sub SaveAttachment(MyMail As MailItem)

instead of

Sub SaveAttachment(strPath As String)



If you want to call one macro from another, passing the path to it,
try this:

Sub Save2HM()
strPath = "\\ad\gbs$\Download\HM\"
Call SaveAttachment(strPath)
End Sub

Then SaveAttachment would accept the path as a string like this:

Sub SaveAttachment(sPath As String)
' more code here
End Sub

But you won't be able to do anything with a MailItem unless you pass
it as an argument or find it in an Explorer or Inspector window.


HTH,
JP
 
First, a macro is an argumentless subroutine, so what you want to call is a sub, not a macro.

It sounds like what you want to do is parameterize the path, in which case, you add it as an argument to the SaveAttachment procedure:

Sub SaveAttachment(MyMail As MailItem, strPath as String)

removing, of course, the strPath= statement from that procedure.

To call it, you provide the values for the procedure's arguments:


Sub Save2HM(MyMail As MailItem)
strPath = "\\ad\gbs$\Download\HM\"
Call SaveAttachment(MyMail, strPath)
End Sub
 
Back
Top