|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
One notable deficiency of FrontPage 2000-2003 (versions before 2000 did not have VBA programmability) is that there is no macro recording capability. Theoretically macros can be used to do frequently performed tasks in FrontPage. But details are not trivial and very difficult to implement without good knowledge of both VBA, Javascript and DOM.
For example there is a capability to control search from macros: . Sounds great but the only example of xml representation of the query that controls behavior of the search module is the following:
strQuery = "<?xml version=""1.0""?>" & _
"<fpquery version=""1.0"">" & _
"<find tag=""b"">" & _
"<rule type=""insideTag"" tag=""td"" />" & _
"</find>" & _
"<replace type=""changeTag"" tag=""i"" />" & _
"</fpquery>"
Microsoft does not provide any additional information other then this one example. If we try to search Frontpage 2003 help we will get the same example
The following example searches for TD elements in the current selection and adds the align attribute with a value of "center."
Dim objSearch As SearchInfo Dim objRange As IHTMLTxtRange Dim blnMatches As Boolean Dim strQuery As String strQuery = "<?xml version=""1.0""?><fpquery version=""1.0"">" & _ "<find tag=""td""><rule type=""insideTag"" tag=""table"" />" & _ "</find><replace type=""setAttribute"" attribute=""align"" " & _ "value=""center""/></fpquery>" Set objRange = ActiveDocument.selection.createRange Set objSearch = Application.CreateSearchInfo objSearch.QueryContents = strQuery blnMatches = Application.ActiveDocument.Find(objSearch, Nothing, objRange) If blnMatches = True Then objRange.Select
Only after a lot of digging you understand that query can be saved from the FrontPage menu and can be pasted into the macro. Actually just this capability can be used instead of writing macros. But let's return to the macro. The code above simply does not work.
Still you can write code in Frontpage's Visual Basic Editor (Alt-F11 or Tools/Macro/Visual Basic Editor). Goof luck. Open the FrontPage Visual Basic Editor. Once inside the VBE, you can begin to write your code. As with other Office applications you can add additional code modules, forms, and even classes to your macro project. However, if you plan to share any of the macros, you will likely want to create an add-in. For more information on add-ins, see About Add-ins.
Macros are stored in the file called Microsoft FrontPage.fpm in the FrontPage macros folder.
On computer running Microsoft Windows 2000 and 2003, this is usually
C:\Documents and Settings\[user name]\Application Data\Microsoft\FrontPage\Macros.
It it closed when Frontpage is closed so you can export it to other computers.
Here is a useful macro that works and that demonstrates how to insert text:
' navigationTree - Indented List from FrontPage Navigation
' Copyright (C) 2002 Stephen C. Travis
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
' or visit their website at http://www.fsf.org/licenses/gpl.txt
'
Dim myHTML As String
Sub navigationTree()
If Not FrontPage.ActiveDocument Is Nothing Then
If FrontPage.ActivePageWindow.ViewMode = fpPageViewNormal Then
Dim thisNode As NavigationNode
Set thisNode = ActiveWeb.HomeNavigationNode
myHTML = "<ul>"
Call getChildren(thisNode)
myHTML = myHTML & "</ul>"
Set myRange = ActiveDocument.selection.createRange
myRange.collapse (True)
myRange.pasteHTML (myHTML)
End If
End If
End Sub
Private Sub getChildren(thisNode)
myHTML = myHTML & "<li>"
myHTML = myHTML & "<a href=""" & MakeRel(ActiveDocument, thisNode.Url) & """>" & thisNode.File.Title & "</a>"
For i = 0 To thisNode.Children.count - 1
myHTML = myHTML & "<ul>"
getChildren (thisNode.Children(i))
myHTML = myHTML & "</ul>"
Next
myHTML = myHTML & "</li>"
End Sub
Below in another useful (and working) sample: Save All command - a command that saves all the open pages in Page view - to your File menu:
Sub Save_All() 'Name of the macro 'Checks that the document is in Page view in FrontPage 'And that at least one page is open If (Application.ActiveWebWindow.ViewMode = fpWebViewPage _ And ActiveWebWindow.PageWindows.Count > 0) Then Dim activePage, page As PageWindow 'Declare variables 'Set the variable ActivePage to the 'current active page Set activePage = ActivePageWindow 'Loop through all open pages For Each page In ActiveWebWindow.PageWindows page.Activate 'Click the Save button for each page CommandBars("Add Command").Controls("&File"). _ Controls("&Save").Execute Next 'Display the page you were working on when 'the Save All command was clicked activePage.Activate End If End Sub
- In FrontPage, click Customize on the Tools menu.
- Click the Commands tab, and then select Macros in the Categories list.
- Drag and drop the entry Custom Menu Item from the Commands list to the File menu beneath the Save command.
- Click Modify Selection, change the name to Save A&ll, and then press ENTER. Note The ampersand in the word All makes the first "L" a keyboard shortcut. After the command is added to the File menu, you can press ALT+L to save all open pages.
- Click Modify Selection again, and then choose Assign Macro.
- Choose Save_All from the list, and then click OK.
- Click Close.
The Save All command is added to the File menu.
Add-ins are supplemental programs that extend the capabilities of Microsoft FrontPage by adding custom commands and specialized features. You can obtain add-ins from the Microsoft Office Online Web site, third-party vendors, or you can write your own custom add-in programs by using Microsoft Visual Basic for Applications.
To conserve memory and increase the speed of FrontPage, it's a good idea to unload add-in programs you don't use often. When you unload an add-in, its features and commands are removed from FrontPage, but the add-in file itself remains on your computer for easy reloading.
Developers can use Component Object Model (COM) add-ins, which provide additional functionality in a variety of programming languages, including Microsoft Visual Basic, Microsoft Visual C++, and Microsoft Visual J++. As a developer, you'll find information about designing COM add-ins in Visual Basic Help. While you are developing and testing, you can load or unload a COM add-in in FrontPage before you have a working installation program for your add-in.
Note Do not confuse add-ins with plug-ins. A plug-in is one of a set of software modules that integrate into Web browsers to offer a range of interactive and multimedia capabilities.
Security Security vulnerabilities in external files or controls may extend to Web pages that use those items. For example, external style sheets (files with a .css extension), script files (files with a .js extension), custom ASP.NET controls, or other items, may pose a security risk. Be sure your style sheets, add-ins, themes, executables, scripts, controls, or other files come from trusted sources.
|
|
1.1.1 Accessing the Position of the Insertion Point
Accessing specific elements is relatively simple. However, usually you want to access the element that is at the position of the insertion point. You may want to access the text and HTML elements that are selected or perhaps just insert text or HTML into the page without replacing any existing text. There are two ways to do this. One way uses the activeElement property, and the other uses an IHTMLSelectionObject object.
1.1.1.1 Accessing the active element
Sometimes you might want to access the currently selected element or the element that is at the position of the insertion point. To do this, you use the activeElement property of the FPHTMLDocument or IHTMLDocument object. The activeElement property returns an IHTMLElement object. The following example uses the activeElement property to create an object variable that accesses the element at the insertion point.
Dim objElement As IHTMLElement Set objElement = ActiveDocument.activeElementThe previous example uses an IHTMLElement object to store a pointer to the active element. However, sometimes you might want to use the exact type of object. In this case you need to know the type of the active element. To do this, you can use the tagName property to return a String that contains the name of the tag. Then you need to determine which FPHTML object corresponds to that element. The following example shows a Select statement that sets the object variable based on the value of the tagName property.
Dim strTagName As String strTagName = ActiveDocument.activeElement.TagName Select Case LCase(strTagName) Case "body" Dim objBody As FPHTMLBody Set objBody = ActiveDocument.activeElement Case "p" Dim objP As FPHTMLParaElement Set objP = ActiveDocument.activeElement Case "div" Dim objDiv As FPHTMLDivElement Set objDiv = ActiveDocument.activeElement Case "span" Dim objSpan As FPHTMLSpanElement Set objSpan = ActiveDocument.activeElement Case Else Dim objElement As IHTMLElement Set objElement = ActiveDocument.activeElement End Select1.1.1.2 Accessing selected text and HTML
To access the selected text or the position of the insertion point, use the createRange method of the IHTMLSelectionObject object to create an IHTMLTxtRange object. To access the IHTMLSelectionObject object for a document, use the selection property of the FPHTMLDocument object.
The following example creates an IHTMLTxtRange object with the currently selected text. If no text is selected, the example accesses the position of the insertion point.
Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRangeYou can also create an IHTMLTxtRange object by using the createTextRange method of the IHTMLBodyElement or FPHTMLBody object. This approach places the entire body of the page into the text range.
Note You can access the createTextRange method from the FPHTMLButtonElement, IHTMLButtonElement, FPHTMLInputButtonElement, IHTMLInputButtonElement, FPHTMLInputHiddenElement, IHTMLInputHiddenElement, FPHTMLInputTextElement, IHTMLInputTextElement, FPHTMLTextAreaElement, and IHTMLTextAreaElement objects; however, doing so doesn't have an effective use in this case, so this article doesn't discuss the createTextRange method in relation to these objects.
1.1.1.3 Inserting text at the insertion point
After you create a text range with the selection, you can insert text or HTML at the location of the insertion point and replace the current selection with text or HTML.
You use the text property to insert text at the insertion point, as shown in the following example.
Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRange objSelection.Text = "The quick red fox jumped over the lazy brown dog."1.1.1.4 Inserting HTML at the insertion point
If you use the text property to insert HTML, FrontPage converts the tags and special symbols to their HTML equivalents. Therefore, to insert HTML into a page, use the pasteHTML method of the IHTMLTxtRange object, as shown in the following example.
Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRange objSelection.pasteHTML "<b>The quick red fox jumped over the lazy brown dog.</b>"The IHTMLTxtRange object includes an htmlText property that you can use to read the selected HTML and text. However, the htmlText property is read-only; therefore, you can't use it to replace the selected HTML. In this case, use the htmlText property to read the selected text and HTML and the pasteHTML method to replace the selected text and HTML with HTML code. The following example shows how you might do this.
Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRange objSelection.pasteHTML "<div>" & objSelection.htmlText & "</div>"Note The preceding code adds a <div> tag around the currently selected text. You should know and understand the HTML specification and HTML block elements to ensure the VBA code actually generates valid HTML. For example, adding only a table cell without the corresponding table row and table tags in the middle of a paragraph generates invalid HTML.
1.1.1.5 Collapsing a text range
Sometimes a user might have selected text, but the code that runs assumes that the user has no text selected. If the code that you need to write requires that the user has no text selected (for example, if you are inserting text or code that replaces selected text or code), you can use the collapse method to collapse the range. The collapse method has an optional Boolean parameter named start that indicates whether the range collapses to the start of the range or the end of the range. The default value is True, which indicates that the range collapses at the beginning of the range; a value of False indicates that the range collapses at the end of the range. The following example uses the collapse method to collapse the range at the end of the selected range and then inserts HTML code.
Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRange objSelection.collapse False objSelection.pasteHTML "<div>The quick red fox jumped over the lazy brown dog.</div>"1.1.2 Inserting Text and HTML into a Page
In addition to using the IHTMLTxtRange object to insert text and HTML code, you can also use the insertAdjacentText and insertAdjacentHTML methods. The insertAdjacentText and insertAdjacentHTML methods are members of the IHTMLElement object and therefore of most IHTML and FPHTML objects. When you have a handle to the appropriate object, you can use the insertAdjacentText or insertAdjacentHTML method to insert text or HTML into the document.
Both the insertAdjacentText and insertAdjacentHTML methods have a where parameter that takes a string and indicates whether the text or HTML is inserted before or after the opening or closing tag for the element. The possible values are "beforeBegin", "afterBegin", "beforeEnd", and "afterEnd". The following example inserts HTML before the closing tag if the active element is the BODY element and pastes it after the closing tag for all other elements.
Dim objElement As IHTMLElement Dim strTagName As String Set objElement = ActiveDocument.activeElement strTagName = objElement.TagName Select Case strTagName Case "body" objElement.insertAdjacentHTML where:="beforeEnd", _ HTML:="<div>The quick red fox jumped over the lazy brown dog.</div>" Case Else objElement.insertAdjacentHTML where:="afterEnd", _ HTML:="<div>The quick red fox jumped over the lazy brown dog.</div>" End Select1.1.3 Programmatically Selecting Text and Code
At times you may want to programmatically select text and code. You can do this using the select method of the IHTMLTxtRange object. Regardless of whether any text or code is selected, you can specify the element to select by using the moveToElementText method of the IHTMLTxtRange object, or you can select a specified range of text and code by using the move, moveEnd, or moveStart methods of the IHTMLTxtRange object.
The following example uses the moveToElementText and select methods to select the active element and the text it contains. If the selection spans several elements, the active element is the element that contains all other selected elements.
Dim objRange As IHTMLTxtRange Dim objElement As IHTMLElement Set objRange = ActiveDocument.Selection.createRange Set objElement = ActiveDocument.activeElement objRange.moveToElementText objElement objRange.Select1.1.4 Inserting META Data, Styles, and Scripts
As mentioned previously, the HEAD element of a Web page doesn't have an accessor property that you can use to access it and all child elements. There is also no corresponding FPHTML or IHTML object that you can use to access elements that are typically in the HEAD section of a Web page. To access some child elements for the HEAD element, such as the TITLE element, you can use the corresponding property of the FPHTMLDocument object, such as the title property.
Sometimes you might need to insert content into the HEAD section of a Web page, such as scripts, META data, and styles; for this reason you may need to access the HEAD element. To do this, you can use the method shown previously and create an IHTMLElement object variable, as shown in the following example.
Dim objHead As IHTMLElement Set objHead = ActiveDocument.all.tags("head").Item(0)When you have access to the HEAD element, you can use the all method (as described previously) to access the child META, STYLE, and SCRIPT elements or use the InsertAdjacentHTML method to insert new elements into the HEAD section of a Web page. For example, you might need to add META data or insert a SCRIPT element. For more information and code examples, see Accessing Scripts and Adding Styles to Specified Elements.
[Dec 7, 2004] Automating Repetitive Tasks in FrontPage 2003
Do you ever find yourself performing the same steps over and over again? Perhaps you publish the same Web site to several locations and find yourself repeatedly typing the Web addresses into the Publish dialog box every time you need to publish the site. Or perhaps you want to add a reference to an external style sheet to every page in your Web site and don't want to open every page to paste the necessary HTML. Macros can help.Macros allow you to automate repetitive tasks. If you've worked with other Microsoft® Office applications, you've probably worked with macros. In fact, several Office applications have a macro recorder that you can use to create macros for those applications. Although you can't record macros in FrontPage, you can still create macros to automate your tasks, and you can create some powerful macros. With a little instruction, you can begin to develop your own collection of macros.
[Nov 30, 2000] EDITING ASP FILES WITH MICROSOFT FRONTPAGE 2000
Did you know that you could safely edit Active Server Pages (ASP) files in the Microsoft FrontPage 2000 Web site creation and management tool? New and improved handling of ASP in FrontPage 2000, combined with a few basic rules for implementing ASP, allows developers to feel comfortable letting users edit pages that include ASP code. This article describes how FrontPage determines whether to display pages containing ASP code in Normal (WYSIWYG--what you see is what you get) View or HTML View, and describes how to create ASP code that allows users to safely edit the Web pages containing the ASP code in Normal View.
Product Home Page for
Microsoft FrontPage
Product Overview of Microsoft FrontPage 2000
Free Downloads & Resources for Microsoft FrontPage Users
Online Support for Microsoft FrontPage
Publishing A Web Site with Microsoft FrontPage
Newsgroups for Microsoft FrontPage
Training & Certification for Microsoft FrontPage
Books for Microsoft FrontPage
Shop for Microsoft FrontPage 2000
Useful Shortcut Keys in FrontPage 2000
CONTROL WEB PAGE DEFAULT SETTINGS
As users of Microsoft Word are probably aware, every time you create a new document, Word bases that document on a template called Normal.dot. This template includes such information as the default font Word uses and the standard margins for pages. What you may not be aware of, however, is that Microsoft FrontPage uses a similar template whenever you create a new HTML document. By modifying this template, you can control many of the default settings on your HTML pages. In this article, you can see where FrontPage stores its template and how you can edit it:
Position and Layer Graphics and Text—Down to the Pixel—with FrontPage 2000
Using new positioning and layering tools in Microsoft FrontPage 2000, you can now place elements on your Web pages exactly where you want them. These new tools enable you to layer Web objects on top of one another, group them together as a unit, position graphics and text by setting precise pixel coordinates, wrap text around an image or text block, and more.
USEFUL SHORTCUT KEYS IN FRONTPAGE 2000
Do you find that using the keyboard is sometimes quicker than using your mouse? Shortcut keys can help you bypass menus and carry out commands directly. You can use shortcut keys in many ways with FrontPage, from accessing commands and toolbar buttons to outlining and editing information. Shortcut keys are sometimes listed next to the command name on menus. For more details, please go to:
http://officeupdate.microsoft.com/2000/focus/articles/FrontPageKeys.htm
THE JAVASCRIPT SOURCE TIP: IMAGE SLIDE SHOW
When you cut and paste this script in HTML view it will enable you to rotate a series of images in a page as a slideshow. You can customize the images and the time between each image. This script works best if all the images are the same dimension! Learn more at:
http://javascript.internet.com/miscellaneous/image-slideshow.html
http://www.microsoft.com/Frontpage/
for information and resources on Microsoft FrontPage
version 2002.
http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28001169
for information and resources on scripting.
http://www.microsoft.com/frontpage/downloads/addin/default.asp
for information and downloads of add-ins for FrontPage at the new FrontPage
Add-in Center.
http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/MSDN-FILES/027/001/710/msdncompositedoc.xml
for the new Software Development Kit (SDK) for Microsoft FrontPage 2002. This
SDK contains not only basic help to get you started developing solutions to
use with FrontPage, but also many articles and samples you can use to create
your own custom solutions.
See vbafp4.chm and vbafpom4.chm files for reference topics covering all FrontPage object model language elements, as well as topics such as “Creating Web Sites Programmatically.” The default installation path for these files is C:\Program Files\Microsoft Office\Office\1033.
This Knowledge Base article demonstrates how you can set the publish status
of files in your FrontPage 2000 web by using Microsoft Visual Basic® for Applications
(VBA).
This article on Microsoft Office Update shows how to build a procedure that
explains how to add the Save All command—a command that saves all the
open pages in Page view—to your File menu.
How to Access Most Recently Used List Using VBA
This Knowledge Base article shows you how to use Visual Basic for Applications
(VBA) code and the Office CommandBar object model to get access to most-recently-used
(MRU) items from within FrontPage.
This section of the FrontPage 2000 documentation in the MSDN Library provides
a thorough introduction to the use of Visual Basic for Applications in creating
Web sites with FrontPage 2000.
This Knowledge Base article explains how to use Visual Basic for Applications
to loop through all files in a FrontPage web.
This tip in the MSDN Member Gazette describes how to use Visual Basic
for Applications code to insert text at the editor’s current insertion point.
This Knowledge Base article explains how to create the appearance of a new instance
of FrontPage by using Visual Basic for Applications.
Accessing Framesets with Microsoft Visual Basic
This section of the FrontPage 2000 documentation in the MSDN Library discusses
how to use Visual Basic for Applications to program the content of frames in
FrontPage 2000.
How to Programmatically Save All Open Pages That Have Changed
This Knowledge Base article describes how to write a macro in FrontPage 2000
that saves all open pages that have been changed.
How to Change Capitalization of File and Folder Names by Using VBA
http://support.microsoft.com/support/kb/articles/Q238/4/10.ASP
The sample code in this Knowledge Base article changes the capitalization
of the file and folder names in the active web to lowercase.
More Support & Troubleshooting results Technical Resources SDKs, resource kits, reference libraries, script centers, white papers, technical articles, product documentation...
Visual Basic (VBA): Securing Microsoft Office Documents
FrontPage Macros by Stephen C. Travis
| All Files - | create a table at the cursor location containing a list of all files in the web and their properties (similar to the 'All Files' report). |
| Sort Table - | sorts a table in Normal View on the column and beginning in the row that the cursor is located. |
| Breadcrumb Trail - | creates a breadcrumb trail from the FrontPage navigation structure. |
| Outline - | automatically numbering heading tags (eg. 1.1.2 for an H3 tag). |
| Navigation Tree - | creates an indented list from the FrontPage navigation structure. |
| Screen Tips - | creates a <span> tag and title attribute for selected text. |
| css Position - | moves an object to the document body and places it inside an absolute positioned <div> tag (Used to change your page from HTML layout to CSS2 layout). |
| Make VBScript - | convert the HTML on a page to VBScript Document.Write statements. |
Two SDK are available: FrontPage 200 SDK and FrontPage 2002 SDK.
Download details FrontPage 2002 Software Development Kit (SDK)
Summary: MSDN Library - Microsoft Office 2000 Developer Object Model Guide. Excerpt: Microsoft Office 2000 Developer Object Model Guide Microsoft FrontPage 2000 Default location: \Program Files\Microsoft Office\Office Source (Type Library) The Microsoft
http:// msdn.microsoft.com / library / officedev / fp2ksdk / frontpageinteractions.htm
Summary: MSDN Library - Office Developer Documentation - Front Page 2000 SDK. Excerpt: FrontPage Interactions Wizards communicate with FrontPage using the FrontPage Visual Basic Object Model described in the FrontPage Visual Basic Help.Summary: MSDN Library - Office Developer Documentation - FrontPage 98. Excerpt: The FrontPage Editor Interface The FrontPage Editor is a WYSIWYG web page editor for HTML documents. Web pages are stored in .htm or .html files on the web server.
The FrontPage To Do List Interface
Summary: MSDN Library - Office Developer Documentation - FrontPage 98. Excerpt: The FrontPage To Do List Interface The FrontPage To Do List is a flexible grid-like object that manages and displays a list of tasks to be performed on the web currently opened by the FrontPage Explor
Web Workshop - FrontPage: Creating Page Backgrounds and Watermarks
Summary: Creating page backgrounds and watermarks in Microsoft FrontPage
Web Workshop - FrontPage: Establish Passwords that Protect
Summary: Use FrontPage to establish passwords that protect
Web Workshop - FrontPage: Designating a Custom Form Confirmation Page
Summary: Designating a custom form confirmation page in FrontPage
Other tutorials
You also can choose how to customize the menus and toolbars. Click the Commands tab of the Customize dialog box to display all the commands available within FrontPage. Drag and drop commands from the Customize dialog box onto the menu or toolbar of your choice. To create a new toolbar, choose Tools/Customize, click the Toolbars tab, and click the New button. Type a name for your new toolbar and then simply drag and drop whatever commands you want on to your new toolbar from the Commands tab.
2. Make a macro button
One of the best new features of FrontPage 2000 is the Visual Basic for Applications
Macro Editor. Macros can be a great help in applying a number of repetitive
tasks. The only problem is that after you create a macro you have to go through
a series of menus just to access it, which defeats the purpose of the shortcut.
Expanding a bit on the previous tip, FrontPage will let you create your own
custom macro button.
Select Tools/Customize and click the Commands tab. Click the Macros category and then drag and drop either the Custom Menu Item command to a menu or the Custom Button command to a toolbar. Right-click the command. On the pop-up menu type in a new name for the command, then select Assign Macro. Choose a macro from the Macro dialog box, and you've got your new macro button. You also can change the icon for the button by right-clicking it and selecting either Change Button Image or Edit Button Image. Change Button Image displays a selection of 48 premade icons from which you can choose. If you don't like any of those, the Edit Button Image function will let you actually create one of your own.
3. Create your own commands
FrontPage 2000 now includes support for the Visual Basic for Applications (VBA)
language, which has become so popular because of its widespread use in Microsoft
Office. With VBA, you can automate tasks, manipulate different aspects of your
Web project within the VBA programming environment, and create your own commands.
As an example, here's how to add a Save All command to FrontPage that
will allow you to save every open page in a project with just one click.
First, start the Visual Basic Editor (VBE) by selecting the Tools/Macro/Visual Basic Editor menu. Insert a new module by selecting the Insert/Module menu. In the Properties window (located on the left side of the VBE window), change the name of the module to SaveAll. Now, in the module code window that opened up when you inserted a new module, type this code:
Sub Save_All() 'Name of the macro
'Checks that the document is in Page view in FrontPage and that at least one page is open.
If (Application.ActiveWebWindow.ViewMode = fpWebViewPage _
And ActiveWebWindow.PageWindows.Count > 0) Then
Dim activePage, page As PageWindow 'Declare variables
'Set the variable ActivePage to the current active page
Set activePage = ActivePageWindow
'Loop through all open pages
For Each page In ActiveWebWindow.PageWindows
page.Activate
'Click the Save button for each page
CommandBars("Add Command").Controls("&File"). _
Controls("&Save").Execute
Next
'Display the page you were working on when the Save All command was clicked
activePage.Activate
End If
End Sub
To add the new command to the File menu in FrontPage, choose Tools/Customize to open the Customize dialog box. Under the Command tab, select Macros from the Categories list. Drag the Custom Menu Item item from the Commands list onto the FrontPage File menu and drop it beneath the Save command. Next, click the Modify Selection button in the Customize dialog box, and a context menu will appear. Type Save All in the Name field and then select Assign Macro. In the Macro dialog box, select the Save_All macro and click OK. Close the Customize dialog box, and your new Save All command appears in the FrontPage File menu.
4. Teach FrontPage to format your code
You can tell FrontPage how you like your code, and it will format it automatically.
By providing a sample file, you can specify if you'd like your code indented,
your tags capitalized, and so on. Open a sample page that contains code formatted
the way you prefer (the more tags in your sample page, the better). Then select
Tools/Page Options and click the HTML Source tab. Click the Base On Current
Page button, and FrontPage will set its formatting rules accordingly. Then select
the Reformat Using The Rules Below option, and every page you load and save
will be formatted like the sample file.
Automating Repetitive Tasks in FrontPage 2003
In FrontPage 2003, FrontPage 2002, and FrontPage 2000, you can search for and replace text in HTML pages in two ways. One way is by using the SearchInfo object, which was added in FrontPage 2003. The other way is to use the IHTMLTxtRange object.
Using the SearchInfo object, you can create customized, conditional searches that look for code in specific instances. For example, you can replace all B elements with I elements inside TD elements only. To do this, you use a query string that adheres to a defined XML schema. You can also specify a query string that finds and replaces text by using a regular expression. The first example below shows how to use the SearchInfo object. For more information, see Extending Find and Replace for Microsoft Office FrontPage 2003.
In versions prior to FrontPage 2003, you can use the IHTMLTxtRange object to perform full text search and replace. The FindInFrontPage function in the second example below uses the findText method of the IHTMLTxtRange object to locate an instance of the specified string in the specified page, and then either inserts the string or replaces the specified string with another string. The function returns a Boolean representing whether the function found the search string and successfully added the text.
The objects used in these examples are as follows:
- SearchInfo
- PageWindowEx
- IHTMLTxtRange
- IHTMLBody
- IHTMLDocument
Finding and Replacing Text Example 1
Sub QueryContents() Dim objSearch As SearchInfo Dim objRange As IHTMLTxtRange Dim objLimits As IHTMLTxtRange Dim strQuery As String Dim blnFoundMatch As Boolean 'Create a query to find a TD element and 'add an align attribute to the tag. strQuery = "<?xml version=""1.0""?>" & _ "<fpquery version=""1.0"">" & _ "<find tag=""b"">" & _ "<rule type=""insideTag"" tag=""td"" />" & _ "</find>" & _ "<replace type=""changeTag"" tag=""i"" />" & _ "</fpquery>" Set objRange = ActiveDocument.body.createTextRange Set objLimits = ActiveDocument.body.createTextRange Set objSearch = Application.CreateSearchInfo objSearch.Action = fpSearchFindTag objSearch.QueryContents = strQuery Do blnFoundMatch = Application.ActiveDocument.Find(objSearch, objLimits, objRange) Loop While blnFoundMatch = True End SubFinding and Replacing Text Example 2
Function FindInFrontPage(ByRef objTargetPage As PageWindow, _ ByVal strFind As String, ByVal blnMatchCase As Boolean, _ ByVal blnFindWholeWord As Boolean, ByVal strText As String, _ ByVal blnInsert As Boolean) As Boolean Dim objBody As IHTMLBodyElement Dim objTextRange As IHTMLTxtRange Dim blnFound As Boolean Dim lngFlags As Long Set objBody = objTargetPage.Document.body Set objTextRange = objBody.createTextRange FindInFrontPage = False If blnMatchCase = True Then lngFlags = lngFlags + 4 If blnFindWholeWord = True Then lngFlags = lngFlags + 2 objTextRange.collapse True Do blnFound = objTextRange.FindText(String:=strFind, _ flags:=lngFlags) If blnFound Then If blnInsert = True Then objTextRange.collapse False End If objTextRange.Text = strText FindInFrontPage = True Exit Function End If Loop While blnFound = True Set objBody = Nothing Set objTextRange = Nothing End Function 'Use the following subroutine to call the preceding function. Sub CallFindInFrontPage() Dim blnFound As String Dim objPageWindow As PageWindow Set objPageWindow = Application.LocatePage( _ DocumentURL:="example.htm", ViewMode:=fpPageViewNormal) blnFound = FindInFrontPage(objTargetPage:=objPageWindow, _ strFind:="text to find", blnMatchCase:=True, _ blnFindWholeWord:=True, strText:="text to add or replace", _ blnInsert:=False) Select Case blnFound Case True MsgBox "Search text found and new text added successfully." Case False MsgBox "Unable to locate search text." End Select End SubInserting an Interactive Button
In FrontPage 2003, you can programmatically add interactive buttons to Web pages using the InsertInteractiveButton method of the FPHTMLDocument object. The following example adds the "Glow Capsule 1" button. When you add an interactive button to a Web page, FrontPage inserts the necessary scripts. When you remove an interactive button from a Web page, FrontPage removes the scripts.
The objects used in this example are as follows:
- FPHTMLDocument
- IHTMLTxtRange
Inserting an Interactive Button Example
Sub InsertNewButton() Dim objSelection As IHTMLTxtRange Set objSelection = ActiveDocument.Selection.createRange ActiveDocument.InsertInteractiveButton objSelection, "fp-btn:Glow Capsule 1", _ "Home", 200, 50, "http://www.microsoft.com", "_blank" End Sub
Copyright © 1996-2007 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008