Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying Emails into a VFP table from Outlook 1

Status
Not open for further replies.

dkean4

Programmer
Feb 15, 2015
282
US
Is there some simple way to copy Emails from Outlook (Multiple accounts)? I need to parse the Email content and be able to delete Emails via VFP...

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Very much off the top of my head, and not tested, but it should give you a start.

Code:
oOut = CreateObject("outlook.application")
oNameSpace = oOut.GetNameSpace("MAPI")
oInBox = oNameSpace.GetDefaultFolder(6)

WITH oInBox

  FOR EACH loM IN .Items
    * Display each email; obviously you will want to 
    * store these items somewhere rather than just
    * showing them on the screen	
    ? loM.SenderName
    ? loM.Subject
    ? loM.Body
		
  ENDFOR
	
ENDWITH

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike,

Just what the doctor ordered! B-)




Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Mike,

It works great but not with oNameSpace.GetDefaultFolder(6) I used 5 and it works great! Exactly what I wanted!

Many thanks

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Mike!

No, you are right. I want the InBox. I think that I stumbled into the wrong account, where the inbox is empty or something.

The more important question is how to select the right Email account. I have at least 20 of them.

I found this code to deal with folders, but it does not work... It must be for some previous version of WinOffice. Mine is Win Office 2010...

Code:
loApp = CREATEOBJECT("outlook.application")
loSpace = loApp.GetNameSpace("MAPI")
loFolders = loSpace.Folders
loItems = loSpace.Folders.Items  &&  [b][COLOR=#EF2929]It fails here[/color][/b]


FOR lni = 1 TO loSpace.Folders.Count
   lcName = loSpace.Folders(lni).Name
   ? lcName
ENDFOR

*Folders are hierarchical so each folder can contain their own set of folders as well.

FOR lni = 1 TO loSpace.Folders.Count
   loFolders = loSpace.Folders(lni).Folders
   FOR lnSub = 1 TO loFolders.Count
      lcName = loFolders.Folders(lni).Name
      ? lcName
   ENDFOR
ENDFOR

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Take a look ar olindex.zip from
In regard of OLE object collectiona, always use FOR EACH to iterate them:
Code:
loApp = CREATEOBJECT("outlook.application")
loSpace = loApp.GetNameSpace("MAPI")
FOR EACH loFolder IN loSpace.Folders
   ? loFolder.Name
   FOR EACH loSubFolder IN loFolder.Folders
       ? CHR(9)+loSubFolder.Name
   ENDFOR
ENDFOR

Bye, Olaf.
 
Olaf

Good Example. Thanks...

Concerning OLIndex.zip I downloaded it, but it requires the OLindex DataBase. And it is not included in the zip file. I tried to find info on it on the web and I get no hits in Google... Do you have any ideas where I can get the DBC files?



Never mind it creates its own DB. Very strange, but educational...

Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
You can bet Christof will never put anything incomplete as a download.
I remember I had trouble getting it to run, but it will give you anything you need to extract emails and you only need to take parts of its code for that matter.

By the way, I think today Outlook itself got better in finding mails via its own search mechanisms, so this download isn't necessary for that matter, it's still very good to learn about Outlook folder management.

Bye, Olaf.

 
Olaf

I am trying to parse through my Emails to delete all the incoming junk on continuous basis. I get nearly a thousand junk emails every day. And nothing can beat VFP when you want to be specific, inspecting originating IPs, the message body, subject, CC, BB, etc... OLE automation also allows me to cast the Message body into the browser by choosing the HTML body. I have had this idea for a while.

Anyway, thanks for the suggestion.



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
I think you misunderstood.

OLINDEX.zip is a tool creating a word/fulltext index of mails and in that function it has become irrelevant, but you want to do different things accessing the inbox items and for that matter the part of the code iterating the inbox items of course is helpful.

Bye, Olaf.
 
Dennis,

I'm a bit confused as to whether you are iterating your Inbox or your Sent Items. However, if the former, it occurs to me that you need to do a bit more work.

Certain mail items that appear in the Inbox are not really emails, and don't have certain properties. These include bounce messages and read receipts, and possibly some others. If you try to retrieve some of their properties, you will get an error.

So, inside your FOR EACH loop, you should test for [tt]TYPE(.SenderName) = "U"[/tt]. If that is [tt].T.[/tt], then the mail item is one of those exceptional types, so you should refrain from accessing any of its properties.

I know this doesn't directly answer any of your questions, but I recall that it caught me out once, so I thought I had better mention it.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike

Thank you Mike. Your comments are always helpful. There are several ways to pluck the Emails. I used this one.

Code:
	FOR EACH loSubFolder IN loFolder.Folders
		
		IF loSubFolder.Name = "Inbox"
			FOR EACH Email IN loSubFolder.Items
				APPEND BLANK 
				REPLACE body 		WITH Email.body
				REPLACE made_time 	WITH Email.CreationTime
				TRY
					REPLACE gotbyname 	WITH Email.ReceivedByName
				CATCH 
				ENDTRY 
				REPLACE got_time 	WITH Email.ReceivedTime
				TRY 
					REPLACE got_name 	WITH Email.Recipients.Item.name
				CATCH 
				ENDTRY 

				REPLACE senderadd 	WITH Email.SenderEmailAddress
				REPLACE sendrname 	WITH Email.SenderName
				REPLACE sent_on 	WITH Email.SentOn
				REPLACE Esize 		WITH Email.Size
				REPLACE subject 	WITH Email.Subject
				TRY 
					REPLACE Send_to		WITH Email.To
				CATCH 
				ENDTRY 
				TRY 
					REPLACE html 		WITH Email.HTMLBody
				CATCH 
				ENDTRY 

				Validate_Email()
			ENDFOR 
		ELSE 
		ENDIF
   ENDFOR

The application is nearly done. From what I have seen so far, OLE Automation is not the fastest Ferrari. It is more like a tricycle. It is going at about 5-10 Emails per second. And it is not my code, which take no more than 0.01 seconds. Outlook seems to do lots of management as you iterate through the Emails. Frequently it pauses for a moment and continues. I don't think there is a cure for that. So I am thrilled to be able to do this... no matter what the speed.

My next obstacle is creating custom folders and moving mail into them. I have not found code which works. Some code I which I did find and test uses OLE variables which simply do not come up when I insert them in the functions of the examples given. But let me save that for another question, if I fail to get ahead.

You guys have been very helpful. Thank you all for that....



Dennis Kean

Simplicity is the extreme degree of sophistication.
Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top