Datos de Access a plantilla Excel
Hola tengo una base de datos en access y necesito pasar algunos datos a una plantilla Excel. He conseguido pasar los datos de Access a Word con el siguiente código:
Dim miWord As New Word.Application
Dim miPlantilla As String
Dim miCarta As String
Dim rutaCartas As String
Dim rutaBD As String
Dim vcif As String
'Guardamos el registro de trabajo
DoCmd.RunCommand acCmdSaveRecord
'Cogemos la ruta de la base de datos
rutaBD = Application.CurrentProject.Path
'Creamos la ruta de la plantilla
miPlantilla = rutaBD & "\WordPlantilla\PlantillaCarta.dotx"
'Creamos la ruta donde guardar la carta
rutaCartas = rutaBD & "\WordCartas\"
'Creamos una instancia de Word
Set miWord = CreateObject("Word.Application")
miWord.Documents.Add miPlantilla
miWord.Visible = True
'Recuperamos el nombre y apellido del socio
Dim vNom As String
Dim vApell As String
Dim VDescrip As String
Dim VPrecio As String
Dim VUnidades As String
vcif = Me.CIF.Value
vNom = Me.PROVEEDOR.Value
vApell = Me.CÓDIGO.Value
VDescrip = Me.DESCRIPCIÓN.Value
VPrecio = Me.PRECIO.Value
VUnidades = Me.UNICADES.Value
'Rellenamos los marcadores del Word con la información
With miWord.ActiveDocument.Bookmarks
.Item("Wcif").Range.Text = CIF
.Item("Wproveedor").Range.Text = vNom
.Item("Wcodigo").Range.Text = vApell
.Item("Wdescripcion").Range.Text = VDescrip
.Item("Wprecio").Range.Text = VPrecio
.Item("Wunidades").Range.Text = VUnidades
End With
'Creamos el archivo final
'miCarta = rutaCartas & Format(Me.Fech.Value, "yymmdd") & "-" & vApell & ".doc"
miCarta = rutaCartas & vApell & ".doc"
miWord.ActiveDocument.SaveAs FileName:=miCarta, FileFormat:=wdFormatDocumentDefault
miWord.Quit
'Eliminamos la instancia de word
Set miWord = Nothing
'Lanzamos un mensaje de aviso
MsgBox "La carta se ha generado correctamente", vbInformation, "OK"
Este código funciona perfectamente en pero cuando he querido adaptarlo para que en lugar de word pase a excel me da errores y no funciona.
Dim oExcel As Object
Dim miPlantilla As String
Dim oBook As Object
Dim rutaBD As String
Dim oSheet As Object
DoCmd.RunCommand acCmdSaveRecord
rutaBD = Application.CurrentProject.Path
miPlantilla = rutaBD & "\WordPlantilla\libro.xlsx"
'Comenzar un nuevo libro de excel
Set oExcel = CreateObject("Excel.Application")
'Set oBook = oExcel.Workbooks.Add
oExcel.Workbooks.Add miPlantilla
oExcel.Visible = True
Dim vNom As String
Dim vApell As String
vNom = Me.PROVEEDOR.Value
vApell = Me.CODIGO.Value
'Add data to cells of the first worksheet in the new
Set oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = vNom
oSheet.Range("B2").Value = vApell
'Save the Workbook and salir
'oBook.SaveAs "\WordPlantilla\libro.xlsx"
oExcel.Quit
Alguien me podría ayudar?
Un saludo y gracias por todo