Hi,

trying to implement a self-updating excel file structure
each file contains an ID of file type
when opens, checks if a new version exists. If so, saves intself into a backup folder, opens template, saves on top of the original location.

If I execute the macro, all works fine.
However, this macro must run with the event "Workbook_Open" and for some reason, excel blocks the file??
Anybody found this problem?
any workaround?

'2017-06-27 / B.Agullo /
Public Function checkTemplateVersion(Optional ByVal dataPendingPublication As Boolean = False) As Boolean
'checks if current version is latest
    
    Dim Cn As ADODB.Connection
    Dim Rs As ADODB.Recordset
    Dim sqlSt As String
    Dim fA() As Variant
    Dim vA() As Variant
    Dim Pwb As Workbook
    Dim dPath As String
    Dim backupFileName As String
    Dim pCNSTws As Worksheet
    Dim sameFolder As Boolean
    Dim pFullPath As String
    Dim pFileName As String
    Dim pVersion As String
    Dim backupFolderPath As String
    Dim msg As String
    Dim folderError As Boolean
    Dim ii As Integer 'rewrite attemp
    
    
    checkTemplateVersion = False
    dPath = ThisWorkbook.fullname
    On Error GoTo errorNoControlado
    
    Set Cn = openADODBconnection(cadConn:=CNSTws.Range("CadenaConnexión"))
    If Cn Is Nothing Then GoTo release
    
    sqlSt = CNSTws.Range("SQLversiónPlantilla")
    sqlSt = replaceTags(sqlSent:=sqlSt, REFr:=CNSTws.Cells)
    
    Set Rs = openADODBrecordset(sqlSt:=sqlSt, Cn:=Cn)
    If Rs Is Nothing Then GoTo noneFound
    
    If Rs.RecordCount > 1 Then GoTo errorSQL
    
    '               0                1                           2
    fA = Array("Versión", "Ubicación de la plantilla", "Nombre del fichero")

    vA = Rs.GetRows(Fields:=fA)
    
    pVersion = vA(0, 0)
    
    If CNSTws.Range("VersiónPlantilla") >= pVersion Then GoTo release
    
    
    
    backupFileName = Format(now, "YYYYMMDD_HHmmss") & " " & ThisWorkbook.Name
    backupFolderPath = ThisWorkbook.Path & "\HISTORICO"
    
    
    
    
    pFullPath = vA(1, 0)
    'pFileName = getFileName(pFullPath)
    'pFileName = Replace(pFileName, ".xlsm", " " & pVersion & ".xlsm")
    
    
    
    'dPath = ThisWorkbook.Path & "\" & pFileName
    
    
    If dataPendingPublication Then
        sameFolder = isSamePath(ThisWorkbook.fullname, CNSTws.Range("Carpeta"), "02_FUENTES")
        
        msg = "Se ha detectado una nueva versión de la plantilla, " _
                & "pero el fichero actual está pendiente de publicación." _
                & "Quiere cerrar el fichero y publicar ahora? Tambien " _
                & "puede responder no, seguir trabajando y al finalizar publicar"
        
        
        If isAnswerOk(txt:=msg) Then
            Call mandarATebot
        Else
            GoTo userCancel
        End If
                
    Else
        msg = "Se ha detectado una nueva versión de la plantilla. " _
                & "Es obligatorio trabajar con la ultima versión. Desea actualizar ahora? " _
                & "El fichero actual se guardará en la subcarpeta HISTORICO con el nombre " & backupFileName
    End If
    
    If Not isAnswerOk(txt:=msg) Then
        GoTo userCancel
    End If
    
    Call createFolderIfNecessary(backupFolderPath, folderError)
    
    If folderError Then
        MsgBox "No se puede generar la subcarpeta HISTORICO. Guarde el fichero actual a una carpeta con permisos y vuelva a abrirlo"
        GoTo release
    End If
    
    'ThisWorkbook.SaveAs CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & backupFileName
    ThisWorkbook.SaveAs backupFolderPath & "\" & backupFileName
    
    On Error GoTo sinAccesoACarpetaPlantilla
    
    Set Pwb = openWorkbook(sFile:=pFullPath, readOnly:=True, RegisterSource:=False, switchFocus:=falso, returnFocus:=True, enableMacros:=False, isVisible:=False)
    
    If Pwb Is Nothing Then GoTo sinAccesoACarpetaPlantilla
        
    Set pCNSTws = getWorksheet(Pwb, "CNSTws", getByCodeName:=True)
    
    If Not pCNSTws Is Nothing Then
        pCNSTws.Range("Nombre") = CNSTws.Range("nombre")
    End If
        
    'On Error GoTo cannotSave
'    Err.Clear
'    On Error Resume Next
'    ii = 0
'    Do
'
'        If Err.Number <> 0 Then
'            Application.Wait now + TimeSerial(0, 0, 2)
'        End If
'
'        If ii = 5 Then
'            MsgBox "click ok"
'        End If
'
'        Kill dPath
'        Err.Clear
        
        On Error GoTo tryAgain
        Pwb.SaveAs filename:=dPath, ConflictResolution:=xlLocalSessionChanges
'        ii = ii + 1
'    Loop While Err.Number <> 0 And ii < 10
'
'    If ii = 10 Then
'        MsgBox "No se pudo guardar ni en 10 intentos" & ThisWorkbook.fullname & " " & dPath
'    End If
'
    
    
   ' If Not saveWorkbookIfPossible(Pwb, destinationPath:=dPath) Then GoTo cannotSave
    
    Pwb.Close savechanges:=False
    
    Set Pwb = openWorkbook(sFile:=dPath, readOnly:=False, RegisterSource:=False)
    
    Pwb.Activate
    
    checkTemplateVersion = True
    
    
    
release:
    Call closeADODBrecordset(Rs)
    Call closeADODBconnection(Cn)
    Set Pwb = Nothing
    
    If checkTemplateVersion Then
        ThisWorkbook.Close savechanges:=False
    End If
    
    
    Exit Function
    
userCancel:
    MsgBox "Se ha cancelado la actualización de plantilla."
    GoTo release
    
noneFound:
    MsgBox "No esta difinido el identificador de plantilla en el listado de plantillas. Contactar con Datamanagement I+D"
    GoTo release
    
errorSQL:
    MsgBox CNSTws.Range("PlantillaID") & " duplicado en Listado plantillas. Contactar con Datamanagement I+D"
    GoTo release
    
errorNoControlado:
    MsgBox "Error no controlado en ""checkTemplateVersion"". Contactar con Datamanagement I+D"
    GoTo release
    
sinAccesoACarpetaPlantilla:
    MsgBox "No tienes acceso a la plantilla. Contacta con Datamanagement I+D"
    GoTo release
    
cannotSave:
    MsgBox "No se puede sobreescribir el fichero " & dPath
    GoTo release
    
tryAgain:
    Application.Wait now + TimeSerial(0, 0, 2)
    If Not isAnswerOk("Try again?") Then GoTo release
    Resume

End Function