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
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 9
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 8
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
V Outlook Error The Attempted operation Failed. An Object Could Not be found Outlook VBA and Custom Forms 0
P Yahoo/IMAP folder rename by Outlook desktop 365 Using Outlook 0
A Outlook 2019 folder counter Using Outlook 0
A Relocate Search Bar in Outlook Using Outlook 2
e_a_g_l_e_p_i Need clarification on 2-Step Verification for Gmail using Outlook 2021 Using Outlook 10
L Opening People Outlook 2021 Using Outlook 2
e_a_g_l_e_p_i Outlook 2021 not letting me setup my Gmail using pop Using Outlook 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
P Can no longer sync Outlook with iPhone calendar after iPhone update to 17.1.1 Using Outlook 7
O Outlook - Switch from Exchange to IMAP Using Outlook 2
e_a_g_l_e_p_i Is it possible to have a reminder in Outlook 2021 for every 90 days Using Outlook 3
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
O How to find out the domain and server settings that my Outlook is using? Using Outlook 2
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
H Integrating Alexa & Outlook Pro 2021 Using Outlook 2
Z Automatically adjust Outlook Reading Pane from bottom to right depending on portrait or landscape window Using Outlook 1
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 15
petunia Outlook tasks module sunsetting? Exchange Server Administration 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 0
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
U Outlook 2021 not showing contact cards in Searches Using Outlook 1
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
H Outlook 365 O365 outlook calendar item editing Using Outlook 1
J Outlook 365 html inline images Using Outlook 0
Rupert Dragwater How to get Outlook 365 to open from websites Using Outlook 5
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
J Outlook 2021 ScanPST errors (yet again ... sorry): repair button missing Outlook 2021 Using Outlook 5
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
K Moved pst to new computer, now Gmail not coming into Outlook Using Outlook 7
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
F Running Scripts in Outlook 2021 Using Outlook 0

Similar threads

Back
Top