Sub Excel()
Me.Refresh
On Error Resume Next
Dim DEPARTAMENTO As String, RESPONSABLE As String, numsolicitud As String, FECHA As String
Dim rutaPlantilla As String
Dim nuevoExcel As String
Dim miexcel As Object
Dim miHoja As Object
Dim msg As String
If IsNull(Me.RESPONSABLE) Then
MsgBox "El campo 'RESPONSABLE' es obligatorio.", vbOKOnly + vbExclamation, "ATENCIÓN"
Me.RESPONSABLE.SetFocus
Exit Sub
ElseIf IsNull(Me.FECHA) Then
MsgBox "El campo 'FECHA' es obligatorio.", vbOKOnly + vbExclamation, "ATENCIÓN"
Me.FECHA.SetFocus
Exit Sub
Else
Dim c1 As String
Dim c2 As String
Dim Descripcion1 As String
Dim Descripcion2 As String
'Cogemos los datos del formulario
DEPARTAMENTO = Nz(Me.DEPARTAMENTO.Value, "")
RESPONSABLE = Nz(Me.RESPONSABLE.Value, "")
numsolicitud = Nz(Me.numsolicitud.Value, "")
c1 = Nz(Me.c1.Value, "")
c2 = Nz(Me.c2.Value, "")
Descripcion1 = Nz(Me.Descripcion1.Value, "")
Descripcion2 = Nz(Me.Descripcion2.Value, "")
NOMBRE = Nz(Me.RESPONSABLE.Value, "")
FECHA = Nz(Me.FECHA.Value, "")
'Asignamos la ruta hasta la carpeta para el nuevo Excel
nuevoExcel = "S:\Solicitudes\Solicitudesnuevas\"
'Cogemos la ruta de la plantilla
rutaPlantilla = Application.CurrentProject.path & "\PLANTILLAS\Plantilla.xlsx"
Set miexcel = CreateObject("Excel.Application")
miexcel.Visible = False
miexcel.Workbooks.Open rutaPlantilla
'Cogemos la Hoja de la plantilla
Set miHoja = miexcel.activeworkbook.Worksheets("Hoja1")
'Operamos sobre la hoja
With miHoja
.Range("d6").Value = DEPARTAMENTO
.Range("d7").Value = RESPONSABLE
.Range("d8").Value = numsolicitud
.Range("c11").Value = c1
.Range("c12").Value = c2
.Range("d11").Value = Descripcion1
.Range("d12").Value = Descripcion2
End With
nuevoExcel = nuevoExcel & numsolicitud & ".xlsx"
If Dir(nuevoExcel) <> "" Then
If MsgBox("El archivo " & numsolicitud & ".xlsx" & " ya existe, deseas sobreescribirlo?", vbExclamation + vbYesNo, "Sobreescribir archivo") = vbNo Then
miexcel.Quit
Set miexcel = Nothing
Exit Sub
End If
End If
'Guardamos el Excel con otro nombre
miexcel.Application.DisplayAlerts = False
miexcel.activeworkbook.SaveAs nuevoExcel
miexcel.Application.DisplayAlerts = True
miexcel.activeworkbook.Close False
miexcel.Quit
Set miexcel = Nothing
MsgBox "El archivo " & (numsolicitud) & ".xlsx " & "se ha guardado en S:\Solicitudes\Solicitudesnuevas\", vbInformation + vbSystemModal, "Información"
End If
Dim milibro As Object
Dim esta As Boolean
Dim mifichero As String
esta = False
mifichero = nuevoExcel & numsolicitud & ".xlsx"
For Each milibro In miexcel.Application.Workbooks
If milibro.FullName = mifichero Then
esta = True
Else
esta = False
Exit For
End If
Next
If esta = True Then
miexcel.activeworkbook.Close False
miexcel.Quit
Set miexcel = Nothing
Else
miexcel.activeworkbook.Close False
miexcel.Quit
Set miexcel = Nothing
End If
End Sub