Pagina 1 di 1

[VB6] grafici in excel da VB6

MessaggioInviato: 27 nov 2009, 13:52
da aduri
Salve a tutti,
Vorrei avere un chiarimento su 2 punti di questo codice che ho attinto dalla rete.
Questa applicazione genera da vb6 (COL TASTO Sub Command1_Click() ) in excel un grafico a istogrammi con 3 serie da 84 punti ciascuna.
I miei problemi sono i seguenti:

Come si fa a scegliere in questa applicazione un grafico a linee anzichè a istogrammi? (ho provato con oChart.chartType = VtChChartType2dLine oppure oChart.chartType = xlLineMarkers ma mi da errore 13)

Come si fa a chiudere excel da vb6 (COL TASTO Sub Command2_Click() );
ho provato con i vari sistemi commmentati nel codice mi da errore 424 (object required)

Codice: Seleziona tutto
Private Sub Command1_Click()
   
    Dim oXL As Object        ' Excel application
    Dim oBook As Object      ' Excel workbook
    Dim oSheet As Object     ' Excel Worksheet
    Dim oChart As Object     ' Excel Chart
   
    Dim iRow As Integer      ' Index variable for the current Row
    Dim iCol As Integer      ' Index variable for the current Row
   
    Const cNumCols = 84      ' Number of points in each Series
    Const cNumRows = 3       ' Number of Series

   
    ReDim aTemp(1 To cNumRows, 1 To cNumCols)
   
    'Start Excel and create a new workbook
    Set oXL = CreateObject("Excel.application")
    Set oBook = oXL.Workbooks.Add
    Set oSheet = oBook.Worksheets.Item(1)
   
    ' Insert Random data into Cells for the three Series:
    Randomize Now()
    For iRow = 1 To cNumRows
       For iCol = 1 To cNumCols
          aTemp(iRow, iCol) = Int(Rnd * 50) + 1
       Next iCol
    Next iRow
    oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
   
    'Add a chart object to the first worksheet
   
    Set oChart = oSheet.ChartObjects.Add(70, 5, 450, 280).Chart
   
    'oChart.chartType = VtChChartType2dLine

    oChart.SetSourceData Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols)

    ' Make Excel Visible:
    oXL.Visible = True
    oXL.UserControl = True
       
End Sub

Private Sub Command2_Click()
'Kill "c:\documents and settings\administrator\documenti\cartel1.xls" effettivamente non è ancora salvata!!!
'oXL.Visible = False
'oXL.UserControl = False
'Excel.application.quit
'application.quit
'oXL.UserControl

End

End Sub


Ciao a tutti
Antonio

Re: [VB6] grafici in excel da VB6

MessaggioInviato: 27 nov 2009, 15:10
da c1b8
Per modificare il grafico portandolo ad un grafico a linee devi utilizzare la seguente:
Codice: Seleziona tutto
            oChart.ChartType = xlLine


Per la chiusura di excel alla pressione del secondo tasto ci sono 2 modifiche da effettuare.
La chiusura si effettua mediante chiamata del metodo Quit dell'oggetto Excel.Application, quindi si deve inserire nel codice del pulsante 2 la seguente:
Codice: Seleziona tutto
        oXL.Quit

Questo però non basta perché avrai un errore in quanto oXL è stato definito con uno scope errato per lo l'uso che se ne vuole fare.
Devi estrarre la dichiarazione di oXL dalla Sub Command1_Click() e portarlo a livello globale, o quantomeno a livello superiore cioè di form.

Il codice diventa quindi:
Codice: Seleziona tutto
    Dim oXL As Object        ' Excel application
   
    Private Sub Command1_Click()
        Dim oBook As Object      ' Excel workbook
        Dim oSheet As Object     ' Excel Worksheet
        Dim oChart As Object     ' Excel Chart
       
        Dim iRow As Integer      ' Index variable for the current Row
        Dim iCol As Integer      ' Index variable for the current Row
       
        Const cNumCols = 84      ' Number of points in each Series
        Const cNumRows = 3       ' Number of Series

       
        ReDim aTemp(1 To cNumRows, 1 To cNumCols)
       
        'Start Excel and create a new workbook
        Set oXL = CreateObject("Excel.application")
        Set oBook = oXL.Workbooks.Add
        Set oSheet = oBook.Worksheets.Item(1)
       
        ' Insert Random data into Cells for the three Series:
        Randomize Now()
        For iRow = 1 To cNumRows
           For iCol = 1 To cNumCols
              aTemp(iRow, iCol) = Int(Rnd * 50) + 1
           Next iCol
        Next iRow
        oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
       
        'Add a chart object to the first worksheet
       
        Set oChart = oSheet.ChartObjects.Add(70, 5, 450, 280).Chart
       
        oChart.ChartType = xlLine
       

        oChart.SetSourceData Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols)

        ' Make Excel Visible:
        oXL.Visible = True
        oXL.UserControl = True
           
    End Sub

    Private Sub Command2_Click()
        oXL.Quit
    End Sub

Re: [VB6] grafici in excel da VB6

MessaggioInviato: 27 nov 2009, 17:59
da aduri
Grazie
=D>
effettivamente a qualcosina, nel frattempo, c'ero arrivato anche io.
Comunque ora funziona tutto.
Come avrai capito sto facendo un datalogger che da VB6 genera un grafico excel.
Manca la parte di codice che acquisisce i dati dal PIC ma durante il week end ci lavoro.
Farà parte della mia Tesi sul provavalvole. :wink:
Mi rimane il problema HW di come generare la rampa HV per l'anodica.
Pensavo ad un PWM, un ampli analogico tipo da audio e trasformatore, ma forse è una pazzia.
Non vado avanti perché sarei OT in questa sezione

Ciao
Antonio