蜂胶:How To Build an Add-in (XLL) for Excel Using ...
来源:百度文库 编辑:九乡新闻网 时间:2024/10/05 22:49:59
How To Build an Add-in (XLL) for Excel Using Visual C++
View products that this article applies to.This article was previously published under Q178474On This Page
- SUMMARY
- MORE INFORMATION
- Steps to Create an XLL
- Using the Add-In with Microsoft Excel
- Additional Notes
- Steps to Create an XLL
- REFERENCES
SUMMARY An XLL is an add-in for Microsoft Excel that you can build with any compiler tha...
An XLL is an add-in for Microsoft Excel that you can build with any compiler that supports building DLLs (dynamic link libraries). This article is designed to get you started building XLLs with Microsoft Visual C++. To follow the steps outlined in this article, you should have some experience building DLLs and you should have the Microsoft Excel 97 Developer's Kit, which contains the necessary header and library files to build an XLL. Back to the topMORE INFORMATION Steps to Create an XLL Create a new MFC AppWizard (.dll) project called Anewxll....
Steps to Create an XLL
- Create a new MFC AppWizard (.dll) project called Anewxll.
- Copy the Xlcall.h, Framewrk.h, Framewrk.c, and Xlcall32.lib from the Microsoft Excel 97 Developer's Kit to your project directory.
- Rename Framewrk.c to Framewrk.cpp.
- Add Framewrk.cpp to your project source files.
- Add the following #include line to the top of Framewrk.cpp to avoid compiler errors about pre-compiled headers:
#include "stdafx.h"
- Change the following line in Framewrk.cpp:
#include "..\..\include\xlcall.h"
#include "xlcall.h"
- Click Settings on the Project menu. Click the Link tab and add Xlcall32.lib to the Object/library modules edit box.
- Add the following #include statements to the top of Anewxll.cpp:
#include "xlcall.h" #include "framewrk.h"
- Append the following code to Anewxll.cpp: Sample Code -----------
//================================================================ // Commonly used global variables int err; char buf[8192]; char txt[8192]; // Function registering table int nFuncs; // proc, type_text, function_text, arg, macro_type, category, // shortcut_text, help_topic, function_help static LPSTR func[][9] = { {" MyFunc", " JJJ", " MyFunc", " ", " 1", " MyCat", " ", " ", " "}, {" MyMotd", " I", " MyMotd", " ", " 1", " MyCat", " ", " ", " "}, {0,0,0,0, 0, 0, 0} }; // Menu table int nMenuItems; static LPSTR menu[][5] = { {" &MyMenu", " ", " ", " Joe's Xll menu!!!", " "}, {" M.O.T.D."," MyMotd", " ", " Message of the Day!", " "}, {0, 0, 0, 0, 0} }; // Initialization routine BOOL __stdcall xlAutoOpen(void) { AFX_MANAGE_STATE(AfxGetStaticModuleState( )); // DEBUG output to indicate when called AfxMessageBox("xlAutoOpen() called!", MB_SETFOREGROUND); int i, j; // Get XLL file name static XLOPER xDll; Excel(xlGetName, &xDll, 0); // Prefix strengths with their length & count items // Note the framework's TempStr() function prefixes the // lengths anyway, but this is for other code that might // use the arrays for(nFuncs=0; func[nFuncs][0]; nFuncs++) { for(i=0; i<9; i++) { func[nFuncs][i][0] = (BYTE) strlen(func[nFuncs][i]+1); } } for(nMenuItems=0; menu[nMenuItems][0]; nMenuItems++) { for(i=0; i<5; i++) { menu[nMenuItems][i][0] = (BYTE) strlen(menu[nMenuItems][i]+1); } } // Loop through the function list, and register the functions for(i=0; i
- Add the following lines to the end of your Anewxll.def file:
xlAutoOpen xlAutoClose xlAddInManagerInfo MyMotd MyFunc
- You can now compile your project to produce a DLL called Anewxll.dll. Once the DLL is compiled, rename it to Anewxll.xll.
Using the Add-In with Microsoft Excel
- Start a new workbook in Microsoft Excel.
- Click Add-ins on the Tools menu. Browse to add Anewxll.xll and click OK. Notice that when you click OK in the Add-ins dialog, the xlAutoOpen function runs.
- Click M.O.T.D on the MyMenu menu. When your menu item is clicked, the MyMotd function runs to display a MessageBox with a quote such as "Rebekah says 'An Apple a day, keeps the doctor away!'".
- In a cell, add the following formula:
=MYFUNC(2,6)
- Click Add-ins on the Tools menu. Clear the add-in check box and click OK. Notice that when you click OK in the Add-ins dialog box, the xlAutoClose function runs.
Additional Notes
If you are using Microsoft Visual C++ 6.0, the preceding example fails when you compile it in Debug mode. The failure occurs because as of Visual C++ 6.0, string literals are placed in read only memory in debug builds, and the trick that XLL developers use to length-prefix their strings causes an access violation. As a work-around, you can remove the /ZI compiler switch in your project settings, or just test a release-build. For more information, see the following article in the Microsoft Knowledge Base: 198477 (http://support.microsoft.com/kb/198477/EN-US/ ) PRB: Use of /ZI May Cause Access Violation Back to the top
How To Build an Add-in (XLL) for Excel Using ...
how to build android cts
How To Build Traffic To Your Blog
How to be an expert
How to Create an HTML Editor Application
How to Create an HTML Editor Application1
In exe export function like below, and add .def file add foo to exports.
How to cross-compile BASH for Android
China to build first satellite for European n...
To abolish the treaty or to ally for China an...
How to be Happy in Hong Kong
HOW TO : use DDMS in Ubuntu
How to create a file in MS
How To Make a Youtube Video Into an Animated GIF
How To Export Data from a DLL or an Application
To Be an Individual Or to Be in a Body?
How to display variables in Taglist window in...
How To Cause Navigation to Occur in Same WebB...
An Investor's Guide to Buying Influence in Ch...
How to Find the FSMO Role Owners Using ADSI and WSH
如何组建一个好的董事会(How to Build a Good Board)_俱道适往
How to Insert YouTube Videos in PowerPoint Presentations
How to close Pandora's Box in Libya?
How to sort American interests in the South C...