Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/08/2014, 12:26
t0n1
 
Fecha de Ingreso: noviembre-2010
Mensajes: 175
Antigüedad: 13 años, 5 meses
Puntos: 1
Exportar datagrid a excel

Hola, que tal.

Estoy utilizando este codigo para exportar los resultados de un datagrid y los muestre en una hoja de Excel y me arroja este error:

"No se encontró "". Compruebe la ortografía del nombre del archivo y si ubicación es correcta.

Este es el código:


Código:
Dim cnn         As Connection  
Dim rs          As Recordset  
' -- Variables para Excel  
Dim Obj_Excel   As Object  
Dim Obj_Libro   As Object  
Dim Obj_Hoja    As Object  
  

Private Sub exportar_Datagrid(Datagrid As Datagrid, n_Filas As Long)  
  
    On Error GoTo Error_Handler  
      
    Dim i   As Integer  
    Dim j   As Integer  
      

    Me.MousePointer = vbHourglass  
      
    If n_Filas = 0 Then  
        MsgBox "No hay datos para exportar a excel. Se ha indicado 0 en el parámetro Filas ": Exit Sub  
    Else  
          
  
        Set Obj_Excel = CreateObject("Excel.Application")  
 
        Set Obj_Libro = Obj_Excel.Workbooks.Open(Path)  
      

        Set Obj_Hoja = Obj_Excel.ActiveSheet  
     
        iCol = 0  
       
        For i = 0 To Datagrid.Columns.Count - 1  
            If Datagrid.Columns(i).Visible Then  
             
                iCol = iCol + 1  
               
                Obj_Hoja.Cells(1, iCol) = Datagrid.Columns(i).Caption  
                
                For j = 0 To n_Filas - 1  
                  
                    Obj_Hoja.Cells(j + 2, iCol) = _  
                    Datagrid.Columns(i).CellValue(Datagrid.GetBookmark(j))  
                Next  
            End If  
        Next  
          
      
        Obj_Excel.Visible = True  
          
       
        With Obj_Hoja  
            .Rows(1).Font.Bold = True  
            .Rows(1).Font.Color = vbRed  
           
            .Columns("A:Z").AutoFit  
        End With  
    End If  
  

    Set Obj_Hoja = Nothing  
    Set Obj_Libro = Nothing  
    Set Obj_Excel = Nothing  
      
    Me.MousePointer = vbDefault  
      
Exit Sub  
  

Error_Handler:  
  
    MsgBox Err.Description, vbCritical  
    On Error Resume Next  
  
    Set Obj_Hoja = Nothing  
    Set Obj_Libro = Nothing  
    Set Obj_Excel = Nothing  
    Me.MousePointer = vbDefault  
  
End Sub  

Private Sub Command1_Click()  
    Call exportar_Datagrid(DataGrid1, DataGrid1.ApproxCount)  
End Sub