Solved

VB.net Excel Add-in Insert Picture in a specific cell

Posted on 2014-07-28
2
2,442 Views
Last Modified: 2014-07-28
Hi

What additional code would I use in that below to insert a picture into a specific cell
and use the size of the actual image rather than forcing it

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 misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        'add some text
        xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
        xlWorkSheet.Cells(2, 1) = "Adding picture in Excel File"

        'replace you picture to xl_pic.JPG

        xlWorkSheet.Shapes.AddPicture("C:\xl_pic.JPG", _
             Microsoft.Office.Core.MsoTriState.msoFalse, _
             Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45)

        xlWorkSheet.SaveAs("C:\vbexcel.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    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
0
Comment
Question by:murbro
2 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40223596
Hi,

pls try

c = ActiveCell
PicTop = c.Top
PicLeft = c.Left
oPic = xlWorkSheet.Shapes.("C:\xl_pic.JPG", _
             Microsoft.Office.Core.MsoTriState.msoFalse, _
             Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, 0, 0)

oPic.ScaleHeight 1, Microsoft.Office.Core.MsoTriState.MsoTrue
oPic.ScaleWidth 1, Microsoft.Office.Core.MsoTriState.MsoTrue

Regards
0
 

Author Closing Comment

by:murbro
ID: 40223640
Thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question