Codigo sencillo para manipular archivos Excel.
El primer paso es agregar la referencia del objeto Excel, que es el siguiente:
Microsoft Excel xx.x Object Library.
Donde xx.x es la versión
Luego el codigo:
Dim EXL As Excel.Application
Set EXL = New Excel.Application
Dim W As Excel.Workbook
Set W = EXL.Workbooks.Open("C:libro1.xls")
Dim S As Excel.Worksheet
Set S = W.Sheets("Hoja1")
MsgBox S.Range("A1").Value 'lee
S.Range("b2").Value = "graba"
Set S = Nothing
W.Save
W.Close
Set W = Nothing
Set EXL = Nothing
Funcion de Ejemplo:
Sub que recorre un grilla tipo MshFlexGrid para luego exportar a Excel.
Public Sub S_ExportarExcel(ByVal Grilla As MSHFlexGrid) 'Exportar en Excel
'Se debe hacer referencia a la libreria Microsoft Excel 11.0 object library, cualquier version
On Error GoTo Ocurrio_Error
Dim i As Integer
Dim J As Integer
Dim k As Integer 'guarda el numero de de filas invisibles que va encontrando
Dim L As Integer 'guarda el numero de de columnas invisibles que va encontrando
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.ActiveSheet
Screen.MousePointer = 11
i = 0
J = 0
k = 0
L = 0
For i = 0 To Grilla.Rows - 1
L = 0
Grilla.Row = i
If Grilla.CellHeight > 10 Then
For J = 0 To Grilla.Cols - 1
Grilla.Col = J
If Grilla.CellWidth > 10 Then
If Grilla.text = Empty Then
xlSheet.Cells(i + 1 - k, J + 1 - L).Value = ""
Else
xlSheet.Cells(i + 1 - k, J + 1 - L).Value = Grilla.text
End If
Else
L = L + 1
End If
Next
Else
k = k + 1
End If
Next
Grilla.Col = 0
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Screen.MousePointer = 0
Exit Sub
Ocurrio_Error:
Screen.MousePointer = 0
MsgBox "Ocurrio un Error en Proc. S_ExportarExcel: " & _
Err.Description, vbCritical, "ERROR EN TRASPASO A EXCEL"
End Sub
Fin!
|