XL97: Only One Character Is Returned from a Declared Function

ID: Q183086

The information in this article applies to:

SYMPTOMS

When you use a formula in a worksheet to call a function that is declared in a Visual Basic module in Microsoft Excel 97, only one character may be returned by the function.

WORKAROUND

You can work around this problem by creating a user-defined "wrapper" function to call the declared function.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

Example

In this example, you create a user-defined "wrapper" function to call the declared function. You use the MessageBox function and the declaration in the following example:

1. Start the Visual Basic Editor (press ALT+F11).

2. On the Insert menu, click Module.

3. Type the following:

      Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
         (ByVal hwnd As Long, ByVal lpText As String, _
         ByVal lpCaption As String, ByVal wType As Long) As Long

      Function MyMessageBox(hwnd As Long, lpText As String, _
         lpCaption As String, wType As Long) As Long

         MyMessageBox = MessageBox(hwnd, lpText, lpCaption, wType)

      End Function

4. Switch to Microsoft Excel (press ALT+F11).

5. In cell A1, type the following formula:

      =MyMessageBox(0,"Works fine in Excel 97","Title Bar",0)

  This function is a wrapper function that is entered directly into
  the worksheet. You can use the wrapper function without experiencing the
  problem listed at the beginning of the article.

 Notice when you press ENTER, the title bar and message text are complete.

6. Click OK.

STATUS

This behavior is by design of Microsoft Excel 97.

MORE INFORMATION

The function is returning a UNICODE string; however, Excel is expecting an American National Standards Institute (ANSI) string. UNICODE is a double- byte character set in which the second byte is reserved in memory. UNICODE appears to ANSI functions as a null byte and is assumed to be the string terminator. This results in the string being truncated after the first character.

Instead of typing the function directly into the worksheet, as in the following example

   =MessageBox(0,"Only one character visible in Excel 97","Title Bar",0)

create the MyMessageBox function to call the following code:

   Function MyMessageBox(hwnd As Long, lpText As String, _
      lpCaption As String, wType As Long) As Long

      MyMessageBox = MessageBox(hwnd, lpText, lpCaption, wType)

   End Function

You can then type the wrapper function directly into the worksheet without experiencing the problem listed at the beginning of the article. The following is an example:

   =MyMessageBox(0,"Works fine in Excel 97","Title Bar",0)

Additional query words: XL97 API DLL
Keywords          : kbprg kbdta kbdtacode OffVBA KbVBA 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbnofix

Last Reviewed: May 18, 1999