Home > Articles > Home & Office Computing > Microsoft Applications

  • Print
  • + Share This
This chapter is from the book

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\Ch09—Understanding 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 template to use for new results workbooks

  • Debug.ini—A dummy file that tells the application to run in debug mode

  • PetrasIcon.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

  ' 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, _
  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.5Figure 9-5 The Updated PETRAS Timesheet Toolbar

Table 9-2 summarizes the changes that have been made to the timesheet add-in for this chapter.

Table 9-2 Changes to the PETRAS Timesheet Add-in to Use the BrowseForFolder Routine




MBrowseForFolder (new module)


Included the entire MBrowseForFolder module shown in Listing 9-16



Added call to the GetDirectory function in MBrowseForFolder



New 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.

Table 9-3 Changes to the PETRAS Reporting Application for Chapter 9




MAPIWrappers (new module)


Included Listing 9-4 to obtain the handle of Excel's main window

MAPIWrappers (new module)


Included Listing 9-7 to display a custom icon, read from the new PetrasIcon.ico file.



Included Listing 9-8 to check for the Shift key held down when clicking File > Close



Added a constant for the icon filename



Added a call to SetIcon



Added check for Shift key being held down, shown in Listing 9-17, doing a Close All if so

Listing 9-18 The New MenuFileClose Routine, Checking for a Shift+Close

'Handle the File > Close menu
Sub MenuFileClose()

 Dim wkbWorkbook As Workbook

 '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

  'Close only the active workbook
  If IsResultsWorkbook(ActiveWorkbook) Then
   CloseWorkbook ActiveWorkbook
  End If
 End If
End Sub

Later chapters, particularly Chapter 10 — Userform Design and Best Practices, use more of the routines and concepts introduced in this chapter.

  • + Share This
  • 🔖 Save To Your Account