XL97: Using the LoadPicture Function with an Image Control

Last reviewed: March 13, 1998
Article ID: Q162050
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel 97, you can add an Image control to a UserForm. There are two ways to specify which picture file is displayed in the Image control; you can specify the picture when you design the UserForm, or when you run the UserForm. 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.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Creating the UserForm 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 UserForm, 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 OptionButton below the Image control to the UserForm and set the following values for the properties for the OptionButton.

          Property   Value
          ------------------------
          Name       OptionButton1
          Caption    HI
    
    

  5. Add another OptionButton below the first OptionButton to the UserForm and set the following values for the properties for the OptionButton.

          Property   Value
          ------------------------
          Name       OptionButton2
          Caption    BYE
    
    

  6. Add another OptionButton below the second OptionButton to the UserForm and set the following values for the properties for the OptionButton.

          Property   Value
          ------------------------
          Name       OptionButton3
          Caption    Clear
    
    

  7. Double-click the UserForm to display the code module that is associated with the UserForm.

  8. Type the following code for the Initialize event for the UserForm:

          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
    
    
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.

  1. Type the following code for the Click events for the three OptionButtons:

          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" (without the quotation marks) 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, in the Copy To dialog box, locate your Desktop, type "hi.bmp" (without the quotation marks) in the File name box, and then click Save.

NOTE: It is important to save your picture files to the Desktop because the macro code in this article refers to files in your Windows\Desktop folder.

  1. Repeat steps 2-4, but type the word "BYE" (without the quotation marks) and save the file as Bye.bmp.

  2. Quit Microsoft Paint.

Running the Macro

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

    The UserForm is displayed, the picture in the Image control is the Hi.bmp picture, and the "HI" OptionButton is selected.

  2. Click the "BYE" OptionButton.

The picture in the Image control is the Bye.bmp picture, and the "BYE" OptionButton is selected.

  1. Click the "Clear" OptionButton.

The picture in the Image control is cleared completely.

  1. Close the UserForm.

NOTE: The Image control in the UserForm does not display any picture when you view the UserForm while you are designing the UserForm.

REFERENCES

For more information about the Image control, click the Office Assistant in the Visual Basic Editor, type "image control", click Search, and then click to view "Things you can do with an Image control".

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If <Product> Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component


Additional query words: 97 XL97
Keywords : kbcode kbprg xlvbahowto xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.