Scraping outlook emails (2 questions)

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
H Macro to Delete Duplicate items in Outlook calendar where title is the same and date is the same Outlook VBA and Custom Forms 0
Y The New Outlook - Q's & Thoughts Using Outlook 7
M Start Outlook 365 at unified inbox Using Outlook 2
B Requesting VBA code to make Outlook prompt for confirmation when deleting a task? Outlook VBA and Custom Forms 4
T In-line reply style in Outlook II Outlook VBA and Custom Forms 1
G Outlook Contact Item.Restrict where FullName is NULL Outlook VBA and Custom Forms 3
J Emails with .ICS calendar invitations attached don't contents when received in Outlook 365 Using Outlook 6
W Outlook 2021 Hanging on "Sending Emails" Using Outlook 3
T Outlook Desk Top 2021 Overdoing Security Using Outlook 7
O Any 3rd party tool that sync (mirror) from Outlook Contacts to Google Contacts? Using Outlook 4
D Outlook 365 Outlook Message "Cannot be sent because the message has changed" Using Outlook 0
J Hotmail drafts started on desktop disappear, but show in web version of Outlook Using Outlook 4
Z Hotmail account deleted from outlook and issue with account not exist Using Outlook 0
J 'Name on the security certificate is invalid or...' - Outlook on desktop talking to hotmail.com account Using Outlook.com accounts in Outlook 5
J Outlook, Word, Access crash unless run in Admin mode - time dependent it seems Using Outlook 10
T Why do Outlook Desktop 2021 tasks from my wife's email show up in my task pane? Using Outlook 2
B Delete Read Receipts in Your Outlook Outlook VBA and Custom Forms 0
M Outlook changes the "Modified" field when moving/copying a Contact. Using Outlook 0
macdotcom Outlook 365 Outlook folder export to PST archival tool - close, but need a nudge Outlook VBA and Custom Forms 2
M Outlook 2016 still feature updating Using Outlook 0
e_a_g_l_e_p_i Question about Outlook 2021 and Gmail Using Outlook 8
J Outlook VBA to send from Non-default Account & Data Files Outlook VBA and Custom Forms 4
P Limited Support for 3rd Party Mail in new Outlook? Using Outlook 1
O Any 3rd party tool that sync (mirror) from Outlook Calendar to Google Calendar? Using Outlook 5
T Outlook is categorizing emails incorrectly Using Outlook 0
R Legacy Outlook on Mac Email Cache Using Outlook 0
A Outlook can't remember outlook.com, Exchange password. Using Outlook 4
S Related messages show in main Outlook window vice new Advanced Find windows Using Outlook 1
H Force Outlook 2019 with GMail 2-Step to Require Login? Using Outlook 0
G Retaining Tabs in outlook body Using Outlook 2
V Setting up Outlook 2021 on new computer Using Outlook 2
G Add Map It button to Custom Contacts Form in Outlook Outlook VBA and Custom Forms 1
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
Victor_50 Problem - Google Workspace will stop "unsafe" access to Outlook end 2024 Using Outlook 3
C New pc, new outlook, is it possible to import auto-complete emailaddress Using Outlook 4
T Outlook 365 won't take new working password Using Outlook 0
S Create Outlook Task from Template and append Body with Email Body Outlook VBA and Custom Forms 4
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
B Sync Outlook Public Folders to Contacts Using Outlook 2
D Delete Outlook emails from MS server Using Outlook 17
B Outlook tasks and PDF Using Outlook 4
D Outlook 2019 is no longer asking for password ... Using Outlook 5
Kika Melo How to mark as Junk any message not from Contacts (in Outlook.com) Using Outlook 3
L Outlook attachments from OneDrive as links Using Outlook 0
G Outlook 365 My iCloud Outlook doesn’t work after reinstalling Microsoft365 on Windows 10 PC – now I get error message on contacts and calendar Using Outlook 1
T How to Export & Import GMAIL Contacts into Outlook 2021 ? Using Outlook 4
M Synchronization and backup of Outlook from local to server. Using Outlook 11
T How to get an EVENT COLOR option in Outlook 2021 ? Using Outlook 0
K How can I delete an e-mail from Outlook Using Outlook 1

Similar threads

Back
Top