Scraping outlook emails (2 questions)

  • Thread starter Thread starter ker_01
  • Start date Start date
Status
Not open for further replies.
K

ker_01

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
 
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
>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
K Scraping a specific line # from an email text Outlook VBA and Custom Forms 2
G Outlook 2021 (New) doesn't respect default browser Using Outlook 0
B Outlook or iPhone turning tabs into spaces in Outlook Notes Using Outlook 1
P newly installed Office 365 includes OLD Outlook Using Outlook 6
R Outlook ribbon menu default? Using Outlook 7
H Spam email in Gmail not visible in Outlook Using Outlook 3
J How to transfer Win 10 Outlook to new Windows 11 pc? Using Outlook 9
J Renegade spam URL line displayed in old local Outlook 365 email title Using Outlook 3
G Reduce whitespace in Outlook desktop Contact Cards display Using Outlook 3
C Outlook classic via 365 Using Outlook 2
Dr. Demento Analogous Outlook code to read info into an array (or collection or whatever) Outlook VBA and Custom Forms 7
S Repair Outlook Using Outlook 8
V Outlook Form ListBox is not editable Outlook VBA and Custom Forms 2
F Outlook's contacts Using Outlook 1
D Outlook 2003 stopped dead Using Outlook 2
G Cannot receive emails from gmail account in Outlook 365 Using Outlook 1
E "Cannot display the folder. MS Outlook cannot access the specified file location" Using Outlook 8
P Outlook 2016 Working Offline Using Outlook 2
Rupert Dragwater Cannot reestablish gmail (email address) account in Outlook 365 Using Outlook 11
O Outlook 365 synchronisieren Exchange Server Administration 1
kburrows Outlook Classic - JPG files are corrupted when opened or saved Using Outlook 3
F Sync Outlook Calendar Using Outlook 0
G Change default font size in sticky notes - Outlook Desktop 2021 Using Outlook 2
C VBA in "New Outlook?" Using Outlook 0
D New Outlook with Business Basic Plans Using Outlook 0
D Outlook 2021 not working with Outlook 2003 installed Using Outlook 5
D Outlook 2003 stopped working - get they dialog box asking for username & Password Using Outlook 2
T Outlook 2021 hangs in close on taskbar occasionally Using Outlook 1
M Duplicate removal feature in Outlook 2021 is faulty Using Outlook 2
D.Moore Outlook COM addins source folder Using Outlook 12
P Removing Outlook 365 Account from Send/Receive Using Outlook 3
kburrows Outlook Automatically Merging Contacts Using Outlook 2
A Outlook 2016 Outlook 2016 vs. New Outlook Using Outlook 4
D Outlook Desktop App Email Software Using Outlook 0
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
M Outlook not logging in to server Using Outlook 0
J Outlook macro to run before email is being send Outlook VBA and Custom Forms 3
R Outlook 2021 change view Using Outlook 2
K Outlook font corrupted in some point sizes, resets on close/open Using Outlook 2
J Is the Windows Outlook Tasks module really going to be gone? Using Outlook 6
F Outlook 2010 and Hotmail Using Outlook 1
A Outlook 2021 needs 'enter' for people search Using Outlook 2
HarvMan Outlook 365 Inbox Font Using Outlook 8
Retired Geek Outlook on MAC delete duplicate Sent emails Using Outlook 0
S New Outlook - IMAP ISSUES and support for addins? Using Outlook 1
C outlook.com fonts Using Outlook 2
R Outlook with several IMAP accounts generating folders with 1111 suffix Using Outlook 0
D Send email from Outlook Alias using Mac? Using Outlook 0
G Reply a selected message and remove blank space before signature Outlook 365 version 2406 64BIT Outlook VBA and Custom Forms 2
G Reply a selected message and remove blank space before signature Outlook 365 version 2406 64BIT Outlook VBA and Custom Forms 0

Similar threads

Back
Top