vba - Method 'ThisWorkbook' of object '_Global' failed -
i'm trying copy mail's subject, sendername, date of email pst mailbox excel workbook. here's code have far :
option explicit sub download_outlook_mail_to_excel() dim folder outlook.mapifolder dim sfolders outlook.mapifolder dim irow integer, orow integer dim mailboxname string, pst_folder_name string mailboxname = "backupmailbox" pst_folder_name = "inbox1" 'sample "inbox" or "sent items" 'to directly folder @ high level 'set folder = outlook.session.folders(mailboxname).folders(pst_folder_name) 'to access main folder or subfolder (level-1) each folder in outlook.session.folders(mailboxname).folders if vba.ucase(folder.name) = vba.ucase(pst_folder_name) goto label_folder_found each sfolders in folder.folders if vba.ucase(sfolders.name) = vba.ucase(pst_folder_name) set folder = sfolders goto label_folder_found end if next sfolders next folder label_folder_found: if folder.name = "" msgbox "invalid data in input" goto end_lbl1: end if 'read through each mail , export details excel email archival thisworkbook.sheets(1).activate folder.items.sort "received" 'insert column headers thisworkbook.sheets(1).cells(1, 1) = "sender" thisworkbook.sheets(1).cells(1, 2) = "subject" thisworkbook.sheets(1).cells(1, 3) = "date" thisworkbook.sheets(1).cells(1, 4) = "size" thisworkbook.sheets(1).cells(1, 5) = "emailid" 'thisworkbook.sheets(1).cells(1, 6) = "body" 'export email data pst folder orow = 1 irow = 1 folder.items.count 'if condition import mails received in last 60 days 'to import emails, comment or remove if condition if vba.datevalue(vba.now) - vba.datevalue(folder.items.item(irow).receivedtime) <= 60 orow = orow + 1 thisworkbook.sheets(1).cells(orow, 1).select thisworkbook.sheets(1).cells(orow, 1) = folder.items.item(irow).sendername thisworkbook.sheets(1).cells(orow, 2) = folder.items.item(irow).subject thisworkbook.sheets(1).cells(orow, 3) = folder.items.item(irow).receivedtime thisworkbook.sheets(1).cells(orow, 4) = folder.items.item(irow).size thisworkbook.sheets(1).cells(orow, 5) = folder.items.item(irow).senderemailaddress 'thisworkbook.sheets(1).cells(orow, 6) = folder.items.item(irow).body end if next irow msgbox "outlook mails extracted excel" set folder = nothing set sfolders = nothing end_lbl1: end sub
when run code
"method of range of object _ global failed"
from statement thisworkbook.sheets(1).activate
i have enabled microsoft excel 14.0 object library reference, since code executed outlook application.
does know error means , how can fix ?
just couple of quick tweaks, try this:
option explicit sub download_outlook_mail_to_excel() dim folder outlook.mapifolder dim sfolders outlook.mapifolder dim irow integer dim orow integer dim mailboxname string dim pst_folder_name string const xlworkbookname string = "c:\users\macroman\myworkbook.xlsx" '// change required '// i'm using late binding in case don't have reference set. dim xlapp object dim xlwb object set xlapp = createobject("excel.application") xlapp.visible = false set xlwb = xlapp.workbooks.open(xlworkbookname) mailboxname = "backupmailbox" pst_folder_name = "inbox1" 'sample "inbox" or "sent items" 'to directly folder @ high level 'set folder = outlook.session.folders(mailboxname).folders(pst_folder_name) 'to access main folder or subfolder (level-1) each folder in outlook.session.folders(mailboxname).folders if vba.ucase(folder.name) = vba.ucase(pst_folder_name) goto label_folder_found each sfolders in folder.folders if vba.ucase(sfolders.name) = vba.ucase(pst_folder_name) set folder = sfolders goto label_folder_found end if next sfolders next folder label_folder_found: if folder.name = "" msgbox "invalid data in input" goto end_lbl1: end if 'read through each mail , export details excel email archival xlwb.sheets(1).activate folder.items.sort "received" 'insert column headers xlwb.sheets(1).cells(1, 1) = "sender" xlwb.sheets(1).cells(1, 2) = "subject" xlwb.sheets(1).cells(1, 3) = "date" xlwb.sheets(1).cells(1, 4) = "size" xlwb.sheets(1).cells(1, 5) = "emailid" 'thisworkbook.sheets(1).cells(1, 6) = "body" 'export email data pst folder orow = 1 irow = 1 folder.items.count 'if condition import mails received in last 60 days 'to import emails, comment or remove if condition if vba.datevalue(vba.now) - vba.datevalue(folder.items.item(irow).receivedtime) <= 60 orow = orow + 1 xlwb.sheets(1).cells(orow, 1).select xlwb.sheets(1).cells(orow, 1) = folder.items.item(irow).sendername xlwb.sheets(1).cells(orow, 2) = folder.items.item(irow).subject xlwb.sheets(1).cells(orow, 3) = folder.items.item(irow).receivedtime xlwb.sheets(1).cells(orow, 4) = folder.items.item(irow).size xlwb.sheets(1).cells(orow, 5) = folder.items.item(irow).senderemailaddress 'thisworkbook.sheets(1).cells(orow, 6) = folder.items.item(irow).body end if next irow msgbox "outlook mails extracted excel" set folder = nothing set sfolders = nothing xlwb.close false set xlwb = nothing xlapp.quit set xlapp = nothing end_lbl1: end sub
this open workbook you, no need before hand
Comments
Post a Comment