Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Using the LoadPicture Function with an Image Control


View products that this article applies to.

Summary

In Microsoft Excel 2000, you can add an image control to a user form. There are two ways to specify which picture file is displayed in the image control; you can specify the picture when you design the user form, or you can specify the picture when you run the user form. The technique you use depends on whether you want to store the picture file with your project.

The advantage of using the run-time method is that the picture file is not stored with the project, which minimizes the size of the project. However, if you distribute the project to others, you must remember to include the picture file with the project file, and you must provide instructions for placing the picture file in the correct location.

This article provides an example Visual Basic for Applications macro that uses the LoadPicture function to load a picture file into an image control during run time.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Creating the User Form and the Macro Code

  1. Save and close any open workbooks, create a new workbook, and then start the Visual Basic Editor (press ALT+F11).
  2. On the Insert menu, click UserForm.
  3. Add an image control near the top and center of the user form, and then set the following values for the properties for the image control.
       Property   Value
       -----------------
       Name       Image1
       AutoSize   True
       Height     100
       Width      100
    					
  4. Add an option button to the user form below the image control and set the following values for the properties for the option button.
       Property   Value
       ------------------------
       Name       OptionButton1
       Caption    Hi
    					
  5. Add another option button below the first option button and set the following values for the properties for the option button.
       Property   Value
       ------------------------
       Name       OptionButton2
       Caption    Bye
    					
  6. Add another option button below the second option button and set the following values for the properties for the option button.
       Property   Value
       ------------------------
       Name       OptionButton3
       Caption    Clear
    					
  7. Double-click the user form to display the code module that is associated with the user form.
  8. Type the following code for the Initialize event for the user form:
    Private Sub UserForm_Initialize()
    
      'Select the "Hi" option button.
       OptionButton1.Value = True
    
      'Load the Hi.bmp picture file into the Image control.
       Image1.Picture = LoadPicture("C:\Windows\Desktop\hi.bmp")
    End Sub
    					

Customizing the Macro for the Path to Your Desktop

NOTE: The path to your Desktop folder may be different, depending on how you logged on to Windows. If you log on to Windows with a password, the Desktop location may be the Windows\Profiles\<username>\Desktop folder. Therefore, you must use a different path in the LoadPicture function and in the following OptionButton functions. To customize the macro for the path to your desktop:
  • Type the following code for the Click events for the three option buttons:
    Private Sub OptionButton1_Click()
    
      'Load the Hi.bmp picture file into the Image control.
       Image1.Picture = LoadPicture("C:\Windows\Desktop\hi.bmp")
    
    End Sub
    
    Private Sub OptionButton2_Click()
    
      'Load the Bye.bmp picture file into the Image control.
       Image1.Picture = LoadPicture("C:\Windows\Desktop\bye.bmp")
    
    End Sub
    
    Private Sub OptionButton3_Click()
    
      'Clear the picture file in the Image control.
       Image1.Picture = LoadPicture("")
    
    End Sub
    
    					

Creating the Two Picture Files

  1. Start Microsoft Paint.
  2. Create the word Hi by free hand. The letters should be about 1.5 inches tall.
  3. On the Paint toolbar, click Select, and then draw a square box around the letters you created. The sides of the box should be about 2 inches long.
  4. On the Edit menu, click Copy To.
  5. In the Copy To dialog box, locate your Desktop, type Hi.bmp in the File name box, and then click Save to save the file as "Hi.bmp".
  6. Repeat steps 2-5, but create the word Bye by free hand, and then save the file to your Desktop as "Bye.bmp".
  7. Quit Microsoft Paint.
NOTE: It is important to save your picture files to the Desktop because the macro code in this article refers to files in your Desktop folder.

Running the Macro

  1. In the Microsoft Excel Visual Basic Editor, run your user form.

    The user form is displayed. The picture in the image control is the Hi.bmp picture, and the "Hi" option button is selected.
  2. Click the "Bye" option button.

    The picture in the image control is the Bye.bmp picture, and the "Bye" option button is selected.
  3. Click the "Clear" option button.

    The picture in the image control is cleared completely.
  4. Close the user form.
NOTE: The image control in the user form does not display any picture when you view the user form while you are designing the user form.

↑ Back to the top


References

For more information about using an Image control, click Microsoft Visual Basic Help on the Help menu, type image control in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213732, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213732
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 269