ACC: How to Call Functions from Microsoft ExcelLast reviewed: August 28, 1997Article ID: Q153748 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. This article demonstrates how to call Microsoft Excel functions from within Microsoft Access using Automation. This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.
MORE INFORMATIONMicrosoft Excel offers some functions that are not available in Microsoft Access, for example, statistical functions. You can access these functions using Automation if you set a reference to the Microsoft Excel object library. To create a reference to the Microsoft Excel object library, follow these steps:
The following two sample subroutines use Microsoft Excel statistical functions. NOTE: Microsoft Excel does not automatically close after it has been opened using Automation. The following functions use the Quit method to close Microsoft Excel. For more information about quitting Microsoft Excel, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q145770 TITLE : ACC: Automation Does Not Close Microsoft Excel Example 1The following function uses Automation to call the Microsoft Excel Median() function. Half of the set of numbers fall below and half above the median.
Sub xlMedian() Dim obj As Excel.Application Set obj = CreateObject("Excel.Application") MsgBox obj.Application.Median(1, 2, 5, 8, 12, 13) obj.Quit Set obj = Nothing End SubThe function displays 6.5 in a message box. If you are using Microsoft Access 97 with Microsoft Excel 97, you can simplify the code by calling the Microsoft Excel reference directly:
Sub xlMedian() MsgBox Excel.Application.Median(1, 2, 5, 8, 12, 13) End SubNOTE: When you use this syntax, Microsoft Excel 97 remains in memory until you reset your code or close your database. Note, too, that although this syntax works with Microsoft Excel 7.0, Microsoft Excel 7.0 remains in memory after you reset your code or close your database, and even after you quit Microsoft Access. To conserve system resources, do not use this syntax with Microsoft Excel 7.0.
Example 2The following function uses Automation to call the Microsoft Excel ChiInv() function, which returns the inverse or the one-tailed probability of the Chi-Squared distribution:
Sub xlChiInv() Dim obj As Excel.Application Set obj = CreateObject("Excel.Application") MsgBox obj.Application.ChiInv(0.05, 10) obj.Quit Set obj = Nothing End SubThis function displays 18.3070290368475 in a message box. If you are using Microsoft Access 97 with Microsoft Excel 97, you can simplify the code by calling the Microsoft Excel reference directly:
Sub xlChiInv() MsgBox Excel.Application.ChiInv(0.05, 10) End SubRefer to the NOTE in Example 1 for information about the ramifications of using this syntax. Microsoft Excel also uses add-ins. These are programs that include custom functions and commands. If you need to use a function included in an add-in program, you must first open the add-in. The following is an example of using the LCM (Least Common Multiple) function:
Sub xlAddin() Dim obj As Excel.Application Set obj = CreateObject("Excel.Application") ' Opens the add-in, which is in the Analysis folder of the ' Microsoft Excel Library Directory. obj.workbooks.Open (obj.Application.librarypath & _ "\Analysis\atpvbaen.xla") ' Runs the AutoOpen macro in the add-in obj.workbooks("atpvbaen.xla").RunAutoMacros (xlAutoOpen) MsgBox obj.Application.Run("atpvbaen.xla!lcm", 5, 2) obj.Quit Set obj = Nothing End SubThis function displays 10 in a message box.
REFERENCESFor more information about using Automation, search for "Automation," and then "Automation with Microsoft Access" using the Microsoft Access 97 Help Index. For more information about using the Object Browser, search for "Object Browser," and then "Work with objects in Visual Basic using the Object Browser" using the Microsoft Access 97 Help Index. For more information about referencing type libraries, search for "Type Libraries," and then "Set References to Type Libraries" using the Microsoft Access 97 Help Index. For more information about add-ins in Microsoft Excel, search for "add-in programs" using the Microsoft Excel 97 Help Index. Keywords : AutoGnrl PgmHowTo Technology : kbole Version : 7.0 97 Platform : WINDOWS Hardware : x86 Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |