A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Scraping outlook emails (2 questions)



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old January 8th 10, 08:47 PM posted to microsoft.public.outlook.program_vba
ker_01[_2_]
external usenet poster
 
Posts: 21
Default Scraping outlook emails (2 questions)


Full code appended below.

My goal is that this code (when triggered) will allow the user to select a
destination folder and enter a filename, and select a source outlook folder.

The code should then scrape each email in that folder to get the sent
date/time, the subject line, and the sender, and dump that info into a flat
file (which will be used in Excel).

(1) Apparently outlook doesn't have an application.filedialogue (code errors
out in the function on this line). What is the best way to allow a user to
select a destination folder for a generated file? Alternatively, is there a
way to determine the path to the desktop and always save to the desktop,
given that each user's path to the desktop will be unique?

(2) I'll need my code to work in both Outlook 2003 and 2007. I don't have
direct access to a 2007 box, but would welcome feedback if you see anything
in this code that is likely to fail in 2007

Thank you!
Keith

Sub ParseAlertEmails() '(strSource As String, strLabel As String)
Set myOlApp = CreateObject("Outlook.Application")
Set olns = myOlApp.GetNamespace("MAPI")
Set myinbox = olns.PickFolder
Set myItems = myinbox.Items
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
UseFolder = GetFolder
UseDateApp = Format(Now(), "YYMMDDHHmmss")
UseFN = InputBox("Please enter the name of the destination file", "Save As")
Set tf = fso.CreateTextFile(UseFN & " " & UseDateApp, True)

StartCount = 0
strEmailContents = ""
For Each outlookmessage In myinbox.Items

If IgnoreEmail = False Then
strEmailContents = outlookmessage.ReceivedTime
strEmailContents = strEmailContents & ";" & outlookmessage.sender
strEmailContents = strEmailContents & ";" & outlookmessage.Subject

tf.Write strEmailContents & vbCrLf
End If


strEmailContents = ""
StartCount = StartCount + 1
Next

tf.Close
Set fso = Nothing

Set myOlApp = Nothing
Set olns = Nothing
Set myinbox = Nothing
Set myItems = Nothing

End Sub


Private Function GetFolder(Optional strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker) 'this is where
it errors
With fldr
.Title = "Select a Folder in which to save the output file"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function

  #2  
Old January 9th 10, 01:36 AM posted to microsoft.public.outlook.program_vba
ker_01[_2_]
external usenet poster
 
Posts: 21
Default Scraping outlook emails (2 questions)

Question 1 solved- Found a way to do it here
(http://www.vbforums.com/archive/index.php/t-400121.html) for the benefit of
future searchers.

I'll try to get access to a 2007 machine to test the new code, and I'll
create a new thread if I run into issues.

Thank you,
Keith

"ker_01" wrote:


Full code appended below.

My goal is that this code (when triggered) will allow the user to select a
destination folder and enter a filename, and select a source outlook folder.

The code should then scrape each email in that folder to get the sent
date/time, the subject line, and the sender, and dump that info into a flat
file (which will be used in Excel).

(1) Apparently outlook doesn't have an application.filedialogue (code errors
out in the function on this line). What is the best way to allow a user to
select a destination folder for a generated file? Alternatively, is there a
way to determine the path to the desktop and always save to the desktop,
given that each user's path to the desktop will be unique?

(2) I'll need my code to work in both Outlook 2003 and 2007. I don't have
direct access to a 2007 box, but would welcome feedback if you see anything
in this code that is likely to fail in 2007

Thank you!
Keith

Sub ParseAlertEmails() '(strSource As String, strLabel As String)
Set myOlApp = CreateObject("Outlook.Application")
Set olns = myOlApp.GetNamespace("MAPI")
Set myinbox = olns.PickFolder
Set myItems = myinbox.Items
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
UseFolder = GetFolder
UseDateApp = Format(Now(), "YYMMDDHHmmss")
UseFN = InputBox("Please enter the name of the destination file", "Save As")
Set tf = fso.CreateTextFile(UseFN & " " & UseDateApp, True)

StartCount = 0
strEmailContents = ""
For Each outlookmessage In myinbox.Items

If IgnoreEmail = False Then
strEmailContents = outlookmessage.ReceivedTime
strEmailContents = strEmailContents & ";" & outlookmessage.sender
strEmailContents = strEmailContents & ";" & outlookmessage.Subject

tf.Write strEmailContents & vbCrLf
End If


strEmailContents = ""
StartCount = StartCount + 1
Next

tf.Close
Set fso = Nothing

Set myOlApp = Nothing
Set olns = Nothing
Set myinbox = Nothing
Set myItems = Nothing

End Sub


Private Function GetFolder(Optional strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker) 'this is where
it errors
With fldr
.Title = "Select a Folder in which to save the output file"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook com add in questions John Add-ins for Outlook 7 October 25th 07 07:59 PM
Outlook questions Fredfarkwater Outlook - Installation 1 February 14th 07 07:49 PM
Need help scraping calendar info rut Outlook and VBA 3 July 7th 06 06:18 PM
Some Questions about Outlook Dotku Outlook - General Queries 4 May 8th 06 03:00 PM
Outlook Questions ezstepper Outlook - Calandaring 0 January 20th 06 07:21 PM


All times are GMT +1. The time now is 12:59 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.