XL: Example of Using API Calls in Multi-Platform Macros

Last reviewed: February 27, 1998
Article ID: Q152261
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows

SUMMARY

This article demonstrates a method for using application programming interface (API) calls in a multiplatform macro.

MORE INFORMATION

When you use Windows API functions from a Visual Basic for Applications macro, you may need to account for the bitness (32-bit or 16-bit) of the macro environment. You will need to make the API declarations for both the 32-bit and the 16-bit versions and selectively call the appropriate library according to your current macro environment.

The recommended method for calling API functions from within Visual Basic for Applications is to encapsulate the API function in a Visual Basic for Applications function. The Visual Basic for Applications function would then be used in the main code routines.

The following example shows this methodology applied to a macro example that will activate a window, given its Class name. For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q104710
   TITLE     : INF: How to Activate an Application with Class Name (2.0)

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 Product Support Services (PSS) 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.

Visual Basic Code Example

  1. Type the following code into a module sheet:

    NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line for the following declare statements. Declares must be entered on a single line.

          ' 32 Bit Declares.
          Declare Function ShowWindow32 Lib "user32" Alias "ShowWindow" _
    
              (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
          Declare Function FindWindow32 Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
          Declare Function SetForegroundWindow32 Lib "user32" Alias _
            "SetForegroundWindow" (ByVal hwnd As Long) As Long
    
          ' 16 Bit Declares.
          Declare Function ShowWindow Lib "User" (ByVal hwnd As Integer, _
              ByVal nCmdShow As Integer) As Integer
          Declare Function SetActiveWindow Lib "User" (ByVal hwnd As _
              Integer) As Integer
          Declare Function FindWindow Lib "User" (ByVal lpClassName As _
              Any, ByVal lpWindowName As Any) As Integer
    
           ' Test routine. Run this subroutine.
           Sub TestIt_ActivateAppClass()
           Dim strClassName As String
           Dim iRetVal As Integer
           strClassName = "opusapp"
           iRetVal = AppActivateClass(strClassName)
           If Not iRetVal Then MsgBox strClassName & " Is NOT currently " & _
              "running"
           End Sub
    
           ' VB Api encapsulation function for ShowWindow.
           Function vb_ShowWindow(ByVal hwnd As Long)
           Const SW_SHOW = 9
           If is32Bit Then
              iRetVal = SetForegroundWindow32(hwnd)
              iRetVal = ShowWindow32(hwnd, SW_SHOW)
           Else
              iRetVal = SetActiveWindow(hwnd)
              iRetVal = ShowWindow(hwnd, SW_SHOW)
           End If
           vb_ShowWindow = iRetVal
           End Function
    
           ' VB Api encapsulation function for FindWindow.
           Function vb_FindWindow(ByVal lpClassName As String) As Long
           Dim strNullString As String 'Define Null String Variable
              If is32Bit Then
                 vb_FindWindow = FindWindow32(lpClassName, strNullString)
              Else
                 vb_FindWindow = FindWindow(lpClassName, 0&)
              End If
           End Function
    
           Function is32Bit() As Boolean
                 is32Bit = False ' Assume Failure.
              If InStr(1, Application.OperatingSystem, "32", 1) > 1 Then
                 is32Bit = True
              End If
           End Function
    
           Function AppActivateClass(lpClassName As String) As Boolean
           Dim hwnd As Long               ' The application's window handle.
           Dim iRetVal As Integer         ' Temp variable.
           Dim iCmdShow As Integer        ' The ShowWindow cmdshow argument.
           ActivateAppClass = False       ' Assume Failure.
                 hwnd = vb_FindWindow(lpClassName) ' Get the Window Handle for
                                                   ' the className.
              If hwnd <> 0 Then                    ' The class was found.
                 iRetVal = vb_ShowWindow(hwnd)     ' Activate the Application.
                 ActivateAppClass = True           ' Return True if Application
                                                   ' Running.
              End If
           End Function
    
    

  2. On the Tools menu, click on Macro. In the list box, click "TestIt_ActivateAppClass" (without the quotation marks), and click Run.


Additional query words: 5.00 5.00a 5.00c 7.00 97 8.00 XL97 API SDK
ALIAS
Keywords : kbcode kbprg kbhowto
Version : WINDOWS:5.0,5.0c,7.0,7.0a,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: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.