Consulting

Results 1 to 5 of 5

Thread: Upon exit excel woorkbook save charts as picture

  1. #1
    VBAX Newbie
    Joined
    May 2018
    Posts
    2
    Location

    Upon exit excel woorkbook save charts as picture

    Hi guys

    Since i'm new here and new on coding with macros and VBA - i'm totaly noob i need help

    I'm gathering data on some things at home (bird watching, animals, ... ) basicaly on daily basis for a group that I'm in we then monitor movements.

    Since these charts are quite some (10) i want to export them when i exit excel workbook at once not one by one and clicking on chart etc.
    These pictures are then monitored on webpages so others in group can just open webpage not pptx or xlsx to see these pictures/charts.

    i found this and adapted to my needs

    Sub SaveChartAsPNG()
    Dim sFileName As String


    sFileName = ThisWorkbook.Path & "" & ActiveChart.Name & ".PNG"
    ActiveChart.Export Filename:=sFileName, FilterName:="PNG"
    End Sub

    now my question is - IS IT POSSIBLE TO EXPORT AT ONCE ALL CHARTS IN SINGLE WORKBOOK AS PICTURE WHEN EXITING/CLOSING WORKBOOK?

    When pressing X on excel workbook then after few secs (5-10s) pop-up would tell you eg - 10 charts exported as pictures! Have a nice day!

    Thank you for your replies guys

    Bye

    HB

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Last edited by mancubus; 05-16-2018 at 04:59 AM. Reason: code deleted. link posted.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    copy below code to ThisWorkbook code module
    it triggers when the workbook is closed

    i understand your workbook does not contain ant chart sheets
    make sure all chart objects have unique names. otherwise it overwrites previously saved file.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        Dim chObj As ChartObject
        Dim ws As Worksheet
        Dim fName As String
        Dim chObjCnt As Long
    
        ThisWorkbook.Save 'remove this line if you dont want to save the file automatically
        
        For Each ws In Worksheets
            For Each chObj In ws.ChartObjects
                fName = chObj.Name & ".png"
                ch.Export ThisWorkbook.Path & "\" & fName, "PNG"
                chObjCnt = chObjCnt + 1
            Next chObj
        Next ws
    
        MsgBox chObjCnt & " charts from worksheets exported at:" & vbLf & ThisWorkbook.Path, vbInformation, "Charts Export Result"
    
    End Sub
    adopted from post #9 of:

    http://www.excelforum.com/excel-prog...rts-title.html
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Newbie
    Joined
    May 2018
    Posts
    2
    Location
    WOW
    Thanx.
    Works.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    pls mark the thread as solved. (see item 3 in my signature)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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