Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Problema Exportando a Excel

Estas en el tema de Problema Exportando a Excel en el foro de Visual Basic clásico en Foros del Web. Tengo este codigo para exportar el contenido de un msflexigrid a excel, pero en un principio me lo exportaba y ahoro no se que toque ...
  #1 (permalink)  
Antiguo 06/04/2010, 11:15
Avatar de gasafonso  
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 15 años, 8 meses
Puntos: 1
Pregunta Problema Exportando a Excel

Tengo este codigo para exportar el contenido de un msflexigrid a excel, pero en un principio me lo exportaba y ahoro no se que toque que no me lo exporta

Código vb:
Ver original
  1. Public Function cierreaExcel(grilla As MSFlexGrid) As Boolean
  2.     Dim i As Long, j As Long
  3.     Dim objExcel As Object
  4.     Dim objWorkbook As Object
  5.     On Error Resume Next ' por si se cierra Excel antes de cargar los datos
  6.    Set objExcel = CreateObject("Excel.Application")
  7.     objExcel.Visible = False
  8.     Set objWorkbook = objExcel.Workbooks.Add
  9.     For i = 0 To grilla.Rows - 1
  10.        grilla.Row = i
  11.         For j = 1 To grilla.Cols - 1
  12.             grilla.Col = j
  13.             If i = 0 Then
  14.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Interior.Color = &H80C0FF
  15.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Font.Bold = True
  16.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Value = grilla.text
  17.             Else
  18.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Style = "Comma"
  19.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).NumberFormat = "_-* #,##0.0_-;-* #,##0.0_-;_-* ""-""??_-;_-@_-"
  20.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
  21.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).NumberFormat = "_-* #,##0_-"
  22.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Interior.Color = &HC0FFFF
  23.                 objWorkbook.ActiveSheet.Cells(i + 1, j + 1).Value = grilla.text
  24.             End If
  25.         Next
  26.     Next
  27.        
  28.     objExcel.Cells.Select
  29.     objExcel.Selection.EntireColumn.AutoFit    ' Ancho de columna
  30.    objWorkbook.ActiveSheet.Range("b1:i59").Borders().LineStyle = 1
  31.     objWorkbook.ActiveSheet.Range("A59:i59").Font.Bold = True
  32.     'objWorkbook.ActiveSheet.Columns("K:K").ColumnWidth = 13.71
  33.    objExcel.Range("b1").Select
  34.     objExcel.Visible = True
  35.    
  36.     objExcel.ActiveWindow.SelectedSheets.PrintPreview ' Previsualizar informe
  37.    Set objWorkbook = Nothing
  38.     Set objExcel = Nothing
  39. End Function


gracias
  #2 (permalink)  
Antiguo 06/04/2010, 13:06
 
Fecha de Ingreso: noviembre-2006
Mensajes: 227
Antigüedad: 17 años, 6 meses
Puntos: 6
Respuesta: Problema Exportando a Excel

Haber si te sirve este codigo para exportar el contenido de una grilla a Excel

En un Modulo coloca este codigo tal cual
Código:
Public Sub ExportarGrid(Grid As MSHFlexGrid, FileName As String, FileType)
Dim i As Long
Dim j As Long

On Error GoTo ErrHandler

    'Let's put a HourGlass pointer for the mouse
    Screen.MousePointer = vbHourglass

    If FileType = 1 Then 'Exporta a excel
        'Gimme the workbook
        Dim wkbNew As Excel.Workbook
        'Gimme the worksheet for the workbook
        Dim wkbSheet As Excel.Worksheet
        'Gimme the range for the worksheet
        Dim Rng As Excel.Range

        'Does the file exist?
        If Dir(FileName) <> "" Then
            'Kill it boy!
            Kill FileName
        End If

On Error GoTo CreateNew_Err

    'Let's create the workbook kid!
    Set wkbNew = Workbooks.Add
    wkbNew.SaveAs FileName

    'Add a WorkPage
    Set wkbSheet = wkbNew.Worksheets(1)

    'Set the values in the range
    Set Rng = wkbSheet.Range("A1:" + Chr(Grid.Cols + 64) + CStr(Grid.Rows))
        For j = 0 To Grid.Cols - 1
            For i = 0 To Grid.Rows - 1
                If Val(j) <> 3 Then
                    Rng.Range(Chr(j + 1 + 64) + CStr(i + 1)) = Grid.TextMatrix(i, j)
                    Else
                    Rng.Range(Chr(j + 1 + 64) + CStr(i + 1)) = Val(Replace(Grid.TextMatrix(i, j), ",", "."))
                End If
            Next
        Next

    'Close and save the file
    wkbNew.Close True

GoTo NoErrors
CreateNew_Err:
    'Stop the show!
    wkbNew.Close False
    Set wkbNew = Nothing
    Resume ErrHandler

    Else 'Export to text
    Dim Fs As Variant
    Dim A As Variant

    'I know, the File # sounds smarter, but, I like weird things :) !
