Microsoft KB Archive/131396

From BetaArchive Wiki
Knowledge Base


"Run-time error '1004'" Using SetBackgroundPicture Method

Article ID: 131396

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Excel 95 Standard Edition



This article was previously published under Q131396


SYMPTOMS

In Microsoft Excel, when you run a Visual Basic macro that contains the SetBackgroundPicture method, you may receive the following error message

Run-time error '1004':

Cannot use that command on a protected sheet

even if you did not use the command on a protected worksheet.

CAUSE

This error message appears if you apply the SetBackgroundPicture method to a DialogSheet in a Visual Basic macro, regardless of whether the sheet is protected. For example, the error message described above appears when you run a macro that contains the following command:

   DialogSheets(1).SetBackgroundPicture ("")
                

You can use the SetBackgroundPicture method to set the background graphic for a worksheet or chart. Because the SetBackgroundPicture method applies only to the Worksheet object and the Chart object, you should receive an error message when you use the SetBackgroundPicture method with the DialogSheet object. However, the error message that should appear in this case (if the dialog sheet is not protected) is the following:

Run-time error '1001':

DialogSheet does not have SetBackgroundPicture method

Note that this error message correctly appears when you use the SetBackgroundPicture method with a Module object.

RESOLUTION

To avoid receiving either of the error messages described above when you use the SetBackgroundPicture method in a Visual Basic macro, use any of the following methods:

Method 1: Activate the worksheet or chart that you want to add the graphic background to before the SetBackgroundPicture method command runs in the macro, as in the following examples:

             Worksheets(1).Select
             ActiveSheet.SetBackgroundPicture ("C:\Clipart\picture1.bmp")
                

-or-

             Charts(1).Select
             ActiveSheet.SetBackgroundPicture ("C:\Clipart\picture2.bmp")
                

Method 2: Apply the SetBackgroundPicture method to a Worksheet or Chart object directly as in the following examples:

             Worksheets(1).SetBackgroundPicture ("C:\Clipart\picture1.bmp")
                

-or-

             Charts(1).SetBackgroundPicture ("C:\Clipart\picture2.bmp")
                

Microsoft provides examples of Visual Basic 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. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

REFERENCES

For more information about using the SetBackgroundPicture method in a Visual Basic macro in Microsoft Excel, click the Index tab in Microsoft Excel Help, and type the following text

setback


and then double-click the selected text to go to the "SetBackgroundPicture Method" topic.


Additional query words: XL7

Keywords: kbcode kbprogramming KB131396