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
 
K

ker_01

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
J GoDaddy migrated to Office365 - Outlook Wont Add Account Exchange Server Administration 7
K Outlook 365 After migrating to Outlook 365, some contacts display in emails with prefixes Using Outlook 0
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
Horsepower Cannot delete gmail in iPhone Outlook outbox Using Outlook 1
F Outlook 2019 Outlook 2019 Add and Sync to New computer Comcast server Using Outlook 2
M Understanding Outlook Identities Using Outlook 1
kburrows Outlook or Phone Combining Contacts? Using Outlook 0
P Outlook 2013 search no longer works Using Outlook 1
M Accessing ALL Outlook contact fields Outlook VBA and Custom Forms 2
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 0
R MailTips and older Outlook versions Using Outlook 0
A iCloud Outlook Add In is causing Outlook 2021 to crash and got disabled Using Outlook 4
C How to import Outlook calendar? Using Outlook 0
HarvMan Outlook 365 loses "outlook.com" exchange server settings Using Outlook 1
HarvMan Outlook.com Account Using Outlook 3
D Office 365 Outlook desktop app prompts for all account passwords on startup Using Outlook 6
D Alerts in outlook Using Outlook 1
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
L Recommendations for Utility to Backup Outlook 365 Account Settings Using Outlook 2
J Recommendations for Outlook Duplicate Email Remover Using Outlook 6
O Outlook - How to embed font ? (If at all possible) Using Outlook 2
Z New minimum Outlook for Windows version requirements for Microsoft 365/Exchange Online Using Outlook.com accounts in Outlook 16
T How to configure Outlook for two users on a single Windows account Using Outlook 3
R Outlook 2021 Having problem setting up outlook 2021 with windows 11. I have 3 gmail accounts and I want the 3 gmail, emails to merge into the same outlook input. Using Outlook.com accounts in Outlook 0
e_a_g_l_e_p_i Outlook 2021 all appointments not showing in calendar Using Outlook 2
S Outlook 365 Can I change the possible range of highlighting colours when writing an Outlook email? Using Outlook 1
B Outlook 213 keeps crashing. I just found out there are DMP files. How can I tell Why? Using Outlook 2
H Outlook 2016 related mails in outlook Outlook VBA and Custom Forms 13
S Outlook 2021 Can you make emails from 3 word domains "safe" by entering top 2 word domain into Safe List in Outlook? Using Outlook 1
@ Outlook Using Outlook 2
kburrows Outlook 365 - Ribbon Customizations Disappear Using Outlook 0
D Wishlist How to use 'app password' in Outlook 2003 after Google pulled plug on "less secure apps" Using Outlook 2
L Restoring Outlook from backup pst file Using Outlook 5
e_a_g_l_e_p_i Kernel Outlook .pst viewer Using Outlook 3
L Outlook 365 Outlook not working on linux how to solve it Using Outlook.com accounts in Outlook 0
T Outlook 2019 Using Gmail aliases in Outlook Using Outlook 6
S Activities in Outlook Outlook VBA and Custom Forms 3
S ios outlook Using Outlook.com accounts in Outlook 0
W Transfer Outlook 2016 autocomplete file to Outlook 2007 Using Outlook 1
V iCloud calendar problems, Outlook shuts down immediately Using Outlook 2
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Outlook - eliminate same adresses from to, and bc line Outlook VBA and Custom Forms 0
R How to force Outlook to use plain text in notes for Contacts? Using Outlook 1
G Outlook Express (DBX) to Outlook (PST) Conversion Software Wanted Using Outlook 1
J Outlook 2016 Can't display some embedded HTML images in Outlook 2016 Using Outlook 2
J Outlook EAS protocol version Using Outlook 1
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
C Why does Outlook (desktop) 365 for Windows keep making me input my passwords? Using Outlook 8

Similar threads

Top