On Error GoTo ErrHandler
    Set Fs = CreateObject("Scripting.FileSystemObject")
    Set A = Fs.CreateTextFile(FileName, True)
    Dim Line As String
        For j = 0 To Grid.Rows - 1
            For i = 0 To Grid.Cols - 1
                Line = Line + Grid.TextMatrix(i, j) + vbTab
            Next
            A.WriteLine (Line)
            Line = ""
        Next
    A.Close

End If

NoErrors:
    'Gimme the default mouse pointer dude!
    Screen.MousePointer = vbDefault
    MsgBox "Los datos fueron exportados Correctamente", vbOKOnly, "Finalizado"
    Exit Sub

ErrHandler:
    'Gimme the default mouse pointer dude!
    Screen.MousePointer = vbDefault
    MsgBox "¡Vaya!, ¡Vaya!, ¡Vaya! lo lamento hoy ¡No puedo exportar Tu fichero!", vbOKOnly, "Error"
    Exit Sub
End Sub
Y lo llamas desde un Command Button asi:
Código:
Private Sub CmdExportar_Click()
On Error GoTo ErrHandler
    CD.Filter = "Excel File(*.xls)|*.xls|Text File (*.txt)|*.txt"
    CD.FilterIndex = 1
    CD.ShowSave
    ExportarGrid NombredeTuGrilla, CD.FileName, CD.FilterIndex
ErrHandler:
End Sub
sobra decir que tienes que agregar el componente Commondialog que en mi caso lo he llamado CD

Espero te sirva
  #3 (permalink)  
Antiguo 06/04/2010, 18:54
Avatar de gasafonso  
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 15 años, 8 meses
Puntos: 1
Respuesta: Problema Exportando a Excel

Hola SalomonSab, no me lo exporta a excel, acordate que no es MSHFLEXIGRID es MSFLEXIGRID .

Me deja el cursor con el reloj de arena y no me lo exporta
  #4 (permalink)  
Antiguo 08/04/2010, 07:11
 
Fecha de Ingreso: noviembre-2006
Mensajes: 227
Antigüedad: 17 años, 6 meses
Puntos: 6
Respuesta: Problema Exportando a Excel

y si cambias esta linea
Código:
Public Sub ExportarGrid(Grid As MSFlexGrid, FileName As String, FileType)
  #5 (permalink)  
Antiguo 08/04/2010, 09:49
Avatar de culd  
Fecha de Ingreso: noviembre-2003
Mensajes: 959
Antigüedad: 20 años, 6 meses
Puntos: 19
Respuesta: Problema Exportando a Excel

Seguis con el copy/paste

Que barbaro... Estos chicos che...
  #6 (permalink)  
Antiguo 08/04/2010, 11:45
Avatar de gasafonso  
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 15 años, 8 meses
Puntos: 1
Respuesta: Problema Exportando a Excel

No, le cambie esa linea y no me responde.


Culd !!! y como tengo que hacer ?
  #7 (permalink)  
Antiguo 08/04/2010, 13:07
Avatar de culd  
Fecha de Ingreso: noviembre-2003
Mensajes: 959
Antigüedad: 20 años, 6 meses
Puntos: 19
Respuesta: Problema Exportando a Excel

Cita:
Iniciado por gasafonso Ver Mensaje
Culd !!! y como tengo que hacer ?
Sobre que estas haciendo copy/paste de codigo que encontraste en internet, por lo menos usa la cabeza y lee linea por linea para encontrar el problema, o por lo menos identificar como funciona la sub accion y tratar de mejorarla. POR LO MENOS

Agota toda tu cabeza primero.
  #8 (permalink)  
Antiguo 08/04/2010, 13:31
Avatar de gasafonso  
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 15 años, 8 meses
Puntos: 1
Respuesta: Problema Exportando a Excel

Son cosas que yo nunca habia usado, y me lo pasron en otro foro, pero tuve que reinstalar de nuevo el VB 6 y dejo de funcionar


ACORDATE CULD QUE SOY PRINCIPIANTE ( AUTODIDACTA)
  #9 (permalink)  
Antiguo 09/04/2010, 07:43
 
Fecha de Ingreso: noviembre-2006
Mensajes: 227
Antigüedad: 17 años, 6 meses
Puntos: 6
Respuesta: Problema Exportando a Excel

Entonces aplica el proceso paso a paso posicionate en la linea donde haces click para hacer la exportación y presiona F9 corres tu programa y cuando haces click te manda a ese proceso y te vas con F8 hasta llegar donde se detiene y ahi te dara cuenta cual seria el problema
  #10 (permalink)  
Antiguo 10/04/2010, 06:30
Avatar de gasafonso  
Fecha de Ingreso: septiembre-2008
Mensajes: 357
Antigüedad: 15 años, 8 meses
Puntos: 1
Respuesta: Problema Exportando a Excel

No ya probe linea por linea y no pasa nada.

Tiene que ser algo de algun driver o configurarcion por que antes me lo hacia perfecto

Etiquetas: excel
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:53.