none
insert image to excel RRS feed

  • Question

  • hi

    I want to insert a picture of the excel from the Resources
    I used this code but it does not work

     excelWorksheet.Shapes.AddPicture(My.Resources.ResourceManager.GetObject("image1"), False, True, 0, 0, 45, 50)

    Sunday, October 14, 2018 4:55 PM

Answers

  • One more thing, the majority of replies you will find using project resources will resort to using the Windows Clipboard which IMHO is very bad as you wipe out what could have been important to the user.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, October 14, 2018 7:22 PM
    Sunday, October 14, 2018 5:25 PM
    Moderator

All replies

  • Here is an example

    Option Strict On
    Option Infer On
    
    Imports Excel = Microsoft.Office.Interop.Excel
    Imports Microsoft.Office
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Sub OpenExcel(ByVal FileName As String, ByVal SheetName As String)
            If IO.File.Exists(FileName) Then
                Dim PicFile As String = IO.Path.Combine(Application.StartupPath, "Cheese.jpg")
                Dim Proceed As Boolean = False
    
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
    
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(FileName)
    
                xlApp.Visible = False
    
                xlWorkSheets = xlWorkBook.Sheets
    
                For x As Integer = 1 To xlWorkSheets.Count
                    xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
    
                    If xlWorkSheet.Name = SheetName Then
                        Proceed = True
                        Exit For
                    End If
    
                    Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
    
                Next
                If Proceed Then
                    Dim xlShapes = xlWorkSheet.Shapes
                    xlShapes.AddPicture(PicFile, Core.MsoTriState.msoCTrue, Core.MsoTriState.msoCTrue, 170, 50, 76, 68)
                    ReleaseComObject(xlShapes)
                    xlWorkSheet.SaveAs(FileName)
                Else
                    MessageBox.Show(SheetName & " not found.")
                End If
    
    
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
    
                ReleaseComObject(xlWorkSheets)
                ReleaseComObject(xlWorkSheet)
                ReleaseComObject(xlWorkBook)
                ReleaseComObject(xlWorkBooks)
                ReleaseComObject(xlApp)
    
            Else
                MessageBox.Show("'" & FileName & "' not located. Try one of the write examples first.")
            End If
        End Sub
        Private Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            End Try
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FileName As String = IO.Path.Combine(Application.StartupPath, "MyFile.xlsx")
            OpenExcel(FileName, "Sheet2")
            MessageBox.Show("Done")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, October 14, 2018 4:59 PM
    Moderator
  • hi

    I want to insert a picture of the excel from the Resources

    not from file path

    I just want to insert the image into the excel


    • Edited by ahmeddc Sunday, October 14, 2018 5:05 PM
    Sunday, October 14, 2018 5:04 PM
  • In regards to resource, the simply approach is to save the file to disk, use it, remove it. Other than what is shown below

    Option Strict On
    Option Infer On
    
    Imports Excel = Microsoft.Office.Interop.Excel
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Sub OpenExcel(pFileName As String, pSheetName As String)
            Dim fileName = IO.Path.Combine(Application.StartupPath, "Cheese.jpg")
            My.Resources.Cheese.Save(fileName)
    
            Dim picFile As String = IO.Path.Combine(Application.StartupPath, "Cheese.jpg")
            Dim proceed As Boolean = False
    
            Try
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
    
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(pFileName)
    
                xlApp.Visible = False
    
                xlWorkSheets = xlWorkBook.Sheets
    
                For x As Integer = 1 To xlWorkSheets.Count
                    xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
    
                    If xlWorkSheet.Name = pSheetName Then
                        proceed = True
                        Exit For
                    End If
    
                    Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
    
    
                Next
                If proceed Then
                    Dim xlShapes = xlWorkSheet.Shapes
                    xlShapes.AddPicture(picFile, Core.MsoTriState.msoCTrue, Core.MsoTriState.msoCTrue, 170, 50, 76, 68)
                    ReleaseComObject(xlShapes)
    
                    xlWorkSheet.SaveAs(pFileName)
    
                Else
                    MessageBox.Show(pSheetName & " not found.")
                End If
    
    
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
    
                ReleaseComObject(xlWorkSheets)
                ReleaseComObject(xlWorkSheet)
                ReleaseComObject(xlWorkBook)
                ReleaseComObject(xlWorkBooks)
                ReleaseComObject(xlApp)
            Finally
                IO.File.Delete(fileName)
            End Try
    
        End Sub
        Private Sub ReleaseComObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            End Try
        End Sub
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim fileName As String = IO.Path.Combine(Application.StartupPath, "MyFile.xlsx")
            OpenExcel(fileName, "Sheet2")
            MessageBox.Show("Done")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, October 14, 2018 5:22 PM
    Moderator
  • One more thing, the majority of replies you will find using project resources will resort to using the Windows Clipboard which IMHO is very bad as you wipe out what could have been important to the user.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, October 14, 2018 7:22 PM
    Sunday, October 14, 2018 5:25 PM
    Moderator
  • Thank you
      There is some sort of cheating inside the ressources
    But in the end it was
    Sunday, October 14, 2018 7:21 PM