James M. Gordon
Microsoft Corporation
November 1999
Applies To: Microsoft FrontPage 2000
Summary: The Microsoft FrontPage 2000 Visual Basic object model provides support for manipulation of theme properties across a web or group of webs. This article describes how to change properties of themes programmatically, details the available options, and discusses the limitations of the object model for themes customization. (12 printed pages)
You can change a number of theme properties programmatically, including the active graphics property, the use of cascading style sheets or vivid colors, and the display of a background image. You can also change the theme applied to a Web, WebFile, or PageWindow object programmatically, and you can find out which theme is currently applied to the object by getting the ThemeName constant from the Web, WebFile, or PageWindow object. For a complete description of the constants, see the FpThemeProperties enumerated type, which is documented in the ThemeProperties property or the ApplyTheme method in the Microsoft® FrontPage® online Help. The ThemeProperties property and the ApplyTheme method are used in the sample application described in this article.
Although you can change several theme properties programmatically, the Microsoft Visual Basic® object model does not expose all of the functionality of the FrontPage theme design environment. The options for modifying colors, graphics, and text exposed by the FrontPage Themes dialog box (on the Format menu, click Theme) are not available through the object model.
The sample application, Theme Properties Wizard, demonstrates how to change theme properties for all open webs in the Webs collection or for a single web selected from a list of webs in the Webs collection. You can extend the capabilities of the sample application by providing a multiple-selection list box that lists all of the available webs and enables the user to select one or more webs from the list. You can also extend the application to apply custom themes to multiple webs, as discussed in the “Applying Custom Themes” section, later in this article.
The main advantage of the application is that it uses the object model to apply theme property changes to multiple webs without the need to apply the changes one web at a time by using the Themes dialog box. This can be particularly useful to web administrators who may need to roll out uniform theme changes across a number of webs.
To change themes and theme properties across multiple webs, the sample application iterates through all webs in the Webs collection and applies the selected theme or theme properties to each web. When the user selects a single web, the sample application applies the theme or theme properties only to the web selected from the drop-down list.
The sample application consists of a one-page form and a module containing the controls and code necessary to process changes to themes and theme properties for a specific web or across all webs in the Webs collection.
To create the form, from the Insert menu in the Microsoft Visual Basic Editor choose User Form. Microsoft Visual Basic creates a new form. Populate the new form with the objects and controls described in the following table. If you would like to learn more about Microsoft Forms, please refer to the Microsoft Forms Reference available through the Visual Basic online Help.
Object or Control Type |
Name |
Description |
Form | frmThemePropertiesWizard | The form has a Caption value of “Theme Properties Wizard”, a Height of 385, and a Width of 280. |
Frame | grpChooseWebs | This frame encloses the two option buttons and the drop-down list that enable the user to choose to apply changes to a single web or to all open webs. The frame’s Caption is “Choose Web(s) to Customize”. The frame has a Left value of 6, a Top value of 6, a Width of 264, and a Height of 96. The frame also has a TabIndex value of 0. |
Option Button | optAllWebs | This option button enables users to apply changes to all open webs. The option button’s Caption is “Apply Changes to All Open Webs”. The option button has a Left value of 6, a Top value of 36, a Width of 246, and a Height of 18. The option button has a TabIndex value of 1 and a Value of True. |
Option Button | optSingleWeb | This option button enables users to apply changes to a single web, which is selected from the lstOpenWebList combo box. The option button’s Caption is “Apply Changes to This Web Only”. The option button has a Left value of 6, a Top value of 12, a Width of 246, and a Height of 18. The option button has a TabIndex value of 2. Code behind this control populates the list in the lstOpenWebList combo box. |
Combo Box | lstOpenWebList | This combo box lists all webs in the Webs collection. The combo box is populated by code attached to the optSingleWeb option button. The combo box has a Left value of 6, a Top value of 60, a Width of 246, and a Height of 18. The combo box is disabled (Enabled is set to False) and it has a TabIndex value of 0. |
Frame | grpChooseTheme | This frame encloses the check box and the drop-down list that enable the user to choose to apply a new theme to a single web or to all open webs. The frame’s Caption is “Choose a New Theme for the Selected Web(s)”. The frame has a Left value of 6, a Top value of 108, a Width of 264, and a Height of 72. The frame also has a TabIndex value of 1. |
Check Box | cbxApplyNewTheme | This check box enables users to change the current theme for a single web or for all webs in the Webs collection. The Caption for this check box is “Apply New Theme”. The check box has a Left value of 6, a Top value of 12, a Width of 246, and a Height of 18. The check box also has a TabIndex value of 1. |
Combo Box | lstThemeList | This combo box lists all available, standard FrontPage themes. The combo box is populated by code attached to the cbxApplyNewTheme check box. The combo box has a Left value of 6, a Top value of 36, a Width of 246, and a Height of 18. The combo box is disabled (Enabled is set to False) and it has a TabIndex value of 0. |
Check Box | cbxActiveGraphics | This check box enables users to enable or disable active graphics. The Caption for this check box is “Enable Active Graphics?” The check box has a Left value of 6, a Top value of 198, a Width of 264, and a Height of 18. The check box also has a TabIndex value of 2. |
Check Box | cbxBackgroundImage | This check box enables users to display or disable the background image. The Caption for this check box is “Display Background Image?” The check box has a Left value of 6, a Top value of 222, a Width of 264, and a Height of 18. The check box also has a TabIndex value of 3. |
Check Box | cbxUseCSS | This check box enables users to use or disable cascading style sheets. The Caption for this check box is “Use Cascading Style Sheet?” The check box has a Left value of 6, a Top value of 246, a Width of 264, and a Height of 18. The check box also has a TabIndex value of 4. |
Check Box | cbxUseVividColors | This check box enables users to use vivid colors or normal colors. The Caption for this check box is “Use Vivid Colors?” The check box has a Left value of 6, a Top value of 270, a Width of 264, and a Height of 18. The check box also has a TabIndex value of 5. |
Command Button | cmdApplySettings | This command button processes the selections that the user made in the form. It is the default command button for the form and the Default property is set to True. The button’s Caption is “Apply Settings”. The button has a Left value of 36, a Top value of 312, a Width of 72, and a Height of 24. The button also has a TabIndex value of 6. |
Command Button | cmdClose | This command button closes the form. The button’s Caption is “Close”. The button has a Left value of 162, a Top value of 312, a Width of 72, and a Height of 24. The button also has a TabIndex value of 7. |
Most of the code needed to process the user’s selections is in the main procedure, ThemePropertiesWizard. There are a few things, however, that must be added to the form to enable and populate lists and to hand off control of the form to the main procedure. This section describes the code behind the form controls that makes it all work.
If the user chooses to set properties on a single web, you need to find out which web to process. An easy way to do this is to populate a drop-down list with all available, open webs on the user’s machine. There’s no need to populate the list if the user has chosen to process all webs, so you can use the Click event of the single web option button to populate the list, as in the following code:
Private Sub optSingleWeb_Click()
Dim strWebTitle As String
Dim intCounter As Integer
Dim intNumWebs As Integer
lstOpenWebList.Enabled = True
‘ Check to see if the list has already been populated.
If lstOpenWebList.ListCount < 1 Then
intCounter = 0
intNumWebs = Webs.Count
While intCounter < intNumWebs
strWebTitle = Webs(intCounter).Title
lstOpenWebList.AddItem (strWebTitle)
intCounter = intCounter + 1
Wend
Else
‘ The list has already been populated.
End If
‘ Set the ListIndex property to ensure that the
‘ drop-down list displays the first item.
lstOpenWebList.ListIndex = 0
End Sub
If the list has already been populated, you don’t want to add the items again. Use the ListCount method to determine whether the list is empty and skip the list population routine if there are any items in the list.
If the user chooses not to select a single web and selects the optAllWebs option button, you should disable the list to avoid confusion. (The list selection is not processed when optAllWebs is selected.) The following code, attached to the optAllWebs option button, disables the list.
Private Sub optAllWebs_Click()
lstOpenWebList.Enabled = False
End Sub
If the user chooses to change the theme for the web or webs, you need to find out which theme the user wants to apply. You can build a list of “friendly names” for all application-supplied themes and let the user select the theme from this list. The code that populates the list follows.
Private Sub cbxApplyNewTheme_Click()
‘ When the user checks this checkbox, the drop-down list
‘ is enabled and the user can choose a theme.
Dim thmFrontPageThemes As Theme
Dim intCounter As Integer
If cbxApplyNewTheme Then
lstThemeList.Enabled = True
Else
lstThemeList.Enabled = False
End If
‘ Check to see if the list has already been populated.
If lstThemeList.ListCount < 1 Then
For intCounter = 0 To (Application.Themes.Count - 1)
Set thmFrontPageThemes = Application.Themes(intCounter)
lstThemeList.AddItem thmFrontPageThemes.Label, intCounter
Next intCounter
‘ The list has already been populated.
End If
‘ Set the ListIndex property to ensure that the
‘ drop-down list displays the first item.
lstThemeList.ListIndex = 0
End Sub
The Apply Settings command button simply hands off the processing to the ThemePropertiesWizard subroutine and the Close command button ends the program. The code for each of these controls follows.
Private Sub cmdApplySettings_Click()
Call ThemePropertiesWizard
End Sub
Private Sub cmdClose_Click()
End
End Sub
The form and the main subroutine require that all webs to be processed be added to the Webs collection. One way to populate the Webs collection is to open all of the webs you want to process in FrontPage. The following procedure opens three webs, which adds the webs to the Webs collection. Alternatively, you can use the Add method of the Webs collection to populate the collection. (Be sure to replace “<your name>“ so the path on your machine is correct.)
Sub PopulateWebsCollection()
‘ This procedure opens three webs.
Webs.Open (“C:/WINNT/Profiles/<your name>/Personal/My Webs/myweb”)
Webs.Open (“C:/WINNT/Profiles/<your name>/Personal/My Webs/myweb2”)
Webs.Open (“C:/WINNT/Profiles/<your name>/Personal/My Webs/myweb3”)
End Sub
The main processing procedure, ThemePropertiesWizard, executes when the user clicks the Apply Settings button on the Theme Properties Wizard form. The procedure evaluates the controls on the form to determine (1) whether the user wants to apply changes to all open webs or to a single web, (2) whether the user wants to apply a new theme and, if so, which theme, and (3) whether the user wants to set one of the theme properties (active graphics, background image, cascading style sheet, or vivid colors). When the procedure completes successfully, it displays a message box. When the user clicks the Close button, the form closes and does not save the user’s settings. The code for the ThemePropertiesWizard procedure follows.
Sub ThemePropertiesWizard()
‘ This procedure contains all of the code for the Wizard.
Dim webSelectedWeb As Web
Dim strCurrentTheme As String
Dim strNewTheme As String
Dim strThemeName As String
Dim intCounter As Integer
Dim intMaxWebs As Integer
Dim lngFpThemeProperties As Long
‘ The first thing we need to determine is whether the user
‘ wants to change properties in all open webs or a single web.
‘ To do this, we test the value of the optAllWebs and
‘ optSingleWeb option buttons. If optSingleWeb is selected,
‘ we must display a drop-down list of available webs;
‘ if optAllWebs is selected, we leave the drop-down list
‘ disabled and continue with the procedure, applying
‘ changes to all open webs.
intCounter = 0
intMaxWebs = Webs.Count
lngFpThemeProperties = 0
strThemeName = ““
While intCounter < intMaxWebs
If frmThemePropertiesWizard.optSingleWeb Then
Set webSelectedWeb = _
Webs(frmThemePropertiesWizard.lstOpenWebList.Value)
intMaxWebs = 1
Else
Set webSelectedWeb = Webs(intCounter)
End If
‘ Now that we have set webSelectedWeb to the selected web or the
‘ first web in the Webs collection, we need to see if the user
‘ wants to change the theme. If a new theme has been selected, we
‘ update the strThemeName variable with the user’s selection.
‘ Otherwise, we will apply the same theme.
If frmThemePropertiesWizard.cbxApplyNewTheme.Value = True Then
strThemeName = Application.Themes _
(frmThemePropertiesWizard.lstThemeList.ListIndex).Name
Else
strThemeName = webSelectedWeb.ThemeProperties(fpThemeName)
End If
‘ We need to check the four checkboxes and apply the
‘ selected values to the web(s) by using the ApplyTheme method.
If frmThemePropertiesWizard.cbxActiveGraphics = True Then
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeActiveGraphics
Else
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeNormalGraphics
End If
If frmThemePropertiesWizard.cbxBackgroundImage = True Then
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeBackgroundImage
Else
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeNoBackgroundImage
End If
If frmThemePropertiesWizard.cbxUseCSS = True Then
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeCSS
Else
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeNoCSS
End If
If frmThemePropertiesWizard.cbxUseVividColors = True Then
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeVividColors
Else
lngFpThemeProperties = lngFpThemeProperties + _
fpThemeNormalColors
End If
‘ Next, we apply the changes to the selected web.
Call webSelectedWeb.ApplyTheme(strThemeName, _
lngFpThemeProperties)
‘ As our last order of business, we need to increment
‘ the counter in case the user has decided to apply theme
‘ and theme property changes to all open webs.
intCounter = intCounter + 1
Wend
‘ When the wizard has finished applying all changes,
‘ we notify the user with a simple message box.
MsgBox “The Theme Properties Wizard has completed successfully.”, _
, “Theme Properties Wizard”
End Sub
You can apply themes that you create or modify yourself to any web or webs by creating a custom theme list and enabling the user of the application to choose a theme name from the list. Instead of populating the cbxApplyNewTheme combo box with the list of Application.Themes.Name
constants, you can build a list of friendly names for your own custom themes by replacing the code for the cbxApplyNewTheme_Click()
event with the following code:
Private Sub cbxApplyNewTheme_Click()
‘ When the user checks this checkbox, the drop-down list
‘ is enabled and the user can choose a theme.
If cbxApplyNewTheme Then
lstThemeList.Enabled = True
Else
lstThemeList.Enabled = False
End If
‘ The list of custom themes is populated when
‘ the user checks this checkbox.
‘ Check to see if the list has already been populated.
If lstThemeList.ListCount < 1 Then
lstThemeList.AddItem (“Marketing Department Custom Theme”)
lstThemeList.AddItem (“R&D Department Custom Theme”)
lstThemeList.AddItem (“Sales Department Custom Theme”)
Else
‘ The list has already been populated.
End If
End Sub
Then, to process the user’s selection, you can use the following code to capture the user’s selection. Simply replace the code that processes the list box selection with the following.
If frmThemeAttributeWizard.cbxApplyNewTheme.Value = True Then
Select Case frmThemeAttributeWizard.lstThemeList.ListIndex
Case 0
strThemeName = “mktgcust”
Case 1
strThemeName = “RDcust”
Case 2
strThemeName = “salecust”
Case Else
strThemeName = webSelectedWeb.ThemeProperties(fpThemeName)
End Select
Else
strThemeName = webSelectedWeb.ThemeProperties(fpThemeName)
End If
See also: | Microsoft Office 2000/Visual Basic Programmer’s Guide (http://mspress.microsoft.com/books/2320.htm): This comprehensive book covers programming for Office by using the Visual Basic for Applications programming language.
Microsoft Office Developer Web site (http://msdn.microsoft.com/officedev/): Look here for the latest information about developing applications with Microsoft Office. |