none
[VB] Insert+Center Image into Excel

    Question

  • Hello,

    I want to insert an image into my Excel Sheet.
    It works when i specify the path, but in my case there is no path.

    With my VB Code, i have 3 type of picture to insert in Excel :
       - 1 wich is in My.Settings.
       - 1 wich is a System.Drawing.Image.
       - 1 wich is a Byte64.

    Have you an idea ?
    Monday, December 14, 2009 9:41 AM

All replies

  • can u try the below code

    Dim xlSheet1 As Excel.Worksheet
    Dim xlRange As Excel.Range
    Try
    	Dim ms As New System.IO.MemoryStream
    	Dim oIMage As Image = Image.FromFile("c:\Integral logo.bmp")
    	oIMage.Save(ms, oIMage.RawFormat)
    	Dim arrImage() As Byte = ms.GetBuffer
    	Dim integralLogo As Bitmap = CType(Image.FromStream(ms), Bitmap)
    	Clipboard.SetDataObject(integralLogo)
    	xlRange = xlSheet1.Range(xlSheet1.Cells(1, 1), xlSheet1.Cells(1, 1))
    	xlRange.Select()
    	xlSheet1.Paste()
    Catch ex As Exception
    End Try

    also check the below thread
    --------------------------------


    hope this helped....



    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Marked as answer by Gilles Lautrou Monday, December 14, 2009 10:11 AM
    • Unmarked as answer by Gilles Lautrou Monday, December 14, 2009 11:47 AM
    Monday, December 14, 2009 9:52 AM
  • Thanks a lot, it is perfect.
    Monday, December 14, 2009 10:10 AM
  • I have an another problem without using Shapes.
    I want to center Image in its Cell.

    Have you an idea ?
    Monday, December 14, 2009 11:47 AM
  • try this code, can u add with the existing after xlSheet1.Paste() .....

    xlRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
    xlRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

    hope this helped....



    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    Monday, December 14, 2009 12:35 PM
  • xlRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
    xlRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
    No, it doesn't works.
    The picture alignment is Top,Left.

    Below is my code :
    Clipboard.SetDataObject(myBmp)
                            xlRange = ActiveWorkbook.ActiveSheet.Range("A1")
                            xlRange.Select()
                            ActiveWorkbook.ActiveSheet.Paste()
                            xlRange.VerticalAlignment = XlVAlign.xlVAlignCenter
                            xlRange.HorizontalAlignment = XlHAlign.xlHAlignCenter

    Monday, December 14, 2009 1:41 PM