Practical Examples
All the routines included in this chapter have been taken out of actual Excel applications, so are themselves practical examples of API 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 templatePetrasAddin.xla
The timesheet data-entry support add-inPetrasReporting.xla
The main reporting applicationPetrasConsolidation.xlt
A template to use for new results workbooksDebug.ini
A dummy file that tells the application to run in debug modePetrasIcon.ico
A new icon 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 take into account is the possibility that the consolidation location might change. If it did, you would have no way, short of editing the application''s registry entries directly, of switching to the new location. Our new Specify Consolidation Folder feature enables you to click a button on the toolbar and use the Windows browse for folders dialog to modify the consolidation folder. The SpecifyConsolidationFolder procedure is shown in Listing 9-17 and the updated toolbar is shown in Figure 9-5.
Listing 9-17. The New SpecifyConsolidationFolder Procedure
Public Sub SpecifyConsolidationFolder()
Dim sSavePath As String
InitGlobals
'' Get the current consolidation path.
sSavePath = GetSetting(gsREG_APP, gsREG_SECTION, _
gsREG_KEY, ")
'' Display the browse for folders dialog with the initial
'' path display set to the current consolidation folder.
sSavePath = GetDirectory(sSavePath, _
gsCAPTION_SELECT_FOLDER, gsMSG_SELECT_FOLDER)
If Len(sSavePath) > 0 Then
'' Save the selected path to the registry.
If Right$(sSavePath, 1) <> "\" Then _
sSavePath = sSavePath & "\"
SaveSetting gsREG_APP, gsREG_SECTION, _
gsREG_KEY, sSavePath
End If
End Sub
Figure 9-5. The Updated PETRAS Timesheet Toolbar
Listing 9-16MEntryPointsPostTimeEntriesToNetworkAdded call to the GetDirectory function in MBrowseForFolderSpecifyConsolidationFolderNew feature to update the consolidation folder location
PETRAS Reporting
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 > Close menu. The detailed changes are shown in Table 9-3, and Listing 9-18 shows the new MenuFileClose routine that includes the check for the Shift key.
Listing 9-18. The New MenuFileClose Routine, Checking for a Shift+Close
Chapter 9
''Handle the File > Close menu
Sub MenuFileClose()
Dim wkbWorkbook As Workbook
''Ch09+
''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
Next
Else
''Ch09-
''Close only the active workbook
If IsResultsWorkbook(ActiveWorkbook) Then
CloseWorkbook ActiveWorkbook
End If
End If
End Sub
