Results 1 to 4 of 4

Thread: [RESOLVED] Exporting Image From Excel

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Resolved [RESOLVED] Exporting Image From Excel

    Hello

    This code works in VBA

    http://www.vbforums.com/showthread.php?t=538529

    However, when I try it from vb.net, I get the following error. (Image attached)

    The code that I am trying is as follows

    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim xlApp As Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            Dim olobj As Excel.Shape
            Dim xlCharts As Excel.ChartObjects
            Dim myChart As Excel.ChartObject
            Dim I As Long = 1
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Open("C:\Test.xlsx")
            xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
            xlCharts = xlWorkSheet.ChartObjects
            myChart = xlCharts.Add(10, 80, 300, 250)
    
            '<~~ There could be more pictures than one
            For Each olobj In xlWorkSheet.Shapes
                olobj.Copy()
                myChart.Activate()
                myChart.Paste()
                myChart.Export("C:\Image" & I & ".gif", "FilterName:=GIF", True)
                I = I + 1
                '<~~ code to delete the picture from Chart and make it ready
                '<~~ for next picture
                '<~~(STILL STUCK ON THIS. WILL TACKLE IT AFTER THE ABOVE IS SORTED)
            Next
            xlWorkBook.Close(SaveChanges:=False)
            xlApp.Quit()
    
            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)
        End Sub
    
        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
    End Class
    Thanks for looking into it.

    Sid
    Attached Images Attached Images  
    Attached Files Attached Files
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Exporting Image From Excel

    Excel ChartObject does not have a Paste property. Try myChart.Chart.Paste() instead.
    There is also an error in the immediate next line and that needs to be corrected too.

    Code:
    myChart.Chart.Paste()
    myChart.Chart.Export("C:\Temp\Image" & I & ".gif", "GIF", True)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Exporting Image From Excel

    Try this code. It will also delete the unwanted image as required in your comments:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim xlApp As Excel.Application
    3.     Dim xlWorkBook As Excel.Workbook
    4.     Dim xlWorkSheet As Excel.Worksheet
    5.     Dim olobj As Excel.Shape
    6.     Dim xlCharts As Excel.ChartObjects
    7.     Dim myChart As Excel.ChartObject
    8.     Dim I As Long = 1
    9.  
    10.     xlApp = New Excel.ApplicationClass
    11.     xlWorkBook = xlApp.Workbooks.Open("C:\Temp\Test.xlsx")
    12.     xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
    13.     xlCharts = xlWorkSheet.ChartObjects
    14.     'myChart = xlCharts.Add(10, 80, 300, 250)
    15.  
    16.     '<~~ There could be more pictures than one
    17.     For Each olobj In xlWorkSheet.Shapes
    18.         myChart = xlCharts.Add(10, 80, 300, 250)
    19.         olobj.Copy()
    20.         myChart.Activate()
    21.         myChart.Chart.Paste()
    22.         myChart.Chart.Export("C:\Temp\Image" & I & ".gif", "GIF", True)
    23.         I = I + 1
    24.         myChart.Delete()
    25.     Next
    26.     xlWorkBook.Close(SaveChanges:=False)
    27.     xlApp.Quit()
    28.  
    29.     releaseObject(xlApp)
    30.     releaseObject(xlWorkBook)
    31.     releaseObject(xlWorkSheet)
    32. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Exporting Image From Excel

    Simply Superb!!!!!

    I was trying to think what to do to get the initial image off the chart...

    Works flawlessly! I knew I could always count on you when it came to vb.net!

    Thanks again...

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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