excel - Auto-populating To,CC,BCC fields in Outlook using VBA -
i trying populate email distribution list list in column "b" based on condition in 1 cell on in cell "a"
private sub outlook_email()
dim olapp object dim olmail object dim cell range dim emailto string dim emailcc string dim emailbcc string on error resume next set olapp = getobject(, "outlook.application") if olapp nothing set olapp = createobject("outlook.application") end if on error goto 0 emailto = "to" emailcc = "cc" emailbcc = "bcc" each cell in range("b2:b100") if cell.offset(-1, 0) = "to" emailto = emailto & cell.value & ", " elseif cell.offset(-1, 0) = "cc" emailcc = emailcc & cell.value & ", " else cell.offset(-1, 0) = "bcc" emailbcc = emailbcc & cell.value & ", " end if next set olmail = olapp.createitem(0) olmail .to = emailto .cc = emailcc .bcc = emailbcc .display end end sub
the error message getting is: compile error, else without if @ cc line in loop.
thanks assistance!
the construction of strings looks bit malformed. try closer this.
emailto = vbnullstring emailcc = vbnullstring emailbcc = vbnullstring each cell in range("b2:b100") select case lcase(cell.offset(-1, 0).value) case "to" emailto = emailto & cell.value & "; " case "bcc" emailbcc = emailbcc & cell.value & "; " case "cc" emailcc = emailcc & cell.value & "; " case else 'do nothing end select next note semi-colon used email address separator.
Comments
Post a Comment