Link to home
Start Free TrialLog in
Avatar of meng_97
meng_97

asked on

reading e-mail attachments

how can I automatically open e-mail attachments in msexchange,one by one, that are in text form and insert the contents in a textbox where I can read the contents using MAPI? If you can answer this by October 6,1999(today's 10/4/1999) i'll be willing to part with you 300pts
Avatar of meng_97
meng_97

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of troywillmot
troywillmot

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
Hi, to be more specific, the following code uses the DLL I mentioned before to go through all the messages in the current users inbox and put the contents of the first attachement from each one into a text box called text1 on a form called form1.

You can modify it/clean it up to do what you want.

  Dim EMailMessage As Message
  Dim EMailSession As session
  Dim EMailRecipient As Recipient
  Dim EMailAttachment As Attachment
  Dim EMailFolder As Folder
  Dim SubFolder As Folder
  Dim Attachement As Attachment
 
  Dim Content As String
  Dim Cnt As Long
 
  ' create a session then log on, supplying username and password
  Set EMailSession = CreateObject("MAPI.Session")
  ' change the parameters to valid values for your configuration
  EMailSession.Logon

  Set EMailFolder = EMailSession.InfoStores("Personal Folders").RootFolder
 
  For Cnt = 1 To EMailFolder.Folders.Count
    If EMailFolder.Folders(Cnt).Name = "Inbox" Then
      Set SubFolder = EMailFolder.Folders(Cnt)
      Exit For
    End If
  Next Cnt
 
  For Cnt = 1 To SubFolder.Messages.Count
    Set EMailMessage = SubFolder.Messages(Cnt)
    If EMailMessage.Attachments.Count > 0 Then
      Set Attachement = EMailMessage.Attachments(1)
     
      Attachement.WriteToFile "c:\temp\tmp1.atc"
     
      Open "c:\temp\tmp1.atc" For Binary Shared As #1
        Content = Space$(LOF(1))
        Get #1, 1, Content
      Close #1
     
      Kill "c:\temp\tmp1.atc"
     
      Form1.Text1 = Form1.Text1 & Content
    End If
  Next Cnt
Avatar of meng_97

ASKER

Hi Thanks very much for such a quick response, but could u please explain the codes? I mean just put comments on what it does I'm not good at reading codes, I'm more of concepts and ideas, nad is not very good at coding. And if your kind enough, could you please write some other codes in other ways? jut incase this one won;t work for me :) THANKS!!
Avatar of meng_97

ASKER

Hi Thanks very much for such a quick response, but could u please explain the codes? I mean just put comments on what it does I'm not good at reading codes, I'm more of concepts and ideas, nad is not very good at coding. And if your kind enough, could you please write some other codes in other ways? jut incase this one won;t work for me :) THANKS!!

I tried the Codes bu the declarations didn't work!
Avatar of meng_97

ASKER

Edited text of question.
Sure. Sorry, I should have done that in the first place. The code wasn't very neat to begin with, so its no wonder you had trouble. Let me know if this is any better.


  'Declare some object variable's we're going to need -
  ' we must reference the Microsoft Active Messaging DLL
  ' before this will work

  Dim EMailMessage As Message    
  Dim EMailSession As session        
  Dim EMailAttachment As Attachment
  Dim EMailFolder As Folder
  Dim SubFolder As Folder  
  Dim Attachement As Attachment
 
  'Declare a couple of primitive variables for holding
  'strings/doing loops
  Dim Content As String
  Dim Cnt As Long
   
  ' create a session then log on, supplying username and
  'password, or in our case just logon without (I Believe
  ' that lets us in under the current user)
 
  Set EMailSession = CreateObject("MAPI.Session")

  ' change the parameters to valid values for your
  'configuration - i.e, pass a user name and password if
  'required
  EMailSession.Logon

  'Get the root EMail folder - in my case I use outlook
  'for  my MAPI client so its 'Personal Folders' - outlook
  'express etc may use a different folder name, so it might
  'be better just to use numeric indexes and select the
  'first one, i.e
  'Set EMailFolder = EMailSession.InfoStores(1).RootFolder
  'But for this example, I'll just use "Personal Folders"

  Set EMailFolder = EMailSession.InfoStores("Personal Folders").RootFolder

  'For each sub folder in the root folder, check its name
  'until we find the inbox
  For Cnt = 1 To EMailFolder.Folders.Count
    If EMailFolder.Folders(Cnt).Name = "Inbox" Then
      'We found the inbox so set out subfolder variable
      'and exit the loop
      Set SubFolder = EMailFolder.Folders(Cnt)
      Exit For
    End If
  Next Cnt
   
  'Subfolder should now contain the object that is the
  'inbox, but in a real system you'd check to see if sub
  'folder was nothing before trying to use it

  'Now, for each message in the inbox (subfolder)
  For Cnt = 1 To SubFolder.Messages.Count
    Set EMailMessage = SubFolder.Messages(Cnt)
    'Check to see if the message has an attachement
    If EMailMessage.Attachments.Count > 0 Then
      ' Get the first attachement - note, in your case
      'you may want to loop here from 1 to
      'emailmessage.attachements.count
      Set Attachement = EMailMessage.Attachments(1)
     
      'Save the contents of the attachment to a file (the
      'only way I know of to read it)
      Attachement.WriteToFile "c:\temp\tmp1.atc"
     
      'Open the file we saved
      Open "c:\temp\tmp1.atc" For Binary Shared As #1
      'Read the files contents
        Content = Space$(LOF(1))
        Get #1, 1, Content
      Close #1
       
      'Kill the file
      Kill "c:\temp\tmp1.atc"
       
      'Put the contents of the attachment into the text box
      Form1.Text1 = Form1.Text1 & Content
     
      'Clean up our content variable
      Content=vbnullstring
    End If
  Next Cnt
You can download the required DLL's from :

http://msdn.microsoft.com/vbasic/downloads/addon.asp
The declarations didn't work ?

Did you include a reference to the DLL I said you needed ?
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
https://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange