Results 1 to 9 of 9

Thread: [RESOLVED] Saving an Excel file using VB6

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    84

    Resolved [RESOLVED] Saving an Excel file using VB6


    Hi, I do have a goup of questions which I need help to resolve:
    1- The code below creates a pie chart using Excel and it displays fine, but with no legend at all, while the legend is displayed on the excel sheet?
    2- Is there any possibility to run that code without the need to save the excel file? If there is not, is it possible to save the file without asking the user to confirm the save or the replace?
    3- I want to delete the excel file created during the chart creation.
    4- for the deleteion i want to make the file path as a variable, is it possible to do that.
    Code:
    1.  Dim exapp As Excel.Application
    2.  Dim wb As New Excel.Workbook
    3.  Dim ws As Excel.Worksheet
    4.
    5.  Set exapp = CreateObject("Excel.Application")
    6.  Set wb = exapp.Workbooks.Add
    7.  Set ws = wb.Worksheets("Sheet1")
    8.  ws.Name = "Data"
    9.
    10. exapp.Visible = True
    11. With exapp
    12. exapp.Range("A1").CopyFromRecordset rsc
    13. .Charts.Add
    14.    .ActiveChart.ChartType = xlPie
    15.    .ActiveChart.SetSourceData Source:=Sheets("Data").Range("A1:A16"), 16.  PlotBy:=xlColumns
    17.    .ActiveChart.SeriesCollection(1).XValues = "=Data!R1C2:R16C2"
    18.    .ActiveChart.Location Where:=xlLocationAsNewSheet
    19.
    20.    .ActiveChart.PlotArea.Select
    21.    .Selection.Width = 400
    22.    .Selection.Height = 400
    23.    
    24.    
    25.    
    26.    End With
    27.
    28.    exapp.ActiveWorkbook.Save
    29.        If exapp.ActiveWorkbook.Saved = True Then
    30.        frmAllChart.Show
    31.        frmAllChart.OLE1.CreateEmbed exapp.ActiveWorkbook.Path_
    32.               & "\" & exapp.ActiveWorkbook.Name
    33.        frmAllChart.OLE1.Refresh
    34.        End If
    35.        
    36.    exapp.Quit
    37.    
    38.    
    39.  End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving an Excel file using VB6

    activeworkbook.fullname will return the complete path/filename

    to delete the workbook when you are finished
    vb Code:
    1. myfile = activeworkbook.fullname 'before closing workbook
    2. kill myfile' after closing workbook

    i don't see any reason why you need to save the workbook at all
    ThisWorkbook.Close SaveChanges:=False
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    84

    Re: Saving an Excel file using VB6


    Thank you very much westconn1 for the reply, it helped so much.
    Still one point missing which is the legend of the chart it do appear on the excel sheet but don't in the VB form.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving an Excel file using VB6

    the legend is on the worksheet not the chart sheet?

    i have no idea why this would be

    This example turns on the legend for Chart1 and then sets the legend font color to blue.

    Charts("Chart1").HasLegend = True
    Charts("Chart1").Legend.Font.ColorIndex = 5
    Remarks

    The chart legend isn’t visible unless the HasLegend property is True. If this property is False, properties and methods of the Legend object will fail.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    84

    Re: Saving an Excel file using VB6


    I did add the part you advised and changed the selection to ChartArea instead of PlotArea, but no success yet.
    Code:
    With ActiveChart
            .HasTitle = True
            .ChartTitle.Characters.Text = "All Cases"
            .HasLegend = True
            .Legend.Font.ColorIndex = 5
        End With
    Code:
    .ActiveChart.ChartArea.Select

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    84

    Re: Saving an Excel file using VB6


    It was only the OLE object need to be resized in order to display everything. Thank your very much westconn1.

  7. #7
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: [RESOLVED] Saving an Excel file using VB6

    Hey NightHeart, something I want to say about your code.
    You have used Active Chart and Workbook in your code. I also used this before Si_the_Geek pointed out its not good to use them. See the following

    http://www.vbforums.com/showthread.php?t=391665
    http://www.vbforums.com/showthread.php?t=485465 Post # 5

    So I suggest you change your code to avoid them

    IIF(Post.Rate > 0 , , )

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Saving an Excel file using VB6

    You have used Active Chart and Workbook in your code. I also used this before Si_the_Geek pointed out its not good to use them. See the following
    i totally agree should not use select, selection,active or activate, unless there is a specific reason for doing so

    address all ranges and sheets by name or setting them as ranges or worksheets as appropriate
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    84

    Re: [RESOLVED] Saving an Excel file using VB6

    Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width