Unable to Pass Boolean Data Type with OLE Automation

ID: Q127133

The information in this article applies to:

SYMPTOMS

When you use OLE Automation (an OLE service that allows documents to "drive" each other) to control Microsoft Excel, an OLE Automation error may occur.

Note that this error may occur even though the same code works perfectly well from within Microsoft Excel.

CAUSE

This problem occurs when you attempt to pass the Boolean value FALSE to Microsoft Excel. Unfortunately, under OLE Automation, there is no way to send the Boolean data type to Microsoft Excel. Using OLE Automation to send the values TRUE or FALSE from one of the applications mentioned at the beginning of this article results in the following Integer data type values being passed to Microsoft Excel: TRUE sends the value -1, and FALSE sends the value 0.

WORKAROUNDS

In some cases, you can send an alternative command to Microsoft Excel that will accomplish the desired result. Below are examples that use the ExecuteExcel4Macro method instead of the Visual Basic, Applications Edition, command.

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.

Example 1

The following code assumes Microsoft Excel is already running and that a workbook is open.

Instead of using this

   ' Set the Page Setup settings for the active sheet:
   ' Fit to 1 page wide by 1 page tall.
   Set XLObj = GetObject(, "excel.application.5")
   XLObj.ActiveSheet.PageSetup.Zoom = False
   Set XLObj = Nothing

use this:

   ' Set the Page Setup settings for the active sheet:
   ' Fit to 1 page wide by 1 page tall.
   Set XLObj = GetObject(, "excel.application.5")
   XLObj.application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{1,1})"
   Set XLObj = Nothing

Note that you could use TRUE instead of {1,1}, as in the following example:

   XLObj.application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,TRUE)"

Also, to alter the width or height of the Page Setup, use the following substitutions:

   To set the page for 3 wide by ___ tall, use {3,#N/A}
   To set the page for ___ wide by 3 tall, use {#N/A,3}

Example 2

The following code assumes Microsoft Excel is already running and that a workbook is open.

Instead of using this

   'Set the Zoom for the active window to 100%.
   Set XLObj = GetObject(, "excel.application.5")
   XLObj.ActiveWindow.Zoom = False
   Set XLObj = Nothing

use this:

   'Set the Zoom for the active window to 100%.
   Set XLObj = GetObject(, "excel.application.5")
   XLObj.application.ExecuteExcel4Macro "ZOOM(FALSE)"
   Set XLObj = Nothing

MORE INFORMATION

Using OLE Automation, it is possible to pass commands to Microsoft Excel from the other applications listed at the beginning of this article.

Certain commands in Microsoft Excel behave differently, based on the data type of the argument passed to the command. One example is the Zoom property. The Zoom property applies to the Window and PageSetup classes in Microsoft Excel. This property will behave differently depending upon whether you send it an integer argument or the Boolean argument FALSE.

KBCategory: kbprg kbmacro KBSubcategory:

Additional reference words: 5.00 5.00c

Last Reviewed: September 9, 1996