MAIN ARTICLE
EXTENDING FRONTPAGE WITH MACROS PART II
By Alex Tushinsky
http://LTMOD.com
In part one of this article, we looked at the basics of macro use within
FrontPage. Macros are very flexible, allowing the user to easily perform
many monotonous tasks. We looked at the main source for information on
creating the FrontPage macros - The Microsoft FrontPage SDK. The SDK
provides detailed programming help for those who want to venture into the
macro world. In addition to that, we looked at objects, properties and
methods, and examined the FrontPage VB Editor.
Today, we'll be developing a macro that adds HTML <META> tags to the
header of any or all web pages within our site. Specifically, we'll
concentrate on the KEYWORD and DESCRIPTION meta tags which are used by
search engines to better catalog your web site (and possibly increase your
ranking within a search engine). As such, this new macro will require us to
look at the file structure of a website within FrontPage, a look at Visual
Basic forms for user input, and finally the FrontPage editing capabilities
that we can control through code. If this sounds complex and scary, relax!
The SDK provides us with everything we need to get started.
Ok, so the first step in creating a more complex macro is to analyze what
you want the macro to do. In our case we want a user to select any or all
files within a folder within their web site (in FrontPage) and apply two
meta tags to the headers of each page selected.
The meta tags will look like this:
<META NAME="DESCRIPTION" CONTENT="This is the description for this web
site….">
<META NAME="KEYWORDS" CONTENT="keyword1, keyword2, keyword3, keyword4…..">
Since the description and keywords vary site to site, we'll need an
interface for the user to enter the correct information. This will involve a
VB form.
Once the keywords and description have been entered, the macro should
open each file selected in the FrontPage Folder List and add the above META
tags to the header of each page, then save and close each file.
The place to start here is going to be with the form. So fire up
FrontPage and go into the Visual Basic Editor (Tools, Macro, Visual Basic
Editor or press ALT-F11). From Part I, you may recall that the VB Editor is
divided into several sections, the main Code Window, the Properties panel
and the Projects Panel. Our first step is to add a "UserForm" to the
Projects Panel. Click on Insert, and then select UserForm from the menu. A
blank window will appear on the screen. You'll also see that the Properties
Panel has picked up in content. What you're looking at is one of those
objects we talked about in Part 1. This one in particular is just a blank
Window for us to work with. We can modify some of its properties, and
populate it with other objects such as text boxes and buttons. On screen you
should also see a Toolbox, which contains some of the most popular objects
we can use on our new form.
To customize the form itself, we'll need to work with the Properties
panel. Properties are different attributes of our object that we can change
or customize. Things like captions, size, and color are all here. Use the
Property Panel to find the properties below and change their values.
CAPTION = Keywords / Description Settings
HEIGHT = 265
WIDTH = 305
That should do it for the form. What you've done is change the size of
the form, and the Caption label (the blue bar at the top of the form).
Now click on the form itself to select it. In the toolbox, select the
object that looks like a capital letter "A" by clicking on it. Move the
mouse over the form, and drag out a rectangle. What you now have is a label
with the caption "Label1". Select Label1 and change its Caption property
(NOT NAME) to the word "Keywords:". Visually size the label to fit the new
caption, and position it in the upper left hand corner of the form itself
(just drag it!). Avoid double-clicking on the objects. Once double-clicked,
you'll end up in the code window. For now we want to work visually, and not
with code.
Create one more label, with the caption of "Description" and position it
below the "Keywords" label, but about half way down the form.
Here are the settings for both labels that I used when building this
tutorial.
Label1
CAPTION = Keywords:
HEIGHT = 12
WIDTH = 42
LEFT = 6
TOP = 6
Label2
CAPTION = Description:
HEIGHT = 12
WIDTH = 48
LEFT = 6
TOP = 126
In case you're wondering, LEFT and TOP refers to the properties that
control the position of the object. They identify the upper left-hand corner
in a measurement called "twips". Although I've been programming for years, I
never really figured out the purpose of "twips", and the only explanation I
ever got about the measurement is that 1440 twips is equal to 1 printed inch
(which of course is not the same as looking at an inch of space on the
screen).
Now that we got the two labels setup on the screen, we need to add the 2
text boxes and buttons to the form. The text box is identified as "ab" in
the toolbox. Go ahead and select it and draw out the first text box below
the keyword label. This textbox will probably hold a lot of information, so
I stretched it to end just above the "Description" label. Here are the
Property settings that I used:
HEIGHT = 102
WIDTH = 288
MULTILINE = TRUE
TOP = 36
LEFT = 6
Regardless of the height and width you set up for your Textbox, be sure
to set the MULTILINE Property to true, otherwise the text box will not
accept more than 1 line of text.
Now, add the second textbox below the description label. Leave yourself
some room at the bottom of the form for buttons. Here are my settings for
the Description textbox:
HEIGHT = 72
WIDTH = 288
MULTILINE = TRUE
TOP = 138
LEFT = 6
Things should be taking shape right about now. You have a form and you
have a way for users to add their keywords and descriptions. Now all we need
is buttons!
Select the CommandButton object from the Toolbox (it's the one that looks
like a button), and drop it in bottom right corner of the form.
Here are my settings for both buttons:
CommandButton1
HEIGHT = 18
WIDTH = 60
LEFT = 234
TOP = 216
CAPTION = Cancel
CommandButton2
HEIGHT = 18
WIDTH = 60
LEFT = 168
TOP = 216
CAPTION = OK
Alright, with that in place, our interface is complete. If you run the
form the way it is now, clicking the buttons won't produce any results, and
the keywords / description you enter into the prompts won't have any effects
on your web's pages. We need to add the code that will do the job for us.
Unlike our first macro, the code for this one will be closely tied into the
form we just built. In other words, the code will be triggered by the OK
button.
I always like to start with the simple tasks when I code, and in this
case, programming the Cancel button is easier than programming the OK
button. Double-click on the Cancel button. The code window appears and the
cursor is placed inside the OnClick procedure for the Cancel button
(CommandButton1). Type in the word END and hit enter.
The final version of the code should look like this:
Private Sub CommandButton1_Click()
END
End Sub
Press F5 to run your form. Click the Cancel button. Your form should
disappear and return back to the code window.
Now, we have to write the meat and potatoes of our macro. So it's off to
the SDK to find code that might give us some help. In the SDK, I typed in
"meta" as my search, which returned 2 topics for me to look at. The second
entry "Opening and Editing Files" seems to be what we're looking for! A
further search on "SelectedFiles" brought up an entry called "Accessing
Selected Files and Selected Folders". Now, that we have what we need, it's
just a matter of combining the stock examples Microsoft provided into a
final macro.
The way our macro should work is as follows - User selects one or more
files from the Folder List. The macro runs, and the user types in the
keywords and description they want to use. They click OK, which triggers the
main part of our macro. For each file in the Folder List, a page should be
opened, and the Meta tags inserted. The page should then be saved and
closed.
After cutting and pasting, and several trial runs, I've come up with the
following code, all based on the examples mentioned above.
Private Sub CommandButton2_Click()
Dim objFile() As WebFile
Dim intCount As Integer
Dim strSelected As String
Dim objPage As FPHTMLDocument
Dim objPageWindow As PageWindowEx
Dim objHead As IHTMLElement
Dim strKeywords As String
Dim strDesc As String
strKeywords = "<meta name=""keywords""
content=""" & TextBox1 & """>"
strDesc = "<meta name=""description""
content=""" & TextBox2 & """>"
objFile = ActiveWebWindow.SelectedFiles
If UBound(objFile) >= 0 Then
For intCount = 0 To UBound(objFile)
Set objPageWindow =
objFile(intCount).Edit(fpPageViewNormal)
Set objPage =
objPageWindow.Document
'Add meta tag to each of
the pages.
Set objHead =
objPage.all.tags("head").Item(0)
Call
objHead.insertAdjacentHTML("BeforeEnd",
strDesc & vbCrLf & strKeywords)
'Now save and
close the pagewindow
objPageWindow.SaveAs
objPageWindow.Document.Url
objPageWindow.Close False
Next intCount
End If
End
End Sub
The above code should be inserted into the procedure called Private Sub
CommandButton2_Click(), which you can access by double-clicking on the OK
button from the UserForm.
Let's examine the above code a bit closer.
The first part - the Dim statements create the necessary objects and
variables for us to use. They are pretty much a copy and paste from their
respective examples. objFile() is the collection of files the user selected.
The brackets on the end mean that the object is a collection which can hold
multiple values (our files).
After the Dim statements, we combine the html Meta tag code with the
contents of the textboxes in our form. When this code runs, Textbox1 and
Textbox2 will be replaces with whatever we typed into the fields in our
form, in essence building the Meta tags for us.
The rest of the code looks through the objFile collection and opens one
of our pre-selected files. As each file is opened, you can see it in the
FrontPage editor. This is handled by objPageWindow (which controls the
editor window). Once the page is opened, objHead which contains all of the
information about the HTML header appends our new Meta tags before the
</head> tag. Finally, the page is saved and closed, and the whole thing
repeats again, until the count of selected files in objFile is exhausted.
Once that's done, the macro closes itself (end statement).
Lastly, if you want to add a custom button to FrontPage for working with
the macro, you'll need a way to call the form.
Add the following procedure to Module1.bas.
Sub MetaTags()
UserForm1.Show
End Sub
Link the above macro name to the button (see Part 1 on how to do this).
Also, be sure to remember that the macro won't do anything unless you
actually select files in the Folder List first!
While this macro isn't the easiest to figure out, if you look at the
code, and study the referenced examples, it should make some sense to you.
With some basic knowledge of Visual Basic, some great ideas, and a bit of
patience and perseverance, you can build your own custom add-ins and your
own custom code for making your web editing chores easier!
As always, I welcome any questions or feedback about this tutorial. You
can email me at alext@ltmod.com. Happy
Programming!!
ABOUT THE AUTHOR
******************************
Alex Tushinsky is the chief operating officer of
LTMOD.COM and developer of PageTools, and
Media Manager add-ons for Microsoft®
FrontPage® 2002 and Microsoft® FrontPage®
2000. Alex's extensive programming and web
site design experience includes work with
international, national, and local corporations,
small businesses and non-profit organizations.
His teaching resume includes courses for
groups of 2-200 and instruction in the areas of
Web Design & Development, Programming,
Graphics Development, and other web related
technologies.
http://ltmod.com
******************************
EXTRA for this issue:
An internet based article on Internet Security. Please see:
[ out of date link - removed ] by Mike " It is a good day if I learned something new" Mike Baynes is editor of MikesWhatsNews (see a sample on his web page, http://www.mwn.ca/ ) as well as a Technical Support Alliance Member.
~~~~~~~~++++SPONSOR AD++++~~~~~~~
FrontLook Component Effects = Series 3 on Steroids! DHTML technology with a
new, powerful set of Component Editors make it word-processor-easy to set up
your Scrollers or Slideshows! Easy Interactive Features - The LATEST!
http://anyfrontpage.com/rd/fpw.htm
~~~~~~~++++SPONSOR AD++++~~~~~~~~
↑ Top
|