VBA for Excel 2007 tutorial-insert pictures


VBA code to insert pictures

In this example, you will learn VBA code to add a picture in to your current Excel worksheet. A picture is a member of the Shapes collection. To add a picture to Excel worksheet, the AddPicture() method is used. The addPicture() has the following prototype:

AddPicture("Filename", Linktofile,Savewithdocument,Left,Top,Width,Height).

-The Filename is the file name of the picture.
-Linktofile: it has two values True or False. If True is specified, the picture displayed is linked to the source file.
-Savewithdocument: True or False can be supplied. If you supply True, the picture displayed is saved with the current document.
-Left, Top, Width, and Height represent the value of x-coordinate, y-coordinate, width, and height of the picture that will be displayed in the Excel sheet.

To run this example VBA code, you will need a Form, and a CommandButton.


VBA for Excel: vba example code to insert pictures in excel sheet
VBA code for this example:


Private Sub CommandButton1_Click()
  'Add a picture in current project folder to the Excel worksheet
  Worksheets(1).Shapes.AddPicture _
  "ExcelVBA-color-index.png", True, True, 200, 100, 170, 170
   
End Sub

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.