Link to home
Start Free TrialLog in
Avatar of kinghaim
kinghaim

asked on

how to get outlook attachment pathname

hi everyone.

i have macro running on outlook checking what is the attachment pathname.

but path name is empty.
dose anyone know how to get the pathname.

here is the function


    For Each oAttach In objMailItme.Attachments
        sPath = oAttach.PathName 'here is my problem oAttach.PathName is empty
        If InStr(sPath, "\\MyServer\MyDataFolders\MyEmailAttachmentFolder") = 0 Then
            CheckAttachment = False
            MsgBox "Attachment is not from a valid Path"
            Exit Function
        End If
    Next
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

What references or objects have you got because when I try that code it says runtime error 424 - object required. I have not done VBA for outlook before but If I know what reference(s) you have used then I am positive I can figure something out :) I am pretty sure it is a reference with regards to the objMailItme. Also when I inserted it into a macro it was

Sub Return_Path()


End Sub

hence the exit function did not work so does this mean you have this in your own function and call it from the macro ?
http://vbcity.com/forums/topic.asp?tid=93733

http://www.fontstuff.com/outlook/oltut01.htm

Those are 2 I found. Maybe they can help you out :) A little busy now so I will try and find some more in a while :)

Other then that I just need to know the references or objects or whatever you have used so that I can mess around with this code :)
Avatar of kinghaim
kinghaim

ASKER

Thank you for the response gecko_au2003

to clarify my problem, i am posting the 2 functions in the macro that need to be fixed.


Public UserAccount As String

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   
    On Local Error GoTo ErrItemSend
   
    frmLogIn.Show 1 ' Force the user to Log In to the Server before sending the Email
   
    'check the user account
    If UserAccount = "" Or UserAccount = "Error" Then
        MsgBox "Unable to log you in, please try again later. " & vbLf & _
        "if the problem persist please contact your system administrator"
        Cancel = True
        Exit Sub
    End If
   
    If CheckAttachment(Item) Then
        Cancel = False
    Else
        Cancel = True
    End If
   

ErrItemSend:
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Cancel = True
    End If
End Sub

Private Function CheckAttachment(ByVal objMailItme As MailItem) As Boolean
    On Local Error GoTo ErrCheckAttachment
    Dim oAttach  As Attachment
   
    For Each oAttach In objMailItme.Attachments
        sPath = oAttach.PathName 'here is my problem oAttach.PathName is empty
        If InStr(sPath, "\\MyServer\MyDataFolders\MyEmailAttachmentFolder") = 0 Then
            CheckAttachment = False
            MsgBox "Attachment is not from a valid Path"
            Exit Function
        End If
    Next
   
    CheckAttachment = True
   
ErrCheckAttachment:
    If Err.Number <> 0 Then
        CheckAttachment = False
        MsgBox Err.Description
    End If
End Function

i have backup function that basically check the "valid directory" for the file and  replace the attachment with the one in that directory (just so the user will not cheat the macro). but i do not want to use that because of permission/Security issues.

i did not add any references that corisponed to the 2 functions.
the only reference i have is ADO.

 
From the macro I call this :

Call Application_ItemSend(Item, False)

Not sure what Item is as you have that byval, so does this mean you have the path to the file you want to attach in there so it would be :

Call Application_ItemSend("C:\excelfile.xls",false)

if not then I need an example of how to call the function so that I can get it to work, I have also tried just calling the CheckAttachment function to no success, when I just had the code from your question it gave me an error on this line :

For Each oAttach In objMailItme.Attachments

which I am sure is fixed now because it is inside of the correct function as you posted :)
I found this, not sure how much use this is to you :

http://www.danevans.co.uk/vba/
the Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
is outlook Sub that get called every time you click the send button.
where Item is MailItem(the email) and Cancel is where you set the Email to be sent or not, by default Cancel = True (would be Sent)  
thanks for the link.
 but that is not what i need that macro check the Email body for "Attach" String and then check to see if you have attachment, if not it will pop up a message saying that in the email body you mention attachment but you have no attachment then you will have the option to send the mail or cancel the send.

It uses the same application sub “Application_ItemSend” but it dose not have what I need
click on that one line you are having problems with in order to debug it :

sPath = oAttach.PathName 'here is my problem oAttach.PathName is empty

Attach a file and then run your coding, after you attach something is the path still empty ? You could also try and programitcally attach something before you check its path. Is that the correct code to get the path though ?

Just you said it does not return a path but you did not mention nothing about attaching anything first so just stating the obvious to make sure.



http://www.vbaexpress.com/forum/showthread.php?p=6329#post6329

http://www.vbaexpress.com/forum/showthread.php?p=16652#post16652

Those are 2 others I have found, closest thing I can find to help you out. basically go to www.vbaexpress.com and do a search on there to see if they have any solutions to your problem. Other then that I am jumping around in the dark as I do not really use VBA / macros to automate things. Something I will need to learn but later on :-S

Sorry I can not help you much more :-S
"click on that one line you are having problems with in order to debug it" Done that that's how i know the path is empty
"Attach a file and then run your coding, after you attach something is the path still empty ?" yes
"You could also try and programitcally attach something before you check its path." well if i attach something like that i would know the path simply because i attached it.
 Is that the correct code to get the path though ? i don't know, and that what i would like to know.

ASKER CERTIFIED SOLUTION
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could try and do something simple like make a macro that just displays the attachment path something like so :

Dim oAttach  As Attachment
MsgBox oAttach.PathName

I checked that command and it is in the drop down list. When you type oAttach and type a . <DOT> it drops down a list and that command exists.

If that piece of code displays the path in your new macro then obviously it works, if it does something else then obviously it is not what you want.
humm that's my problem oAttach.PathName is empty
what about oAttach.FileName ?? OR

oAttach.PathName(oAttach.FileName)

or something like that to get the pathname of the currently selected file(s) ??
Sorry about all the posts and suggestions, just never done something like this before so I am just trying to figure this out by trying different things.
well we've got it gecko_au2003

one of your link gave me idea to use FileDialog to attach the files, and it's working, not like i would like it to but I’ll take it.

Thanks for the hard work.

the poins are yours.
thanks kinghaim, sorry I couldnt be of more help. I have never done this sort of thing before. If you would like I can carry on researching and see if we can come up with another solution. Not sure how succesful that will be though.