Microsoft KB Archive/183086

= XL97: Only One Character Is Returned from a Declared Function =

Article ID: 183086

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q183086





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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

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

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:  Start the Visual Basic Editor (press ALT+F11). On the Insert menu, click Module.  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  Switch to Microsoft Excel (press ALT+F11).  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.


 * 1) Click OK.

<div class="status_section">

STATUS
This behavior is by design of Microsoft Excel 97.

<div class="moreinformation_section">

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: kbbug kbdtacode kbnofix kbprogramming KB183086

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.