Estrazione e archiviazione di allegati da e-mail tramite script

' Il seguente script è un esempio di task estrae i files contenuti in allegati ai rapporti di accettazione e li archivia in una categoria documentale avente codice "CERFOR"

dim q as new qwtable()
q.database=DB
q.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"
q.requestlive=false
q.active=true

dim i as integer
dim msg As MsgReader.Outlook.Storage.Message
dim fn as string
dim numero as string
dim campi as assocarray
dim titolo as string
dim ok as boolean
dim emsg as string=""
dim errmsg as string

dim DOCUMENT as new qwtable()
DOCUMENT.database=DB
DOCUMENT.sql="select * from DOCUMENT"
DOCUMENT.requestlive=false
DOCUMENT.allowallrecords=false

while not q.rowset.endofset()   
   msg = new MsgReader.Outlook.Storage.Message(ctype(q.rowset.fields("NOMEFILE").value,string))
   
   for i=0 to msg.Attachments.count-1
   	if 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))<>".msg"
         	ok=true
      
      		' Recupera la riga del documento se esiste già
            DOCUMENT.active=true
            DOCUMENT.rowset.setrange({"CERFOR"})      
      		DOCUMENT.rowset.first()
            DOCUMENT.rowset.beginlocate()     
      		DOCUMENT.rowset.fields("CUSTOM8").value=msg.Attachments(i).filename
      		DOCUMENT.rowset.fields("CUSTOM10").value=q.rowset.fields("NOMEFILE").value

            if DOCUMENT.rowset.applylocate()
               numero=DOCUMENT.rowset.fields("NUMERO").value
            else                         
               if DOCUMENT.rowset.last()
                  numero=transform(val(DOCUMENT.rowset.fields("NUMERO").value)+1,"@L 999999")
               else
                  numero="000001"
               end if
            end if

            campi=new assocarray()
            campi("CUSTOM11")=q.rowset.fields("CODART").value
      		campi("CUSTOM2")=q.rowset.fields("DESCRIZION").value
            campi("CUSTOM3")=q.rowset.fields("CODFOR").value
            campi("CUSTOM4")=q.rowset.fields("RAGSOC").value
            campi("CUSTOM5")=q.rowset.fields("P_CODICECONTATOREPROV").value
            campi("CUSTOM6")=q.rowset.fields("P_CODICEPROV").value
            campi("CUSTOM8")=msg.Attachments(i).filename
      		campi("CUSTOM10")=q.rowset.fields("NOMEFILE").value

            campi("DATE1")=today()

            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

      		' Salva l'allegato su un file temporaneo
   			fn=funique(Q95_PATH_TEMP+"\TEMP????"+System.io.path.getExtension(msg.Attachments(i).filename))      
      		try            
   				System.IO.File.WriteAllBytes(fn,msg.Attachments(i).data)
      		catch e as exception
         		emsg+="Si è 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)         
         		ok=false
      		end try
            
      		if ok
               errmsg=DW_Archivia(DB.qwsession,"CERFOR","CF",numero,0,titolo,Campi,true,fn) 
               if errmsg<>""
                   emsg+="Si è 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)
                   ok=false
               end if

               if ok
                   Link_Doc(DB,q.rowset.fields("IDDOC1").value,q.rowset.fields("CODDOC1").value,"DW",PackCode("DW","CERFOR","CF",numero),1)
               end if
      		end if
      
      		DOCUMENT.active=false
      End If
	next
	q.rowset.next()
end while
q.active=false

if emsg<>""
	throw new exception(emsg)
End If