{"id":25507,"date":"2019-03-01T17:40:31","date_gmt":"2019-03-01T16:40:31","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=25507"},"modified":"2019-03-01T17:40:31","modified_gmt":"2019-03-01T16:40:31","slug":"estrazione-e-archiviazione-di-allegati-da-e-mail-tramite-script","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/estrazione-e-archiviazione-di-allegati-da-e-mail-tramite-script\/","title":{"rendered":"Estrazione e archiviazione di allegati da e-mail tramite script"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">' Il seguente script \u00e8 un esempio di task estrae i files contenuti in allegati ai rapporti di accettazione e li archivia in una categoria documentale avente codice \"CERFOR\"\r\n\r\ndim q as new qwtable()\r\nq.database=DB\r\nq.sql=\"select l1.*,r.*,c.RAGSOC from LINKDOC l1 join RIDOCACC r on r.[_IDDOC]=l1.IDDOC1 and r.[_CODDOC]=l1.CODDOC1 join CLIFOR C on c.CODICE=r.CODFOR where IDDOC1='A' and IDDOC2='**' and NOMEFILE like '%.msg' and not exists(select * from DOCUMENT d where d.TIPO='CERFOR' and d.CUSTOM10 like l1.NOMEFILE) order by r.NUMDOC,r.RIGA\"\r\nq.requestlive=false\r\nq.active=true\r\n\r\ndim i as integer\r\ndim msg As MsgReader.Outlook.Storage.Message\r\ndim fn as string\r\ndim numero as string\r\ndim campi as assocarray\r\ndim titolo as string\r\ndim ok as boolean\r\ndim emsg as string=\"\"\r\ndim errmsg as string\r\n\r\ndim DOCUMENT as new qwtable()\r\nDOCUMENT.database=DB\r\nDOCUMENT.sql=\"select * from DOCUMENT\"\r\nDOCUMENT.requestlive=false\r\nDOCUMENT.allowallrecords=false\r\n\r\nwhile not q.rowset.endofset()   \r\n   msg = new MsgReader.Outlook.Storage.Message(ctype(q.rowset.fields(\"NOMEFILE\").value,string))\r\n   \r\n   for i=0 to msg.Attachments.count-1\r\n   \tif not dbdollar(\"IMAGE\",upper(msg.Attachments(i).filename)) and not left(msg.Attachments(i).filename,1)=\"~\" and lower(System.io.Path.Getextension(msg.Attachments(i).filename))&lt;&gt;\".msg\"\r\n         \tok=true\r\n      \r\n      \t\t' Recupera la riga del documento se esiste gi\u00e0\r\n            DOCUMENT.active=true\r\n            DOCUMENT.rowset.setrange({\"CERFOR\"})      \r\n      \t\tDOCUMENT.rowset.first()\r\n            DOCUMENT.rowset.beginlocate()     \r\n      \t\tDOCUMENT.rowset.fields(\"CUSTOM8\").value=msg.Attachments(i).filename\r\n      \t\tDOCUMENT.rowset.fields(\"CUSTOM10\").value=q.rowset.fields(\"NOMEFILE\").value\r\n\r\n            if DOCUMENT.rowset.applylocate()\r\n               numero=DOCUMENT.rowset.fields(\"NUMERO\").value\r\n            else                         \r\n               if DOCUMENT.rowset.last()\r\n                  numero=transform(val(DOCUMENT.rowset.fields(\"NUMERO\").value)+1,\"@L 999999\")\r\n               else\r\n                  numero=\"000001\"\r\n               end if\r\n            end if\r\n\r\n            campi=new assocarray()\r\n            campi(\"CUSTOM11\")=q.rowset.fields(\"CODART\").value\r\n      \t\tcampi(\"CUSTOM2\")=q.rowset.fields(\"DESCRIZION\").value\r\n            campi(\"CUSTOM3\")=q.rowset.fields(\"CODFOR\").value\r\n            campi(\"CUSTOM4\")=q.rowset.fields(\"RAGSOC\").value\r\n            campi(\"CUSTOM5\")=q.rowset.fields(\"P_CODICECONTATOREPROV\").value\r\n            campi(\"CUSTOM6\")=q.rowset.fields(\"P_CODICEPROV\").value\r\n            campi(\"CUSTOM8\")=msg.Attachments(i).filename\r\n      \t\tcampi(\"CUSTOM10\")=q.rowset.fields(\"NOMEFILE\").value\r\n\r\n            campi(\"DATE1\")=today()\r\n\r\n            titolo=\"Ordine \"+q.rowset.fields(\"P_CODICECONTATOREPROV\").value+\"\/\"+q.rowset.fields(\"P_CODICEPROV\").value+\" \"+q.rowset.fields(\"CODART\").value+\" \"+q.rowset.fields(\"DESCRIZION\").value+\" - \"+q.rowset.fields(\"CODFOR\").value+\" \"+q.rowset.fields(\"RAGSOC\").value\r\n\r\n      \t\t' Salva l'allegato su un file temporaneo\r\n   \t\t\tfn=funique(Q95_PATH_TEMP+\"\\TEMP????\"+System.io.path.getExtension(msg.Attachments(i).filename))      \r\n      \t\ttry            \r\n   \t\t\t\tSystem.IO.File.WriteAllBytes(fn,msg.Attachments(i).data)\r\n      \t\tcatch e as exception\r\n         \t\temsg+=\"Si \u00e8 verificato un errore durante l'archiviazione del certificato per \"+titolo+chr(13)+\"File: \"+q.rowset.fields(\"NOMEFILE\").value+\" Allegato: \"+msg.Attachments(i).filename+\" Errore: \"+e.message+chr(13)+chr(13)         \r\n         \t\tok=false\r\n      \t\tend try\r\n            \r\n      \t\tif ok\r\n               errmsg=DW_Archivia(DB.qwsession,\"CERFOR\",\"CF\",numero,0,titolo,Campi,true,fn) \r\n               if errmsg&lt;&gt;\"\"\r\n                   emsg+=\"Si \u00e8 verificato un errore durante l'archiviazione del certificato per \"+titolo+chr(13)+\"File: \"+q.rowset.fields(\"NOMEFILE\").value+\" Allegato: \"+msg.Attachments(i).filename+\" Errore: \"+errmsg+chr(13)+chr(13)\r\n                   ok=false\r\n               end if\r\n\r\n               if ok\r\n                   Link_Doc(DB,q.rowset.fields(\"IDDOC1\").value,q.rowset.fields(\"CODDOC1\").value,\"DW\",PackCode(\"DW\",\"CERFOR\",\"CF\",numero),1)\r\n               end if\r\n      \t\tend if\r\n      \r\n      \t\tDOCUMENT.active=false\r\n      End If\r\n\tnext\r\n\tq.rowset.next()\r\nend while\r\nq.active=false\r\n\r\nif emsg&lt;&gt;\"\"\r\n\tthrow new exception(emsg)\r\nEnd If\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; Il seguente script \u00e8 un esempio di task estrae i files contenuti in allegati ai rapporti di accettazione e li archivia in una categoria documentale avente codice &#8220;CERFOR&#8221; dim q as new qwtable() q.database=DB q.sql=&#8221;select l1.*,r.*,c.RAGSOC from LINKDOC l1 join RIDOCACC r on r.[_IDDOC]=l1.IDDOC1 and r.[_CODDOC]=l1.CODDOC1 join CLIFOR C on c.CODICE=r.CODFOR where IDDOC1=&#8217;A&#8217; and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[71,78,82],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25507"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=25507"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25507\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=25507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=25507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=25507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}