Tuesday 15 September 2015

vba - Outlook.MailItem, Object variable or With block variable not set -



vba - Outlook.MailItem, Object variable or With block variable not set -

first time have programming in outlook vba 2007.

i can save info email excel file.

i think problem in outlook.mailitem.

i'm running code:

alternative explicit sub copytoexcel() dim olitem outlook.mailitem dim xlapp excel.application dim xlwb object dim xlsheet object dim vtext, vtext2, vtext3, vtext4, vtext5 variant dim stext string dim rcount long dim bxstarted boolean dim enviro string dim strpath string enviro = cstr(environ("userprofile")) 'the path of workbook strpath = enviro & "\documents\test1.xlsx" on error resume next set xlapp = new excel.application if err <> 0 application.statusbar = "please wait while excel source opened ... " set xlapp = createobject("excel.application") bxstarted = true end if on error goto 0 'open workbook input info set xlwb = xlapp.workbooks.open(strpath) set xlsheet = xlwb.sheets("test") ' process message record 'find next empty line of worksheet rcount = xlsheet.range("b" & xlsheet.rows.count).end(-4162).row rcount = rcount + 1 stext = olitem.body '<------ error dim reg1 regexp dim m1 matchcollection dim m match set reg1 = new regexp ' \s* = invisible spaces ' \d* = match digits ' \w* = match alphanumeric reg1 .pattern = "((boa tarde \w*))" end if reg1.test(stext) set m1 = reg1.execute(stext) each m in m1 vtext = trim(m.submatches(1)) vtext2 = trim(m.submatches(2)) vtext3 = trim(m.submatches(3)) vtext4 = trim(m.submatches(4)) vtext5 = trim(m.submatches(5)) next end if xlsheet.range("b" & rcount) = vtext xlsheet.range("c" & rcount) = vtext2 xlsheet.range("d" & rcount) = vtext3 xlsheet.range("e" & rcount) = vtext4 xlsheet.range("f" & rcount) = vtext5 xlwb.close 1 if bxstarted xlapp.quit end if set xlapp = nil set xlwb = nil set xlsheet = nil end sub

but, have error: line:

stext = olitem.body

some help?

you haven't set olitem reference anywhere in code, you've declared on line dim olitem outlook.mailitem. missing assignment somewhere?

vba outlook-2007

No comments:

Post a Comment