{"id":24370,"date":"2016-12-08T19:32:40","date_gmt":"2016-12-08T18:32:40","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=24370"},"modified":"2019-05-08T10:57:07","modified_gmt":"2019-05-08T08:57:07","slug":"merge-di-documenti-word","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/merge-di-documenti-word\/","title":{"rendered":"Merge di documenti Word"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">\u2018 Grazie alla libreria OpenXml SDK di Microsoft, inclusa dalla release 2015.05b della piattaforma\r\n\r\n\u2018 web QWay\/QualiWare, \u00e8 possibile con poche righe di codice aggiungere il contenuto di uno o pi\u00f9 documenti Word all\u2019interno di un altro documento Word esistente.\r\n\r\n\u2018 Il seguente frammento di codice inserisce in un documento Word diversi documenti presenti in un\u2019altra\r\n\u2018 categoria documentale, e il cui codice \u00e8 specificato in 3 listbox.\r\n\u2018 I documenti vengono inseriti in corrispondenza di 3 segnalibri, uno per listbox.\r\ndim i as integer\r\ndim items as dbarray\r\ndim tipo_cont,numero as string\r\ndim q as qwtable\r\ndim a as dbarray\r\ndim msg as string=\u201d\u201d\r\ndim QWSess as QWSession=form.page.session(\u201cQWSess\u201d)\r\ndim s as string\r\ndim listboxes as dbarray\r\ndim l as integer\r\ndim segnalibri as dbarray\r\n \r\ndim path_doc as string\r\ndim path_scheda as string\r\n \r\nq=new qwtable\r\nq.database=form.form_state.dmd_ref.DB\r\nq.requestlive=false\r\nq.allowallrecords=false\r\n \r\n\u2018 Recupera il percorso del documento corrente\r\nq.sql=\u201dselect DIRECTORY,NOMEFILE from DOCUMENT d join TIPI_DOC t on d.TIPO=t.TIPO where d.TIPO=\u2019POSDOC\u2019 and d.TIPO_CONT=:TIPO_CONT and d.NUMERO=:NUMERO and d.IND_REV=:IND_REV\u201d\r\nq.params(\u201cTIPO_CONT\u201d)=form.findcontrol(\u201cTIPO_CONT\u201d).Value\r\nq.params(\u201cNUMERO\u201d)=form.findcontrol(\u201cNUMERO\u201d).Value\r\nq.params(\u201cIND_REV\u201d)=form.findcontrol(\u201cIND_REV\u201d).Value\r\nq.active=true\r\n \r\nif not dbdollar(\u201c.DOCX\u201d,upper(q.rowset.fields(\u201cNOMEFILE\u201d).value)) and not dbdollar(\u201c.DOCM\u201d,upper(q.rowset.fields(\u201cNOMEFILE\u201d).value))\r\n   msg=\u201dIl documento del POS deve essere in formato DOCX o DOCM\u201d+chr(13)\r\nelse   \r\n\u2018 Recupera il percorso del documento corrente\r\npath_doc=QWSess.Q95_NOME_DIR_DOCUM+\u201d\\\u201d+q.rowset.fields(\u201cDIRECTORY\u201d).value+\u201d\\\u201d+QWSess.DESCRITT.rowset.fields(\u201cDIR_ATTESA\u201d).value+\u201d\\\u201d+q.rowset.fields(\u201cNOMEFILE\u201d).value\r\n \r\nq.active=false\r\n \r\nif not file(path_doc) then\r\n  form.alert(\u201cFile del documento non presente. Contattare l\u2019amministratore\u201d)\r\nelse\r\n  q.active=false\r\n  q.sql=\u201dselect NOMEFILE,DIRECTORY from DOCUMENT d join TIPI_DOC t on t.TIPO=d.TIPO where d.TIPO=\u2019POSSCH\u2019 and d.PUBBLICATO=1 and d.OBSOLETO=0 and d.TIPO_CONT=:TIPO_CONT and d.NUMERO=:NUMERO order by d.IND_REV desc\u201d\r\n \r\n  Dim myDoc As DocumentFormat.OpenXml.Packaging.WordprocessingDocument = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(path_doc, True)\r\n  Dim mainPart As DocumentFormat.OpenXml.Packaging.MainDocumentPart = myDoc.MainDocumentPart\r\n  \u2018 Scorre su tutte le listbox utilizzando la stessa logica\r\n  listboxes=new dbarray(form.findcontrol(\u201cNotebook1\u201d).controls(3).findcontrol(\u201cATTIVITA\u201d),form.findcontrol(\u201cNotebook1\u201d).controls(4).findcontrol(\u201cATTREZZATURE\u201d),form.findcontrol(\u201cNotebook1\u201d).controls(5).findcontrol(\u201cMEZZI\u201d))\r\n  segnalibri=new dbarray(\u201cATTIVITA\u201d,\u201dATTREZZATURE\u201d,\u201dMEZZI\u201d)\r\n  \r\n  for l=1 to listboxes.size\r\n  items=listboxes(l).GetItems()\r\n \r\n  for i=1 to items.size\r\ns=left(items(i),at(\u201d -\u201c,items(i))-1)\r\na=string2array(s,\u201d|\u201d)\r\ntipo_cont=a(1)\r\nnumero=a(2)\r\n\u2018 Cerca tutti i files che fanno riferimento all\u2019oggetto inserito\r\nq.params(\u201cTIPO_CONT\u201d)=tipo_cont\r\nq.params(\u201cNUMERO\u201d)=numero\r\nq.active=true\r\nif q.rowset.first()\r\n    if not dbdollar(\u201c.DOCX\u201d,upper(q.rowset.fields(\u201cNOMEFILE\u201d).value)) and not dbdollar(\u201c.DOCM\u201d,upper(q.rowset.fields(\u201cNOMEFILE\u201d).value))\r\n  msg+=\u201dIl documento relativo a \u201c+items(i)+\u201d non \u00e8 in formato DOCX o DOCM\u201d+chr(13)\r\nelse   \r\n      path_scheda=QWSess.Q95_NOME_DIR_DOCUM+\u201d\\\u201d+q.rowset.fields(\u201cDIRECTORY\u201d).value+\u201d\\\u201d+QWSess.DESCRITT.rowset.fields(\u201cDIR_IN_VIG\u201d).value+\u201d\\\u201d+q.rowset.fields(\u201cNOMEFILE\u201d).value\r\n \r\n                       try\r\n  \u2018 Verifica se il documento \u00e8 gi\u00e0 presente cercando un segnalibro avente come nome il codice del documento\r\n  dim BookmarkStart=myDoc.MainDocumentPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.BookmarkStart)().FirstOrDefault(Function(x) x.Name = tipo_cont+numero)\r\n  If BookmarkStart Is Nothing Then                          \r\n\u2018 Il documento non esiste ancora: cerca il segnalibro del gruppo e aggiunge il documento alla fine\r\nBookmarkStart=myDoc.MainDocumentPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.BookmarkStart)().FirstOrDefault(Function(x) x.Name = segnalibri(l))\r\nif not BookmarkStart is nothing then\r\n  Dim altChunkId As DocumentFormat.OpenXml.StringValue = tipo_cont+numero \u2018 Deve essere unico\r\n  Dim chunk As DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(\r\n  DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML, altChunkId)\r\n  Using fileStream As System.IO.FileStream = System.IO.File.Open(path_scheda, System.IO.FileMode.Open)\r\nchunk.FeedData(fileStream)\r\n  End Using\r\n \r\n  Dim altChunk As New DocumentFormat.OpenXml.Wordprocessing.AltChunk()\r\n  altChunk.Id = altChunkId\r\n  \r\n  BookmarkStart.Parent.InsertAfterSelf(altChunk)\r\n \r\n\u2018 Inserisce un segnalibro per identificare la scheda appena aggiunta\r\ndim bkmStart as DocumentFormat.OpenXml.Wordprocessing.BookmarkStart= new DocumentFormat.OpenXml.Wordprocessing.BookmarkStart()\r\nbkmStart.Name=tipo_cont+numero\r\nbkmStart.Id=tipo_cont+numero\r\ndim bkmEnd as DocumentFormat.OpenXml.Wordprocessing.BookmarkEnd = new DocumentFormat.OpenXml.Wordprocessing.BookmarkEnd()\r\nbkmEnd.Id=tipo_cont+numero\r\nmyDoc.MainDocumentPart.Document.Body.InsertBefore(bkmStart, altChunk)\r\nmyDoc.MainDocumentPart.Document.Body.InsertAfter(bkmEnd, altChunk)\r\n \r\nelse\r\n  dim e as string=\u201dSegnalibro \u201c+segnalibri(l)+\u201d non presente nel documento. Non \u00e8 possibile aggiungere le relative schede\u201d+chr(13)\r\n  if not dbdollar(e,msg) then\r\n msg+=\u201dSegnalibro \u201c+segnalibri(l)+\u201d non presente nel documento. Non \u00e8 possibile aggiungere le relative schede\u201d+chr(13)\r\n  end if  \r\nend if\r\nend if\r\ncatch e as exception\r\n   msg+=e.message+chr(13)\r\nend try\r\nend if   \r\nelse\r\n  msg+=\u201dDocumento relativo a \u201c+items(i)+\u201d non esistente o non pubblicato\u201d+chr(13)\r\nend if   \r\nq.active=false\r\nif msg&lt;&gt;\u201d\u201d then\r\n  exit for\r\nend if\r\nnext\r\n \r\nif msg&lt;&gt;\u201d\u201d then\r\n  exit for\r\nend if\r\nnext\r\nif msg=\u201d\u201d then\r\n  mainPart.Document.Save()\r\nend if   \r\nmyDoc.Close()\r\n \r\nend if\r\nend if\r\n \r\nif msg&lt;&gt;\u201d\u201d then\r\n   form.alert(msg)\r\nelse\r\n   form.alert(\u201cIl documento \u00e8 stato aggiornato.\u201d)\r\nend if\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u2018 Grazie alla libreria OpenXml SDK di Microsoft, inclusa dalla release 2015.05b della piattaforma \u2018 web QWay\/QualiWare, \u00e8 possibile con poche righe di codice aggiungere il contenuto di uno o pi\u00f9 documenti Word all\u2019interno di un altro documento Word esistente. \u2018 Il seguente frammento di codice inserisce in un documento Word diversi documenti presenti in&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],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/24370"}],"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=24370"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/24370\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=24370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=24370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=24370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}