Hola, que tal.
Estoy intentando mostrar en excel los resultados arrojados desde un DataGrid con este código:
Public Function Exportar_Excel(sOutputPath As String, DataGrid As Object) As Boolean
On Error GoTo Error_Handler
Dim o_Excel As Object
Dim o_Libro As Object
Dim o_Hoja As Object
Dim Fila As Long
Dim Columna As Long
Set o_Excel = CreateObject("Excel.Application")
Set o_Libro = o_Excel.Workbooks.Add
Set o_Hoja = o_Libro.Worksheets.Add
iCol = 0
For i = 0 To DataGrid.Columns.Count - 1
If DataGrid.Columns(i).Visible Then
iCol = iCol + 1
o_Hoja.Cells(1, iCol) = DataGrid.Columns(i).Caption
For j = 0 To n_Filas - 1
o_Hoja.Cells(j + 2, iCol) = _
DataGrid.Columns(i).CellValue(DataGrid.GetBookmark(j))
Next
End If
Next
o_Libro.Close True, sOutputPath
o_Excel.Quit
Call ReleaseObjects(o_Excel, o_Libro, o_Hoja)
Exportar_Excel = True
Exit Function
Error_Handler:
If Not o_Libro Is Nothing Then: o_Libro.Close False
If Not o_Excel Is Nothing Then: o_Excel.Quit
Call ReleaseObjects(o_Excel, o_Libro, o_Hoja)
If Err.Number <> 1004 Then MsgBox Err.Description, vbCritical
End Function
Private Sub ReleaseObjects(o_Excel As Object, o_Libro As Object, o_Hoja As Object)
If Not o_Excel Is Nothing Then Set o_Excel = Nothing
If Not o_Libro Is Nothing Then Set o_Libro = Nothing
If Not o_Hoja Is Nothing Then Set o_Hoja = Nothing
End Sub
Private Sub Command2_Click()
If Exportar_Excel(App.Path & "\libro1.xls", Datagrid1) Then
MsgBox " Datos exportados en " & App.Path, vbInformation
End If
End Sub
La cuestión es que si bien me genera el excel, me muestra sólo los encabezados de las columnas sin ningún registro.
Slds.