Ecco qui!
codice:
Private Sub MyButtonSave_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles MyButtonSave.Click
Dim progress As New frm_progbar
progress.Visible = True
progress.Refresh()
Dim Custom_Doc_Prop As DocumentProperties
Dim Doc_Prop As Object
Dim Risp As MsgBoxResult
Dim Flag As Boolean
Dim IndirizzoWS As String
Dim objServizio As Object
Dim errore_conn As String = ""
Dim dati_con_dm As Hashtable
objServizio = New SrvWebSigMil.Service
'Recupero dal documento word il nome dell'utente connesso a SIgMil e l' ID_DOC
Try
Custom_Doc_Prop = CType(applicationObject.ActiveDocument.CustomDocumentProperties, Office.DocumentProperties)
Catch e As Exception
MsgBox("Non è possibile utilizzare il comando in quanto nessun documento risulta aperto.", MsgBoxStyle.Exclamation, "SIGMIL")
End Try
Doc_Prop = Custom_Doc_Prop
If (Custom_Doc_Prop.Count <> 0) Then
Utente_Conn = Doc_Prop.Item("UTENTE").Value()
ID_Doc = Doc_Prop.Item("ID_DOC").Value()
'CANCELLO LE PROPRIETà PERSONALIZZATE DEL DOCUMENTO WORD
Doc_Prop.Item("UTENTE").Delete()
Doc_Prop.Item("ID_DOC").Delete()
End If
If (Utente_Conn = "") And (ID_Doc = 0) Then
MsgBox("Non è possibile utilizzare questa funzione per una delle seguenti cause:" & vbLf & vbLf & "1) Il documento aperto non appartiene al sistema SIGMIL;" & vbLf & "2) Il documento appartiene al sistema SIGMIL ma è stato aperto in modalità di visualizzazione,in tal caso scegliere l'opzione 'blocca e scarica documento' per modificarlo e aggiornarlo.", MsgBoxStyle.Exclamation, "SIGMIL")
Exit Sub
End If
If Not File.Exists(WebService_Url) Then
Flag = sw_file_create()
If Flag = False Then
MsgBox("Si è verificato un errore nella creazione del file che contiene l'indirizzo del servizio web.", MsgBoxStyle.Critical, "SIGMIL - Utente connesso: " & Utente_Conn)
Exit Sub
Else
MsgBox("Impostare nel file 'C:\sigmil_plugin_url.txt' l'indirizzo del servizio web.", MsgBoxStyle.Exclamation, "SIGMIL - Utente connesso: " & Utente_Conn)
Exit Sub
End If
Else
IndirizzoWS = sw_file_read()
If IndirizzoWS = "" Then
MsgBox("Impostare nel file 'C:\sigmil_plugin_url.txt' l'indirizzo del servizio web.", MsgBoxStyle.Exclamation, "SIGMIL - Utente connesso: " & Utente_Conn)
Exit Sub
Else
Try
objServizio.url = IndirizzoWS
Catch e As Exception
MsgBox("L'indirizzo del servizio web impostato nel file 'C:\sigmil_plugin_url.txt' non è valido:verificare.", MsgBoxStyle.Exclamation, "SIGMIL - Utente connesso: " & Utente_Conn)
Exit Sub
End Try
End If
End If
'SALVO IL DOCUMENTO WORD sull'HD
applicationObject.ActiveDocument.SaveAs(tempfolder & "temp.doc")
'FACCIO UNA COPIA DEL DOCUMENTO WORD SALVATO COSI NON MI DA ERRORE (FILE IN USO DA UN ALTRO PROCESSO)
File.Copy(tempfolder & "temp.doc", tempfolder & "temp#.doc", True)
Dim FilePath As String
Dim FileNum As Integer
FilePath = tempfolder & "temp#.doc"
FileNum = FreeFile()
FileOpen(FileNum, FilePath, OpenMode.Binary, OpenAccess.Read)
Dim TotalFileSize As Long
Dim TotalBytesWritten As Long
Dim instance As EventLog
TotalFileSize = LOF(FileNum)
Dim bdata(TotalFileSize - 1) As Byte
Try
While (Not EOF(FileNum))
TotalBytesWritten = TotalFileSize
If TotalBytesWritten > 0 Then
If (TotalBytesWritten > UBound(bdata)) Then
TotalBytesWritten = UBound(bdata) + 1
Else
TotalFileSize = TotalBytesWritten
End If
FileGet(FileNum, bdata)
TotalBytesWritten = TotalFileSize - TotalBytesWritten
End If
End While
Catch e As Exception
MsgBox("Si è verificato un errore,non è possibile completare l'aggiornamento del documento.", MsgBoxStyle.Critical, "SIGMIL - Utente connesso: " & Utente_Conn)
Finally
FileClose(FileNum)
File.Delete(tempfolder & "temp#.doc")
End Try
Risp = MsgBox("Si conferma l'aggiornamento del presente documento?", MsgBoxStyle.YesNo, "SIGMIL PlugIn v1.0 - Utente connesso: " & Utente_Conn)
If Risp = MsgBoxResult.Yes Then
'Eseguo la connessione a DM tramite servizio web
Try
errore_conn = objServizio.Conn_Dm(ID_Doc, Utente_Conn, bdata, TotalFileSize)
Catch e As Exception
MsgBox("Il Servizio Web non è raggiungibile, controllare le impostazioni di rete e che l'indirizzo inserito nel file 'C:\sigmil_plugin_url.txt' sia corretto.", MsgBoxStyle.Exclamation, "SIGMIL - Utente connesso: " & Utente_Conn)
Exit Sub
End Try
End If
End Sub
Anche quando premo yes per confermare, l'applicazione riprende ad elaborare e si riblocca la progressbar fino a quando non termina l'elaborazione!