excel vba - How do I add code to my Mail_Workbook vba to open everyday, refresh, send, then close? -
i need add vba open workbook, refresh data, automatically, send, close.
here code works fine on it's own need automate daily.
sub mail_workbook() dim outapp object dim outmail object dim emailaddr string dim subj string set outapp = createobject("outlook.application") set outmail = outapp.createitem(0) outmail .to = "me.meeee@company.com" .cc = "" .bcc = "" .subject = "***test*** " & subj .body = subj .attachments.add activeworkbook.fullname .display application.wait (now + timevalue("0:00:02")) application.sendkeys "%s" end set outmail = nothing end sub
you may try below. on workbook open calls procedure runmacro
.
the runmacro
procedure reads values ranges , sets time when mis
procedure has called.
mis
procedure open workbook, refresh , path save file , send mail.
in mail send link workbook , wont attach workbook. can save workbook on shared drive.
put code on thisworkbook
code section
private sub workbook_open() runmacro end sub
put code in standard module.
sub runmacro() dim string, b string, c string, d string, e string = format(range("a3"), "hh:mm:ss") b = format(range("a4"), "hh:mm:ss") c = format(range("a5"), "hh:mm:ss") d = format(range("a6"), "hh:mm:ss") e = format(range("a7"), "hh:mm:ss") application.ontime timevalue(a), "mis" application.ontime timevalue(b), "mis" application.ontime timevalue(c), "mis" application.ontime timevalue(d), "mis" application.ontime timevalue(e), "mis" end sub sub mis() 'open workbook dim wkb workbook dim path string, strfile string, strfilepath string strfile = "file1.xlsx" path = thisworkbook.path & "\" & strfile if isworkbookopen(path) set wkb = workbooks(strfile) else set wkb = workbooks.open(path) end if 'refresh data wkb.refreshall 'get new filepath strfilepath = getfilelink wkb.saveas filename:=strfilepath wkb.close 'send mail sendmail strfilepath end sub function isworkbookopen(filename string) 'check if workbooks open 'isopen return true dim ff long, errno long on error resume next ff = freefile() open filename input lock read #ff close ff errno = err on error goto 0 select case errno case 0: isworkbookopen = false case 70: isworkbookopen = true case else: error errno end select end function sub sendmail(mydest string) 'procedure send mail 'you need configure server & port dim imsg object dim iconf object dim flds variant set imsg = createobject("cdo.message") set iconf = createobject("cdo.configuration") iconf.load -1 set flds = iconf.fields flds .item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "test-svr-002" .item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .update end imsg set .configuration = iconf .to = "test@gmail.com" .from = "test@gmail.com" .subject = "mis reports" & " " & date & " " & time .textbody = "link mis report :" & vbnewline & "<" & mydest & ">" .send end set imsg = nothing set iconf = nothing end sub function getfilelink() string dim fso object, myfolder string set fso = createobject("scripting.filesystemobject") myfolder = thisworkbook.path & "\reports" if fso.folderexists(myfolder) = false fso.createfolder (myfolder) end if myfolder = myfolder & "\" & format(now(), "mmm_yyyy") if fso.folderexists(myfolder) = false fso.createfolder (myfolder) end if getfilelink = myfolder & "\mis " & format(now(), "dd-mm-yy hh.mm.ss") & ".xls" set fso = nothing end function
Comments
Post a Comment