Public Function AbreWorbook(sWorkbook As String, Optional CrearWorkbook As Boolean = False, Optional Visible As Boolean = False) As Boolean
'PARAMETROS
' sWorkbook = cadena que indica la ruta donde se encuentra el workbook
' CrearWorkbook = True si se quiere crear un Workbook en blanco
' Visible = True si se quiere ver el Excel
' La función devuelve True si todo se ha echo correctamente
On Error Resume Next
Dim bolEnWorkbooks As Boolean
Dim bolAux As Boolean
Dim bolExiste As Boolean
Dim worAux As Workbook
Dim strAux As String
bolAux = True
gApp.ScreenUpdating = Visible
gApp.Interactive = Visible
bolEnWorkbooks = False
bolExiste = False
strAux = Dir(sWorkbook)
If strAux <> "" Then
bolExiste = True
For Each worAux In gApp.Workbooks
If worAux.Name = strAux Then
bolEnWorkbooks = True
Exit For
End If
Next
End If
If bolExiste Then
'Existe y lo activamos
If bolEnWorkbooks Then
gApp.Workbooks(strAux).Activate
Set gLibro = gApp.ActiveWorkbook
Else
Set gLibro = gApp.Workbooks.Open(sWorkbook)
End If
Else 'NO EXISTE
If CrearWorkbook Then
gApp.Workbooks.Add
gApp.ActiveWorkbook.SaveAs sWorkbook
Set gLibro = gApp.ActiveWorkbook
Else
If err.Number <> 0 Then
err.Clear