Automating Excel From a VB6 EXEIn the most common types of application that combine a VB6 EXE with Excel, the VB6 EXE is the initiating application. The VB6 EXE may either be the primary application automating Excel, or just a front loader for an Excel application yet to be started. In this section, we demonstrate a stripped-down example of a VB6 EXE application automating Excel. We then describe one of the most common real-world examples of a VB6 EXE combined with an Excel application, the front loader. An Excel Automation PrimerAutomating Excel from a VB6 EXE application is much simpler than using a VB6 ActiveX DLL from an Excel application. In fact we have already covered all of the points required to automate one application from another in Chapter 18 Controlling Other Office Applications. The process of controlling Excel from VB6 is no different from controlling an outside application from Excel.We now demonstrate a very basic example of a VB6 EXE that automates Excel. The files for this example can be found on the CD in the \Concepts\Ch20Combining Excel and Visual Basic 6\AutomatingExcel folder. Creating an elaborate example using Excel would be time consuming and would tend to obscure the fundamentals. Instead we create a somewhat lighthearted example that uses an obscure function provided by Excel to convert whole numbers into Roman numerals. This is not as far from reality as it may seem at first. One of the primary reasons for automating Excel from VB6 is to utilize the powerful capabilities of Excel's calculation engine in a VB6 application.To begin the project, start VB6 and select Standard EXE as the project type in the New Project dialog. In addition to the project itself, you will get a single VB6 form by default. This is all we need for this example. Rename your project ConvertToRoman, rename your form FRoman and then save your project. In the VB6 Project window, your project should now look like Figure 20-20. Figure 20-20. The ConvertToRoman Project![]() Figure 20-21. Referencing the Excel Object Library![]() Figure 20-22. The Initial FRoman Form![]() Listing 20-11. The Form_Load Event ProcedureIn this procedure we set all the properties of the form and its controls that are required to make the form user friendly. The result of displaying the form with only this procedure in place is shown in Figure 20-23. Compare this picture with the design-time version of the form shown in Figure 20-22. Figure 20-23. The Display Appearance of the Form![]() Listing 20-12. The txtConvert_KeyPress Event ProcedureThe KeyPress event passes us a KeyAscii argument that is the ASCII value of the character the user is trying to enter. You can modify that character by changing the value of the KeyAscii argument with the KeyPress event. To cancel a character you just change the KeyAscii argument to zero, which is the equivalent of Chr$(0) or vbNullChar, and therefore has the effect of entering nothing.In our txtConvert_KeyPress event we use a Select Case statement to handle the incoming characters. This construct makes it easier to enable one whole series of characters and disable all others. We enable the Backspace character (to allow editing) and the numerals 0 through 9 by including their ASCII values in a Case expression that takes no action. All other characters are caught by the Case Else clause, which cancels them by setting the KeyAscii argument to 0. In this manner the KeyPress event ensures the value in the txtConvert TextBox will always be within the domain of values convertible to a Roman numeral.Speaking of Excel, didn't we say the point of this exercise was to demonstrate how to automate Excel? Well now we have created everything required by our CommandButton's Click event procedure to use Excel to convert the contents of the txtConvert TextBox into an equivalent Roman numeral. The cmdConvert_Click event that accomplishes this feat is shown in Listing 20-13. Listing 20-13. The cmdConvert_Click Event ProcedureBefore we go into a detailed discussion of the cmdConvert_Click event procedure we present a working example of the form in Figure 20-24. If you have been following along with the example in your own copy of VB6, just press F5 to start the application. Because the form is the only object in the project it will display automatically. If you select the File > Make ConvertToRoman.exe menu you will generate a standalone executable file that can be run by just double-clicking it from Windows Explorer. Figure 20-24. The Completed Convert to Roman Numerals Dialog![]() In addition, you cannot help but notice that you never saw Excel when running this example. This is because when an instance of Excel is created via automation it is invisible by default. In the Standard EXECreating a Front Loader for Your Excel Application section below, we demonstrate not only how to make an automated instance of Excel visible, but how to turn it over to the user and exit the VB6 application that started it completely, leaving the created instance of Excel running for the user.Lastly, if you are expecting any add-ins or other auto-loaded files to be open when you create an instance of Excel via automation you will be disappointed. An instance of Excel created via automation does not load any add-ins that are selected in the Tools > Add-ins list nor does it load any files located in auto-start directories like XLStart. If your application depends on these files being loaded you will need to write the code to open them yourself. Using a VB6 EXE Front Loader for Your Excel ApplicationA VB6 EXE front loader has the unique ability to examine the configuration of a user's computer without committing itself to any specific version of Excel or any other application. If your Excel application will be distributed widely and/or run on hardware and software over which you have no control, the front loader can verify that everything required to run your application is in place prior to starting Excel, running your application and exiting. ![]() |