excel - Look in separate folders then specify message details on attached file location -


i have code picks recent pdf out of folder , send specfied email address (courtesy of user answered previous post).

it working individual folders , various email specs i'd folder , have different message specifications if file found in other folder.

my code @ moment re-runs process , looks in other folder (this hasn't worked due on complication , confusing variables). know lot of cry looking @ attempt i've made scrappy, clunky poor quality - @ moment sends files processed first message spec, , last 1 processed again second message specification.

option explicit  sub sendfiles()     dim objoutlook object     dim fso object     dim strfile string     dim fsofile     dim fsofldr     dim dtnew date, snew string     dim newoutlookinstance boolean  set fso = createobject("scripting.filesystemobject")  if getoutlook(objoutlook, newoutlookinstance)      strfile = "c:\temp\" 'path folder     set fsofldr = fso.getfolder(strfile)     dtnew = now() - timevalue("00:00:30") '30 seconds ago      each fsofile in fsofldr.files         if fsofile.datecreated > dtnew             snew = fsofile.path             objoutlook.createitem(olmailitem)                 .to = "email@address.com"                 .subject = "example"                 .bodyformat = olformatplain                 .attachments.add snew                 .importance = olimportancehigh                 .send             end         end if     next     if newoutlookinstance objoutlook.quit '<--| quit outlook if running instance of hasn't been found     set objoutlook = nothing  else     msgbox "sorry: couldn't valid outlook instance running" end if  dim obj object dim usdfile string dim afile dim afldr dim dnew date, tnew string dim newinstance boolean  set fso = createobject("scripting.filesystemobject")  if getoutlook(obj, newinstance)  usdfile = "h:\supply chain - jan17\depannage & kanban requests (ab-   tg)\unsatisfied depannage\" set afldr = fso.getfolder(usdfile) dnew = now() - timevalue("00:00:30")  each afile in afldr.files     if afile.datecreated > dnew         tnew = afile.path         obj.createitem(olmailitem)             .to = "email.address2@gmail.com"             .subject = "kanban request - limited stock"             .bodyformat = olformatplain             .attachments.add snew             .importance = olimportancehigh             .send         end     end if next  if newinstance obj.quit set obj = nothing   else     msgbox "sorry: couldn't valid outlook instance running" end if  end sub      function getoutlook(objoutlook object, newoutlookinstance boolean)     boolean set objoutlook = getobject(, "outlook.application") if objoutlook nothing     set objoutlook = new outlook.application     newoutlookinstance = true end if getoutlook = not objoutlook nothing end function 

you refactor code , generate helper sub demand task search passed folder , send emails passed address passed subject:

sub sendfilesfromfolder(objoutlook object, fso object, fldrname string, emailaddress string, subject string, dtnew date)     dim fsofile file      each fsofile in fso.getfolder(fldrname).files         if fsofile.datecreated > dtnew             objoutlook.createitem(olmailitem)                 .to = emailaddress                 .subject = subject                 .bodyformat = olformatplain                 .attachments.add fsofile.path                 .importance = olimportancehigh                 .send             end         end if     next end sub 

correspondingly, "main" code become:

sub sendfiles()     dim objoutlook object     dim fso object     dim dtnew date     dim newoutlookinstance boolean       if getoutlook(objoutlook, newoutlookinstance)         set fso = createobject("scripting.filesystemobject")         dtnew = now() - timevalue("00:00:30") '30 seconds ago          sendfilesfromfolder objoutlook, _                             fso, _                             "c:\temp\", _                             "email@address.com", _                             "example", _                             dtnew          sendfilesfromfolder objoutlook, _                             fso, _                             "h:\supply chain - jan17\depannage & kanban requests (ab-   tg)\unsatisfied depannage\", _                             "email.address2@gmail.com", _                             "kanban request - limited stock", _                             dtnew          if newoutlookinstance objoutlook.quit '<--| quit outlook if running instance of hasn't been found         set objoutlook = nothing         set fso = nothing      else         msgbox "sorry: couldn't valid outlook instance running"     end if  end sub 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -