Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409

    Export chart as image from excel and load in access - resolution

    hi, i made this code to export as image a chart and then set and image control in a report to that image, but i'd like to have a better resolution
    here the code

    Code:
    Private Sub Report_Load()
    
        ' Set up variables
        Dim excelApp As Object
        Dim excelWorkbook As Object
        Dim excelChart As Object
        Dim accessApp As Object
        Dim imagePath As String
        
        ' Initialize Excel application
        Set excelApp = CreateObject("Excel.Application")
        excelApp.Visible = False ' You can set this to False if you don't want Excel to be visible
        
        ' Open the Excel workbook
        Set excelWorkbook = excelApp.Workbooks.Open(CurrentProject.Path & "\Grafici.xlsx") ' Update the path accordingly
        
        ' Set a reference to the chart you want to copy (replace "Chart1" with your actual chart name)
        Set excelChart = excelWorkbook.Sheets("CostIncomeGrafico").ChartObjects("Costincome").Chart
        excelChart.ChartArea.Copy
        ' Save the chart as an image file
        imagePath = CurrentProject.Path & "\Immagini Grafici\CostIncome.png" ' Update the path and filename accordingly
        Kill (imagePath)
        excelChart.Export FileName:=imagePath, FilterName:="PNG"
        
        'excelChart.Export FileName:=imagePath, FilterName:="PNG"  ', Width:=800, Height:=600 ' THIS NOT WORKED
        
        ' Set the Picture property of an Image control on the form to the saved image
        'Me.Report("Report-Analisi operazione").Controls("graficoimg").Picture = imagePath ' Replace "ImageControlName" with the actual name of your image control
        Me.GraficoIMG.Picture = imagePath
        '
     Clean up
        Set excelChart = Nothing
        Set excelWorkbook = Nothing
    
    
        Set accessApp = Nothing
    End Sub
    THX

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What would you think is better?

    Is the image always the same path and name?

    I have never used code to set Picture property. I use ControlSource property.
    Could eliminate that little bit of code by setting ControlSource property with an expression that references the image path: =CurrentProject.Path & "\Immagini Grafici\CostIncome.png"

    Or maybe build chart within Access.

    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Quote Originally Posted by diegomarino View Post
    hi, i made this code to export as image a chart and then set and image control in a report to that image, but i'd like to have a better resolution...
    So you're effectively copying the chart as a graphic into an access graphic control. Access will store your graphics, but it's not the best solution for historical data tracking

    Your better solution might be to:
    1. Import that excel data into an access table
    2. Build a chart on a form
    4. Set the datasource of your chart to the table (or query)
    5. Include an import button on your form that opens and imports your excel data anytime you want to append it to your access table.
    6. Add a couple of date range textboxes to your form also, and a button to refresh your query criteria and viola. Nifty chart tool for your excel data.

    The benefit would be that you could use the access chart to display any historical data you want by modifying the criteria of your query
    The cons are that Access charts are notoriously prickly - they are not as easy to work with as excel charts - an you'll have some learning to do ... but thankfully, this is a great forum with loads of knowledgeable people who can help if you ask specific and understandable questions.

  4. #4
    Join Date
    Apr 2017
    Posts
    1,680
    As it looks like the Excel charts to import into Acccess will be simply pictures, and having a lot of them in Access table will make it's size grow quickly and slow your Access app down as result, I advice another approach:
    Have some PDF printer (like CutePDF, which is freeware) installed, and print any charts you want the access from Access to some folder;
    In Access, have some form, where you can select a chart (or any other file) from any available folder, and save the link to it into field in currently active Access table record, along with another field which will contains some info helping user to identify the chart;
    Create an OnClick event for control displaying chart identify info, which will open the file linked to this table record, using your window's default program for type of linked file (Access itself simply forwards the open the link command to windows).

    You can link any types of files, and when you want to display the linked file, it works exactly in same way like when you click on it in File Explorer.

  5. #5
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    Click image for larger version. 

Name:	Screenshot 2023-10-05 103630.png 
Views:	20 
Size:	141.2 KB 
ID:	50856I should have been more detailed

    I have all my data in access, but i want better charts (i tried even Better Access charts) and the best option is excel.
    So i made a workbook called "charts" in which i linked my access query and made a chart based on them.

    The next step was to copy the (BEAUTIFUL ) chart from excel to access
    I did by picture property but also by control source property, and this is the result. It's not bad, but could be better

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Why not open the workbook from Access and look at the Excel chart?
    Or use a form with a bound object control?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    yes, i was thinking about that but i dunno how to do that. I never used the web control (or the new edge)

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Yes, chart is beautiful but simple. Access should be able to produce it as well.

    Who mentioned web control? Suggestion I see was to use bound object control.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If your reply is based on what I posted, then I don't see where a web control or M$ Edge comes into this. I said a bound object control but I should have said "unbound" object frame.
    https://learn.microsoft.com/en-us/of...ss.objectframe
    Maybe your dissatisfaction with the image approach was caused by the picture file type you used? You could try .bmp or .wmf or .jpg instead of .png
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    sorry, i knew you could link an excel in a web browser, i was barely aware of the objectframe and it is just what i looked for!
    thanks very much, now i have excel charts in my access

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If I was able to help then 1 more dram of Scotch for me!
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    i have just one question more

    this is the code on open

    Code:
    Private Sub Report_Open(Cancel As Integer)   
       DoCmd.SetWarnings False
       DoCmd.RunSQL ("DELETE * FROM CostIncomeGrafico")
       DoCmd.RunSQL ("INSERT INTO CostIncomeGrafico (CostiRicavi,Valore) SELECT CostiRicavi, Valore FROM qryCostincome")
       DoCmd.SetWarnings True
       Call AggiornaExcelGrafico(CurrentProject.Path & "\Grafici\CostIncome.xlsx")
       Me.CostIncomeObj.Requery
    
    
    End Sub
    and this is the function i made to refresh the excel (it has just one connection)

    Code:
    Public Function AggiornaExcelGrafico(WorkbookPath As String)   Dim xlx As Object
       Dim xlw As Object
       
       Set xlx = CreateObject("Excel.application")
       Set xlw = xlx.Workbooks.Open(WorkbookPath)
    
    
       
       xlx.DisplayAlerts = False
       xlw.RefreshAll
       xlw.Save
       xlw.Close 'SaveChanges:=True
       xlx.DisplayAlerts = True
    
    
       Set xlw = Nothing
       xlx.Quit
       Set xlx = Nothing
    
    
    
    End Function
    it does not refresh anything, i have to open the excel and click "refreshall" button.

    Why that?

  13. #13
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    hi, i took another approach.

    this is the button on the form i use to open the form with objectframe

    Code:
    Private Sub MappaBTN_Click()   Dim xlx As Object, xlw As Object, Xls As Object, xlc As Object
       Dim db As DAO.Database, rst As DAO.Recordset
       Dim Worksheetname As String
    
    
       DoCmd.SetWarnings False
    
    
       DoCmd.RunSQL ("DELETE * FROM OcfXRegione")
       DoCmd.RunSQL ("INSERT INTO OcfXRegione (Regione, Conteggio) SELECT Regione,Conteggio FROM qryOcfXregione")
    
    
       Set db = CurrentDb
       On Error Resume Next
       Set xlx = GetObject(, "Excel.application")
       If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.application")
       End If
       Err.Clear
       On Error GoTo 0
       xlx.Visible = False ' se setto vero mi apre il file excel in questione
    
    
       Set rst = db.OpenRecordset("OcfXregione")
       If rst.RecordCount > 0 Then
    
    
    
    
          Set xlw = xlx.Workbooks.Open("C:\Users\diegomarino\OneDrive - Indipendente\Lavoro\Informatica\Gestionale\FrontEnd\Grafici\CFNumeroMappa.xlsx")
          Set Xls = xlw.Worksheets("qryOcfXRegioni")
    
    
          Set xlc = Xls.Range("A2")
          Do While Not rst.EOF Or rst.BOF
             xlc = rst.Fields(0).Value
             xlc.Offset(0, 1).Value = rst.Fields(1).Value
             Set xlc = xlc.Offset(1, 0)
             Debug.Print (xlc)
             rst.MoveNext
          Loop
          xlx.DisplayAlerts = False
    
    
    
    
          'xlw.RefreshAll
    '      With xlw.grafico1
    '        .SetSourceData Source:=Sheets("qryOCFXRegione").Range("$A$1:$b$21")
    '        'other chart formatting code that all works fine
    '      End With
          DoEvents
          xlw.Save
          xlw.Close SaveChanges:=True
          xlx.DisplayAlerts = True
       End If
    
    
       rst.Close
       Set rst = Nothing
       Set xlc = Nothing
       Set Xls = Nothing
       Set xlw = Nothing
       xlx.Quit ' se esiste l'applicazione excel la chiude
       Set xlx = Nothing
    
    
    
    
       DoCmd.OpenForm "AnalisiOCFxregione"
    
       DoCmd.SetWarnings True
    then, i have a combobox in which i can filter data (that's very simple data, just 20 rows max with two columns)

    Code:
    Private Sub CercaBancaCB_AfterUpdate()
    
    Dim xlx As Object, xlw As Object, Xls As Object, xlc As Object, oChart As Object
    Dim db As DAO.Database, rst As DAO.Recordset
    Dim Worksheetname, WorkBookName As String
    Dim i As Integer
    
    
       
       DoCmd.SetWarnings False
       DoCmd.RunSQL ("DELETE * FROM ocfXregione")
       DoCmd.RunSQL ("INSERT INTO OcfXRegione (REgione, conteggio) " & vbCrLf & _
                     "SELECT Regione, conteggio  " & vbCrLf & _
                     "FROM( " & vbCrLf & _
                     "SELECT Comuni.Regione, Count(IscrittiOCF.regione) AS Conteggio " & vbCrLf & _
                     "FROM Comuni INNER JOIN (OCFComuni INNER JOIN IscrittiOCF ON OCFComuni.COMUNE = IscrittiOCF.COMUNE) ON Comuni.IDComune = OCFComuni.ComuneID " & vbCrLf & _
                     "WHERE (iscrittiocf.regione <> """" AND iscrittiocf.regione <> ""\n"") AND (IscrittiOCF.DEnominazione_soggetto_abilitato = FORMS!Analisiocfxregione!Cercabancacb or FORMS!Analisiocfxregione!Cercabancacb is null) " & vbCrLf & _
                     "GROUP BY Comuni.Regione)")
       Me.Requery
      
    
    
       Set db = CurrentDb
       On Error Resume Next
       Set xlx = GetObject(, "Excel.application")
       If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.application")
       End If
       Err.Clear
       On Error GoTo 0
       xlx.Visible = False ' se setto vero mi apre il file excel in questione
    
    
       Set rst = db.OpenRecordset("OcfXregione")
       If rst.RecordCount > 0 Then
          rst.MoveLast
          rst.MoveFirst
          WorkBookName = "C:\Users\diegomarino\OneDrive - Indipendente\Lavoro\Informatica\Gestionale\FrontEnd\Grafici\CFNumeroMappa.xlsx"
          
          Set xlw = xlx.Workbooks.Open(WorkBookName)
          xlw.RefreshAll
          Set Xls = xlw.Worksheets("qryOCFXRegioni")
               
          Set xlc = Xls.Range("A2")
          Do While Not rst.EOF Or rst.BOF
             xlc = rst.Fields(0).Value
             xlc.Offset(0, 1).Value = rst.Fields(1).Value
             Set xlc = xlc.Offset(1, 0)
             Debug.Print (xlc)
             rst.MoveNext
          Loop
    
    
          Set xlc = Xls.Range("A" & rst.RecordCount + 2)
          i = rst.RecordCount + 2
          Do Until i = 22
             xlc = ""
             xlc.Offset(0, 1).Value = ""
             Set xlc = xlc.Offset(1, 0)
             i = i + 1
          Loop
          
          
          xlx.DisplayAlerts = False
    
    
          xlw.Close SaveChanges:=True
          xlx.DisplayAlerts = True
       End If
    
    
    
    
       rst.Close
       Set rst = Nothing
       Set xlc = Nothing
       Set Xls = Nothing
       Set xlw = Nothing
       xlx.Quit ' se esiste l'applicazione excel la chiude
       Set xlx = Nothing
       DoCmd.SetWarnings True
    
    
    End Sub

    everything work perfectly, except the chart sometimes is not refreshing in objectframe, and when i open the excel it tooks few seconds to show the chart even in excel. Why that?

    i'd liek to stick with this method that's the best i found

  14. #14
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    GOT IT! i need just a SLEEP bifore closing the excel application!

  15. #15
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Thanks for posting your progress and solution. It may help someone in the future.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 12-25-2015, 02:41 PM
  2. Replies: 2
    Last Post: 04-06-2015, 12:08 PM
  3. Image Load
    By RayMilhon in forum Forms
    Replies: 2
    Last Post: 12-03-2014, 06:30 PM
  4. Export Access Chart to Excel using VBA code
    By ADFC in forum Programming
    Replies: 8
    Last Post: 04-09-2014, 03:05 PM
  5. Bitmap image won't export to excel?
    By C6067 in forum Import/Export Data
    Replies: 2
    Last Post: 03-04-2013, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums