Extract outlook message body text with VBA from Excel -
i have huge number of outlook .msg , outlook .eml files saved shared network folder (ie outside of outlook). trying write vba in excel extracts subjects,sender, cc, receiver, senttime, sentdate, message body text each file , import these info excel cells orderly
subject sender cc receiver senttime sentdate
re:.. mike jane tom 12:00:00 23 jan 2013
i've done similar thing word documents i'm struggling 'get at' text in .msg files.
so far have code below. think i'm on right track @ least, i'm stuck @ line i'm trying set reference msg file. advice appreciated...
dim myoutlook outlook.application dim mymail outlook.mailitem set myoutlook = new outlook.application set mymail = dim filecontents string filecontents = mymail.body
regards
so i've been able working .msg files saved outside of outlook. however, don't have access outlook express have no way of saving .eml files @ moment. here's sub i've come insert subject,sender,cc,to, , sendon excel worksheet starting @ row 2 column 1 (assuming header row @ row 1):
sub getmailinfo(path string) dim myoutlook outlook.application dim msg outlook.mailitem dim x namespace set myoutlook = new outlook.application set x = myoutlook.getnamespace("mapi") filelist = getfilelist(path + "*.msg") row = 1 while row <= ubound(filelist) set msg = x.openshareditem(path + filelist(row)) cells(row + 1, 1) = msg.subject cells(row + 1, 2) = msg.sender cells(row + 1, 3) = msg.cc cells(row + 1, 4) = msg.to cells(row + 1, 5) = msg.senton row = row + 1 wend end sub
which uses getfilelist function defined below (thanks spreadsheetpage.com)
function getfilelist(filespec string) variant ' taken http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/ ' returns array of filenames match filespec ' if no matching files found, returns false dim filearray() variant dim filecount integer dim filename string on error goto nofilesfound filecount = 0 filename = dir(filespec) if filename = "" goto nofilesfound ' loop until no more matching files found while filename <> "" filecount = filecount + 1 redim preserve filearray(1 filecount) filearray(filecount) = filename filename = dir() loop getfilelist = filearray exit function ' error handler nofilesfound: getfilelist = false end function
should straightforward, let me know if need more explanation.
edit: you'll have add reference outlook library
hth!
z
Comments
Post a Comment