html - Loop through drop down and create separate Outlook email body -
i have dashboard. cell values based on drop down list value (drop down list = name of months). send each dashboard 5 different customers. have loop in code because of customers (for = 1 5), , have loop change email body based on drop down list value. issue email body same - not changing based on drop down value.
sub custommailmessage() dim oapp object dim omail object dim rng range dim sig string dim inputrange range sig = readsignature("internal.htm") set rng = thisworkbook.worksheets("sheet2").range("a1:m3") set dvcell = worksheets("sheet2").range("s1") set inputrange = evaluate(dvcell.validation.formula1) each c in inputrange = 1 5 dvcell = c.value set oapp = createobject("outlook.application") set omail = oapp.createitem(0) omail .to = thisworkbook.worksheets("sheet1").cells(i, 1).value .subject = "this subject" .htmlbody = c.value & rangetohtml(rng) & sig .display end next next c set oapp = nothing set omail = nothing end sub **funtion signature** private function readsignature(signame string) string dim ofso, otextstream, osig object dim appdatadir, sig, sigpath, filename string appdatadir = environ("appdata") & "\microsoft\signatures" sigpath = appdatadir & "\" & signame set ofso = createobject("scripting.filesystemobject") set otextstream = ofso.opentextfile(sigpath) sig = otextstream.readall readsignature = sig end function **funtion create html format excel , create email body** function rangetohtml(rng range) dim fso object dim ts object dim tempfile string dim tempwb workbook tempfile = activeworkbook.path & ".htm" 'copy range , create new workbook past data in rng.copy set tempwb = workbooks.add(1) tempwb.sheets(1) .cells(1).pastespecial paste:=8 .cells(1).pastespecial xlpastevalues, , false, false .cells(1).pastespecial xlpasteformats, , false, false .cells(1).select application.cutcopymode = false end 'publish sheet htm file tempwb.publishobjects.add( _ sourcetype:=xlsourcerange, _ filename:=tempfile, _ sheet:=tempwb.sheets(1).name, _ source:=tempwb.sheets(1).usedrange.address, _ htmltype:=xlhtmlstatic) .publish (true) end 'read data htm file rangetohtml set fso = createobject("scripting.filesystemobject") set ts = fso.getfile(tempfile).openastextstream(1, -2) rangetohtml = ts.readall ts.close rangetohtml = replace(rangetohtml, "align=center x:publishsource=", _ "align=left x:publishsource=") 'close tempwb tempwb.close savechanges:=false 'delete htm file used in function kill tempfile set ts = nothing set fso = nothing set tempwb = nothing end function
any idea?
Comments
Post a Comment