I attach a base document, two document to insert, a image to be inserted and the code simplified treatment is failing us randomly.
We have three environments (development, testing and production) and only gave us this error in the production environment. The development server has a processor with two cores, the test server has a processor with one core and the production is a multiprocessor server (8 processors).
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const cDOCVARIABLE_FECHACAST As String = "FIRMA_FECHACAST"
Const cDOCVARIABLE_FECHAEUSK As String = "FIRMA_FECHAEUSK"
Dim objBuilder As DocumentBuilder
Dim objBookmark As Bookmark
Dim cont As Integer
Dim Buscar As DictionaryEntry
Dim blnExiste As Boolean
Dim strNUMFirma As String
Dim strFechaCast As String
Dim strFechaEusk As String
Dim objDocDest As Document
Dim shapesDocumentos As NodeCollection
Dim Documento As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\O028.doc"
Dim objWordDocumento As Document
Dim strFileDestDoc As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\FIR030_2.DOC"
Dim RutaFirma As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\S10035PA.JPG"
Try
InsertarBloqueMultiplesFirmas("C:\PROYECTOS\PruebaASPOSE\ErrorSave", "FIR000", _
Documento)
strNUMFirma = "FIRMA1"
objWordDocumento = New Aspose.Words.Document(Documento)
fProtegerDocumento(objWordDocumento, False)
objBookmark = objWordDocumento.Range.Bookmarks(strNUMFirma)
objWordDocumento.Variables.Add("CARGCAST", Nothing)
objWordDocumento.Variables.Add("CARGEUSK", Nothing)
objWordDocumento.Variables.Add("DELSUSTC", Nothing)
objWordDocumento.Variables.Add("DELSUSTE", Nothing)
objWordDocumento.Variables.Add("SERVICIOCAST", Nothing)
objWordDocumento.Variables.Add("SERVICIOEUSK", Nothing)
objWordDocumento.Variables.Add("FIRMA_FECHACAST", "Bilbao, a 27 de Julio de 2010")
objWordDocumento.Variables.Add("FIRMA_FECHAEUSK", "Bilbon, 2010(e)ko Uztailaren 27(e)an")
objDocDest = New Document(strFileDestDoc)
InsertarDocumento(strNUMFirma, objWordDocumento, objDocDest)
ActualizarMarcadores(objWordDocumento.Range.Bookmarks)
objBuilder = New DocumentBuilder(objWordDocumento)
If Not objBuilder.MoveToBookmark("ImagenFirma1") Then
Throw New Exception("En el bloque " & Documento & " no existe el marcador 'ImagenFirma1'")
End If
If Trim(RutaFirma) <> "" Then
objBuilder.InsertImage(RutaFirma)
Else
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
End If
objWordDocumento.Range.Bookmarks.Remove("ImagenFirma1")
shapesDocumentos = objWordDocumento.GetChildNodes(NodeType.Shape, True, False)
For Each shape As Shape In shapesDocumentos
If InStr(shape.GetText, "FECACUSE") > 0 Then
shape.Remove()
End If
Next shape
fProtegerDocumento(objWordDocumento, True)
objWordDocumento.Save(Documento, SaveFormat.Doc)
Catch Ex As Exception
Dim strError As String
strError = vbTab & "Error: " & Ex.Message
logErrores(strError)
Throw New Exception(Ex.Message)
Finally
objWordDocumento = Nothing
objBuilder = Nothing
objBookmark = Nothing
Buscar = Nothing
objDocDest = Nothing
shapesDocumentos = Nothing
End Try
End Sub
Friend Sub ActualizarMarcadores(ByVal bkMarcadores As BookmarkCollection)
Try
For Each bkMarcadorActual As Bookmark In bkMarcadores
If bkMarcadorActual.Name.IndexOf("ASPOSE") > -1 Then
ActualizarContenidoMarcador(bkMarcadorActual)
End If
Next
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Finally
End Try
End Sub
Friend Sub ActualizarContenidoMarcador(ByVal bkMarcador As Bookmark)
Dim bookmarkStart As BookmarkStart = bkMarcador.BookmarkStart
Dim bookmarkEnd As BookmarkEnd = bkMarcador.BookmarkEnd
Dim nodo As Node = bookmarkStart
Dim doc As Document = nodo.Document
Try
Do While Not nodo Is bookmarkEnd
If nodo.NodeType = NodeType.Paragraph Then
Try
nodo.Range.UpdateFields()
Catch ExUpdate As Exception
'Si el nodo contiene un campo "IF" se produce un error.
'Se sigue comprobando el resto de nodos.
End Try
End If
nodo = nodo.NextPreOrder(doc)
Loop
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Finally
End Try
End Sub
Friend Sub logErrores(ByVal cadena As String, _
Optional ByVal strInfoAdicional As String = "")
Try
Dim strError As String
Dim strRutaFichero As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\Log_" & Format(Today).Replace("/", "") & ".txt"
Dim strFichero As New System.IO.StreamWriter(strRutaFichero, True)
strError = CStr(Now) & vbCrLf & CStr(cadena)
If strInfoAdicional <> "" Then strError &= vbTab & "Informacion Adicional : " & strInfoAdicional
strFichero.WriteLine(strError & vbCrLf)
strFichero.Close()
Catch ex As Exception
Throw New Exception("Error en la Gestión de Logs : " & ex.Message.ToString)
End Try
End Sub
Friend Sub InsertarDocumento(ByVal strMarcador As String, ByVal dstDoc As Document, ByVal srcDoc As Document)
Dim objBuilder As DocumentBuilder
Dim insertAfterNode As Node
Dim dstStory As CompositeNode
Dim srcSection As Section
Try
objBuilder = New DocumentBuilder(dstDoc)
objBuilder.MoveToBookmark(strMarcador)
objBuilder.Writeln()
insertAfterNode = objBuilder.CurrentParagraph.PreviousSibling
dstStory = insertAfterNode.ParentNode
While (Not srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
srcDoc.LastSection.Body.LastParagraph.Remove()
End While
For Each srcSection In srcDoc.Sections
Dim srcNode As Node
For Each srcNode In srcSection.Body
Dim newNode As Node = dstDoc.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
Next
Next
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
objBuilder = Nothing
insertAfterNode = Nothing
dstStory = Nothing
srcSection = Nothing
End Try
End Sub
Friend Sub InsertarBloqueMultiplesFirmas(ByVal pstrRutaBloque As String, _
ByVal pstrBloque As String, _
ByVal pstrDocumento As String)
Dim objWordDocumento As Document
Dim strBloque As String
Dim strMarcaFirma As String
Dim objDocDest As Document
Try
strBloque = pstrRutaBloque & "\" & pstrBloque & ".doc"
strMarcaFirma = "FIRMA"
objWordDocumento = New Document(pstrDocumento)
fProtegerDocumento(objWordDocumento, False)
objDocDest = New Document(strBloque)
InsertarDocumento(objWordDocumento.Range.Bookmarks(strMarcaFirma).BookmarkStart.ParentNode, objDocDest)
fProtegerDocumento(objWordDocumento, True)
objWordDocumento.Save(pstrDocumento)
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
objWordDocumento = Nothing
objDocDest = Nothing
End Try
End Sub
Friend Sub InsertarDocumento(ByVal insertAfterNode As Node, _
ByVal srcDoc As Document)
Dim dstStory As CompositeNode
Dim importer As NodeImporter
Dim para As Paragraph
Dim newNode As Node
Try
If Not ((insertAfterNode.NodeType = NodeType.Paragraph) OrElse (insertAfterNode.NodeType = NodeType.Table)) Then
Throw New ArgumentException("El nodo destino debe ser de tipo paragraph o table.")
End If
dstStory = insertAfterNode.ParentNode
importer = New NodeImporter(srcDoc, insertAfterNode.Document, _
ImportFormatMode.KeepSourceFormatting)
For Each srcSection As Section In srcDoc.Sections
For Each srcNode As Node In srcSection.Body
If (Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes) Then
Exit For
End If
newNode = importer.ImportNode(srcNode, True)
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
Next srcNode
Next srcSection
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
End Try
End Sub
Friend Sub fProtegerDocumento(ByVal objDocProteger As Aspose.Words.Document, _
ByVal blnProteger As Boolean, _
Optional ByVal pblnPorSecciones As Boolean = False)
Dim strClave As String
Dim strSqlCon As String
Dim rsdClave As IDataReader
Dim blnCerrarConexion As Boolean
Try
strClave = "PRUEBA"
objDocProteger.AcceptAllRevisions()
objDocProteger.TrackRevisions = False
objDocProteger.RemoveSmartTags()
If blnProteger = True Then
If pblnPorSecciones Then
objDocProteger.Protect(Aspose.Words.ProtectionType.AllowOnlyFormFields, strClave)
Else
objDocProteger.Protect(Aspose.Words.ProtectionType.ReadOnly, strClave)
End If
Else
objDocProteger.Unprotect()
End If
Catch Ex As Exception
Dim strError As String
strError = vbTab & "Error: " & Ex.Message
logErrores(strError)
Throw New Exception(Ex.Message)
Finally
If Not rsdClave Is Nothing Then
rsdClave.Close()
rsdClave.Dispose()
End If
rsdClave = Nothing
If Not objDocProteger Is Nothing Then
objDocProteger = Nothing
End If
End Try
End Sub