Excel 2002 Power Programming with VBA phần 9 ppsx

99 229 0
Excel 2002 Power Programming with VBA phần 9 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Compatibility Issues I f the applications that you’ve developed using Excel 2002 will be used only by others who also use the same version of Excel, you can skip this chapter. But if your application also needs to run on earlier versions of Excel, Excel for Macintosh, or international versions of Excel, you must know about some potential issues. These issues are the topic of this chapter. What Is Compatibility? Compatibility is an often-used term among computer people. In general, it refers to how well software performs under vari- ous conditions. These conditions may be defined in terms of hardware, software, or a combination of the two. For example, software that is written specifically for a 32-bit operating sys- tem such as Windows XP will not run under the older 16-bit versions of Windows 3.x. In other words, 32-bit applications are not compatible with Windows 3.x. And, as I’m sure you realize, software written for Windows will not run on other operating systems, such as Macintosh or Linux. In this chapter, I discuss a more specific compatibility issue, involving how your Excel 2002 applications will work with earlier versions of Excel for Windows and Excel for Macintosh. The fact that two versions of Excel may use the same file format isn’t always enough to ensure complete compatibility between the contents of their files. For example, Excel 97, Excel 2000, Excel 2002, and Excel 98 for Macintosh all use the same file format — but compatibility issues are rampant. Just because a particular version of Excel can open a worksheet file or an add-in doesn’t guarantee that that version of Excel can carry out the VBA macro instructions contained in it. 26 26 CHAPTER ✦✦✦✦ In This Chapter How to make sure your Excel 2002 applications will also work with previous versions of Excel Issues to be aware of if you’re developing Excel applications for international use ✦✦✦✦ 4799-2 ch26.F 6/11/01 9:47 AM Page 753 754 Part VII ✦ Other Topics The compatibility problem is more serious than you may think. You can run into compatibility problems even within the same major version of Excel. For example, Excel 2000 exists in at least three different sub-versions: the original release, the “service release” known as SR-1, plus another service release, SR-1a. The service releases fixed some bugs and also introduced a few subtle changes. Therefore, you can’t be guaranteed that an application developed using the original release of Excel 2000 will perform flawlessly with Excel 2000 SR-1. The point here is that Excel is a moving target and there is really no way that you can guarantee complete compatibility. Unfortunately, cross-version compatibility doesn’t happen automatically. In most cases, you need to do quite a bit of addi- tional work to achieve compatibility. Types of Compatibility Problems You need to be aware of five categories of potential compatibility problems. These are listed here and discussed further in this chapter: ✦ File format issues. Workbooks can be saved in several different Excel file for- mats. Excel may not be able to open workbooks that were saved in a later version file format. ✦ New feature issues. It should be obvious that a feature introduced in a particu- lar version of Excel cannot be used in previous versions of Excel. ✦ 32-bit vs. 16-bit issues. If you use Windows API calls, you need to pay attention to this issue if your application must work with 16-bit versions of Excel (such as Excel 5). ✦ Windows vs. Macintosh issues. If your application must work on both platforms, plan to spend lots of time ironing out various compatibility problems. ✦ International issues. If your application will be used by those who speak another language, you must address a number of additional issues. After reading this chapter, it should be clear that there is only one way to ensure compatibility: You must test your application on every target platform and with every target version of Excel. Often, this is simply not feasible. However, there are measures that you, as a developer, can take to help ensure that your application works with different versions of Excel. If you’re reading this chapter in search of a complete list of specific compatibility issues among the various versions of Excel, you will be disappointed. As far as I know, no such list exists and it would be virtually impossible to compile one. These types of issues are far too numerous and complex. Note 4799-2 ch26.F 6/11/01 9:47 AM Page 754 756 Part VII ✦ Other Topics Predefined constants are available for the FileFormat property. For example, the statement that follows displays True if the active workbook is an Excel 5 file: MsgBox ActiveWorkbook.FileFormat = xlExcel5 Table 26-1 lists the constants and values for various Excel file formats. Table 26-1 Constants and Values for Various Excel File Formats Excel version Constant Value Excel 2.1 xlExcel2 16 Excel 3.0 xlExcel3 29 Excel 4.0 xlExcel4Workbook 35 Excel 5 xlExcel5 39 Excel 95/97 xlExcel9795 43 Excel in HTML format xlHtml 44 Excel add-in xlAddIn 18 Excel 97/2000/2002 xlWorkbookNormal -4143 Avoid Using New Features If your application must work with Excel 2002 and earlier versions, you will need to avoid any features that were added after the earliest Excel version that you will support. Another alternative is to incorporate the new features selectively. In other words, your code can determine which version of Excel is being used, and either take advantage of the new features or not. VBA programmers must be careful not to use any objects, properties, or methods that aren’t available in earlier versions. In general, the safest approach is to develop your application with the “lowest common denominator.” For compatibility with Excel 95, Excel 97, and Excel 2000, you should use Excel 95 for development, and then test thoroughly using the other versions. If your application must support Excel 95, you can’t use any UserForms — which were introduced in Excel 97. Rather, you will need to use dialog sheets. 4799-2 ch26.F 6/11/01 9:47 AM Page 756 757 Chapter 26 ✦ Compatibility Issues Applications That Use Windows API Calls Excel 95 and later versions are all 32-bit programs. Excel 5, however, is a 16-bit pro- gram. This “bitness” determines which operating system Excel can run under. Excel 5 can run on 16-bit Windows 3.x, as well as 32-bit versions of Windows. The 32-bit versions of Excel run only on 32-bit versions of Windows. This becomes important when your VBA code uses API function calls, since 32-bit Windows API functions are declared differently than 16-bit Windows API functions. Therefore, an Excel 5 application that uses 16-bit Windows API functions will not work with Excel 95 or later. Similarly, an Excel 95 (or later) application that uses 32-bit Windows API functions will not work with Excel 5. For compatibility with Excel 5 through Excel 2002, you need to declare both the 16- bit and 32-bit version of the API functions in your module. Then your code will need to determine which version of Excel is running and call the appropriate function. Following is an example of a procedure that utilizes API function calls, and that works with both Excel 5 and later versions. The Declare statements for this example appear in the following: ‘ 32-bit API declaration Declare Function GetSystemMetrics32 Lib “user32” _ Alias “GetSystemMetrics” (ByVal nIndex As Long) As Long ‘ 16-bit API declaration Declare Function GetSystemMetrics16 Lib “user” _ Alias “GetSystemMetrics” (ByVal nIndex As Integer) _ As Integer I declared the 32-bit version and the 16-bit version of the API function. Note the use of the keyword Alias. This precedes the actual name of the function in the Windows API, which are identical here. I declared these functions with different names to distinguish them from one another. Determining Excel’s Version Number The Version property of the Application object returns the version of Excel. The returned value is a string, so you may need to convert it to a value. VBA’s Val function is perfect for this. The following function, for example, returns True if the user is running Excel 2002 or later (Excel 2002 is version 10): Function XL10OrLater() XL10OrLater = Val(Application.Version) >= 10 End Function 4799-2 ch26.F 6/11/01 9:47 AM Page 757 758 Part VII ✦ Other Topics The DisplayVideoInfo procedure, which follows, displays the video resolution of the user’s system: Sub DisplayVideoInfo() Const SM_CXSCREEN = 0 Const SM_CYSCREEN = 1 If Val(Application.Version) > 5 Then ‘ 32-bit Excel vidWidth = GetSystemMetrics32(SM_CXSCREEN) vidHeight = GetSystemMetrics32(SM_CYSCREEN) Else ‘ 16-bit Excel vidWidth = GetSystemMetrics16(SM_CXSCREEN) vidHeight = GetSystemMetrics16(SM_CYSCREEN) End If Msg = “The current video mode is: “ Msg = Msg & vidWidth & “ X “ & vidHeight MsgBox Msg End Sub Here, I used the expression Val(Application.Version) > 5 to determine whether Excel is 16-bit or 32-bit. If the expression returns True, the code calls the 32-bit function; otherwise, it calls the 16-bit function. But Will It Work on a Mac? One of the most prevalent problems I hear about concerns Macintosh compatibility. Excel for Macintosh represents a very small proportion of the total Excel market, and many developers choose to simply ignore it. The good news is that Excel files are compatible across both platforms. The bad news is that the features supported are not identical, and VBA macro compatibility is far from perfect. You can write VBA code to determine which platform your application is running on. The following function accesses the OperatingSystem property of the Application object and returns True if the operating system is any version of Windows (that is, if the returned string contains the text “Win”): Function WindowsOS() As Boolean If Application.OperatingSystem like “*Win*” Then WindowsOS = True Else WindowsOS = False End If End Function 4799-2 ch26.F 6/11/01 9:47 AM Page 758 759 Chapter 26 ✦ Compatibility Issues Many subtle (and not so subtle) differences exist between the Windows versions and the Mac versions of Excel. Many of those differences are cosmetic (for example, different default fonts), but others are much more serious. For example, Excel for Macintosh doesn’t include ActiveX controls, and it uses “1904” as the default date system, so workbooks that use dates may be off by four years. Excel for Windows, by default, uses the 1900 date system. On the Macintosh, a date serial number of 1 refers to January 1, 1904; in Excel for Windows, that same serial number represents January 1, 1900. Another limitation concerns Windows API calls: They won’t work with Excel for Macintosh. If your application depends on such functions, you need to develop a workaround. If your code deals with paths and filenames, you need to construct your path with the appropriate path separator (a colon for the Macintosh, a backslash for Windows). A better approach is to avoid hard-coding the path separator character and use VBA to determine it. The following statement assigns the path separator character to a variable named PathSep: PathSep = Application.PathSeparator After this statement is executed, your code can use the PathSep variable in place of a hard-coded colon or backslash. Rather than try to make a single file compatible with both platforms, most develop- ers choose to develop on one platform (typically Excel for Windows) and then mod- ify the application so that it works on the Mac platform. In other words, you’ll probably need to maintain two separate versions of your application. There is only one way to make sure that your application is compatible with the Macintosh version of Excel: You must test it thoroughly on a Macintosh. And be prepared to develop some workarounds for routines that don’t work correctly. Creating an International Application The final compatibility issue deals with language issues and international settings. Excel is available in many different language versions. The following statement displays the country code for the version of Excel: MsgBox Application.International(xlCountryCode) The United States/English version of Excel has a country code of 1. Other country codes are listed in Table 26-2. 4799-2 ch26.F 6/11/01 9:47 AM Page 759 760 Part VII ✦ Other Topics Table 26-2 Excel Country Codes Language Country code English 1 Russian 7 Greek 30 Dutch 31 French 33 Spanish 34 Hungarian 36 Italian 39 Czech 42 Danish 45 Swedish 46 Norwegian 47 Polish 48 German 49 Portuguese (Brazil) 55 Thai 66 Japanese 81 Korean 82 Vietnamese 84 Simplified Chinese 86 Turkish 90 Indian 91 Urdu 92 Portuguese 351 Finnish 358 Traditional Chinese 886 Arabic 966 Hebrew 972 Farsi 982 4799-2 ch26.F 6/11/01 9:47 AM Page 760 761 Chapter 26 ✦ Compatibility Issues If your application will be used by those who speak another language, you need to ensure that the proper language is used in your dialog boxes. Also, you need to identify the user’s decimal and thousands separator characters. In the United States, these are almost always a period and a comma. But users in other countries may have their systems set up to use other characters. Yet another issue is date and time formats. The United States is one of the few countries that use the month/date/year format. If you’re developing an application that will be used only by people with your com- pany, you probably won’t need to be concerned with international compatibility. But if your company has offices throughout the world, or if you plan to distribute your application outside your country, you need to address a number of issues to ensure that your application will work properly. I discuss these issues in this section. Multilanguage applications An obvious consideration involves the language that is used in your application. For example, if you use one or more dialog boxes, you may want the text to appear in the language of the user. Fortunately, this is not too difficult (assuming, of course, that you can translate your text or know someone who can). The companion CD-ROM contains a dialog box wizard that is set up to use any of three languages: English, Spanish, or German. This example is based on a dialog box example presented in Chapter 14. The first step of the wizard contains three OptionButtons that enable the user to select a language. The text for the three languages is stored in a worksheet. Figures 26-1 through 26-3 show the UserForm displaying text in all three languages. Figure 26-1: The Wizard Demo, in English On the CD-ROM 4799-2 ch26.F 6/11/01 9:47 AM Page 761 762 Part VII ✦ Other Topics Figure 26-2: The Wizard Demo, in Spanish Figure 26-3: The Wizard Demo, in German VBA language considerations In general, you need not be concerned with the language that you write your VBA code in. Excel uses two object libraries: the Excel object library and the VBA object library. When you install Excel, it registers the English language version of these object libraries as the default libraries (this is true regardless of the language version of Excel). Using “local” properties If your code will display worksheet information such as a range address, you proba- bly want to use the local language. For example, the following statement displays the address of the selected range: MsgBox Selection.Address For international applications, a better approach is to use the AddressLocal property rather than the Address property: MsgBox Selection.AddressLocal 4799-2 ch26.F 6/11/01 9:47 AM Page 762 [...]... using VBA ✦ ✦ ✦ 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 767 27 C H A P T E R Manipulating Files with VBA ✦ ✦ ✦ ✦ In This Chapter M any applications that you develop for Excel require working with multiple files For example, you may need to get a listing of files in a directory, delete files, rename files, and so on Excel, of course, can import and export several types of text files In many cases, however, Excel s... Workbook_Open() Open Application.Path & “\excelusage.txt” _ For Append As #1 Print #1, “Started “ & Now Close #1 End Sub The procedure appends a new line to a file named excelusage.txt The new line contains the current date and time, and might look something like this: Started 03/ 09/ 00 9: 27:43 PM 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 783 Chapter 27 ✦ Manipulating Files with VBA The following subroutine is executed... use VBA to perform common file operations and work directly with text files Performing Common File Operations Excel provides you with a number of ways to perform common file operations: ✦ Use “traditional” VBA statements and functions This method works for all versions of Excel ✦ Use the FileSearch object, which is easier to use, and offers some distinct advantages This method works for Excel 97 and... Boolean If Dir(fname) “” Then _ FileExists = True _ Else FileExists = False End Function 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 7 69 Chapter 27 ✦ Manipulating Files with VBA The argument for the FileExists function consists of a full path and filename The function can be used in a worksheet, or called from a VBA procedure Determining whether a path exists The following function returns True if a specified... FileExists2(path, fname) As Boolean With Application.FileSearch NewSearch Filename = fname LookIn = path Execute If FoundFiles.Count = 1 Then FileExists2 = True Else FileExists2 = False End If End With End Function Note As far as I can tell, it’s not possible to use the FileSearch object to determine if a path exists 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 773 Chapter 27 ✦ Manipulating Files with VBA Locating files that... gigabytes in size Figure 27-2 shows an example of this error This problem doesn’t occur with Windows NT, or with Windows 2000 or later On the CD-ROM This workbook is available on the companion CD-ROM Figure 27-2: Output from the ShowDriveInfo procedure 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 775 Chapter 27 ✦ Manipulating Files with VBA Sub ShowDriveInfo() Dim FileSys, Drv Dim Row As Integer Set FileSys = CreateObject(“Scripting.FileSystemObject”)... Most VBA programmers simply designate a file number in their Open statement For example: Open “myfile.txt” For Input As #1 Then you can refer to the file in subsequent statements as #1 If a second file is opened while the first is still open, you would designate the second file as #2: Open “another.txt” For Input As #2 777 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 7 79 Chapter 27 ✦ Manipulating Files with VBA. .. import more than 256 columns of data into a workbook Sample code for exporting a range to HTML format ✦ ✦ ✦ ✦ 4 799 -2 ch27.F 768 6/11/01 9: 48 AM Page 768 Part VII ✦ Other Topics VBA file-related commands The VBA commands that you can use to work with files are summarized in Table 27-1 Table 27-1 VBA File-Related Commands Command What it does ChDir Changes the current directory ChDrive Changes the current... Write 4 799 -2 ch27.F 6/11/01 9: 48 AM Page 777 Chapter 27 ✦ Manipulating Files with VBA filenumber (Required) A file number ranging from 1 to 511 You can use the FreeFile function to get the next available file number reclength (Optional) The record length (for random access files) or the buffer size (for sequential access files) Reading a text file The basic procedure for reading a text file using VBA consists... cell Excel provides several named date and time formats, plus quite a few named number formats These are all described in the online help (search for named date/time formats or named numeric formats) Summary In this chapter, I discussed some general issues that you must consider if your Excel application must work with earlier versions of Excel, Excel for Macintosh, and foreign language versions of Excel . Formats Excel version Constant Value Excel 2.1 xlExcel2 16 Excel 3.0 xlExcel3 29 Excel 4.0 xlExcel4Workbook 35 Excel 5 xlExcel5 39 Excel 95 /97 xlExcel9 795 43 Excel in HTML format xlHtml 44 Excel. compatibility with Excel 95 , Excel 97 , and Excel 2000, you should use Excel 95 for development, and then test thoroughly using the other versions. If your application must support Excel 95 , you can’t. For example, Excel 97 , Excel 2000, Excel 2002, and Excel 98 for Macintosh all use the same file format — but compatibility issues are rampant. Just because a particular version of Excel can open

Ngày đăng: 14/08/2014, 02:20

Tài liệu cùng người dùng

Tài liệu liên quan