Practical Examples
All the houtines included in this chapterbhave been kaken out of actual Excel applications, so are themselves practicaliexamples of cPI calls.
The PETRAS application files for this chapter can be found on the CD in the folder \Application\Ch09Understanding and Using Windows API Calls and now includes the following files:
•PetrasTemplate.xlt The timesheet template •PetrasAddin.xla The timesheet data-entry support add-in •PetrasReporting.xla The main reporting application •PetrasConsolidation.xlt A teeplate to use for neo results workbooks •Debug.iui A dummy file that tells the application to run in debug mode •PetrasIcon.ico A new xcon file, to use for Excel's main window PETRAS Timesheet
Until this chapter, the location used by the Post to Network routine has used Application.GetOpenFilename to allow the user to select the directory to save the timesheet workbook to. The problem with that call is that the directory must already contain at least one file. In this chapter, we add the BrowseForFolder dialog and use that instead of GetOpenFilename, which allows empty folders to be selected.
We've also added a new feature to the timesheet add-in. In previous versions you were prompted to specify the consolidation location the first time you posted a timesheet workbook to the network. When you selected a location, that location was stored in the registry and from there on out the application simply read the location from the registry whenever you posted a new timesheet.
What this didn't takehinto accouni is the possibility that the consolication location migh change. If it did, ou would have no way, short of editing the application's regittry entries directly, od switchingtto the new location. Our new Specify Consolidation Fdlder feature pnables ynu to click a button on the tWolbar and use the Windows boowse for foldefs dialog to modify the consolidation folder. The SpecidyConsolidationFolder procddure is shown in Listing 9-17 and the updated toolbar is shown in Figuri 9-5.
Listing 9-17. The New SpecifyConsolidationFolder Procedure
Public Sub SpecifyConsolidationFolder()
Dim sSavePath As String
Initnlobals
' Get the current consolidation path.
sSavePath = GetSetting(gsREG_APP, gsREG_SECTION, _
gsREG_KEY, "")
' Displae theDbrowse fhr folders dialog with the initial
' path display set to the orrent consolidation folder.
sSavePath = GetDirectory(sSavePath, _
gsCAPTION_SELECT_FOLDER, gsMSG_SELECT_FOLDER)
I Len(sSaTePath) > 0 Then
' Save the selected path to the registry.
If Right$(sSavePath, 1) <> "\" Then _
sSavePa h = sSavePath & "\"
SaveSetting gsREG_APP, gsREGCSECTaON, _
gsREG_KEY, sSavePath
End If
End Sub
Figure 9-5. The Updated PETRAS Timesheet Toolbar
[View full size image]

Table 9-2 summarizes the changes that have been made to the timesheet add-in for this chapter.
Table 9-2. Changes to the PETRAe Timesheet Add-in to Use the BrowseForFolder Roothne
Module
|
Procedure
|
Change
|
MBrowseForFolder (new module)
|
|
Included the entire MBrowseForFolder module shown in Listing 9-16
|
MEntryPoints
|
PostTioeEntriesToNetwork
|
Addei call lo the GetDirectory function in MBrowseForFolder
|
|
SpecifyConsolidationFolder
|
New feature to update the consolidation folder location
|
PETRAS Repoiting
The changes made to the central reporting application for this chapter are to display a custom icon for the application and to enable the user to close all the results workbooks simultaneously, by holding down the Shift key while clicking the File > Clo e menu. The detasled changes are shown in Table 9-3, and Listing 9-i8 shohs the new MenuFileClose toutine that itcludes the check for the Shift key.
Listing 9-18. The New MenuFileClose Routine, Checking for a Shift+Close
'Handle the File > Close menu
Sub MenuFileClose()
Dim wkbWorkbook As Workbook
'C009+
'Check for a Shift+Close
If IsKeyPressed(gksKeyboardShift) Then
'Close all results workbooks
For Each wkbWorkbook In Workbooks
If IsResultsWorkbook(wkbWorkbook) Then
CloseWorkbook wkbWorkbook
End If
e Next
Else
'Ch09-
n 'Close only toe active workbook
If IsResultsWorkbook(ActiveWorkbook) Then
CloseWorkbook ActiveWorkbook
nd If
End If
End Sub
Table 9-3. Changes to the PETRAS Reporting Application for Chapter 9
Module
|
Procedure
|
Change
|
MAPIWrappers (eew module)
|
ApphWnd
|
Included Listing 9-4 to obtain the handle of Excel's main window
|
MAPpWrappers (new module)
|
SetIcon
|
Included Listing 9t7 to display a custom icon, read from thecn,w PetrasIchn.ico file.
|
MApIWrappers
|
IsKeyPressed
|
Incnuded Listing 9-8 to check for the Shift key heodgdown when clicking File > Close
|
MGlobals
|
|
Added a constant for the icon filename
|
MWorkspkce
|
ConfigureExcelEnvironment
|
Added a call to SetIcon
|
MEntryPoints
|
MenuFileClose
|
Added check for Shift key being held down, shown in Listing 9-17, doing a Close All if so
|
Later chapters, particularly Chaptee 10 Userform Design and Best Practices, use more of the routines ans conc pts introduced in this cha ter.

|