Accessing Framesets with Microsoft Visual Basic
Frames are an important part of the design of a Web site. Microsoft FrontPage provides support for programming the content of frames. Click one of the following links for more information on a particular topic.
Iterating all frames in a page window
Changing Meta tag content to another character set
Exploring frames
Accessing frames within a frameset in FrontPage is relatively straightforward as long as you keep in mind that each frame accesses its own page and that you access the contents of each page through the Page object model. The two windows involved in displaying the frames are the PageWindowEx
object and FPHTMLWindow2 objects. The equivalent expression for accessing the contents in the window associated with the active frame is Set myActiveFrameInFrameset = ActivePageWindow.Document
. The ActivePageWindow object accesses the page window for the frame, and the Document object accesses the FPHTMLWindow2 object.
Note You may get permission denied errors if you try to access objects while in HTML view. When you want to add code or text to a document object, you must set the FpPageViewMode constant for the ViewMode property to fpPageViewNormal. The value for the view mode cannot be set to fpPageViewHtml or fpPageViewPreview. Alternatively, in the FrontPage user interface (UI), you cannot have the HTML tab or the Preview tab open in Page view.
Role of a frameset
A frameset is the container for all of the frames in a Web window. Each frame is contained in an individual page window and has an individual page associated with it. By accessing the FrameWindow object from the Web object model, you access an FPHTMLWindow2 object that contains the frame page document. From these objects you can access the windows, documents, and frames of the frameset. In the following statement,
myFrameset
is an FPHTMLWindow2 object that returns the frames page document. From this object, you can access the <FRAME> and <FRAMESET> tags, or the window or document objects.
Set myFrameset = ActivePageWindow.FrameWindow
This statement returns an FPHTMLWindow2 object through the Page object model. The Document property of myFrameset
accesses the Page object model for the page that is equivalent to accessing the frames page HTML tab in Page view in FrontPage.
Note The Frames Page HTML and No Frames tabs are only available when frames exist on the current page.
Accessing HTML tags
You can access the same information that a frames tag accesses by declaring an object as an FPHTMLFrameElement. Some of the properties and methods available for this object include border, borderColor, click, frameBorder, frameSpacing, innerHTML, innerText, insertAdjacentHTML, and insertAdjacentText.
Dim myFramesElements() As FPHTMLFrameElement
You can access the information for a frameset tag by declaring an FPHTMLFramesetSite object.
Dim myFramesetSite As FPHTMLFramesetSite
Dynamic frame sources
You can dynamically change the frame source in the HTML code by using the following statements. This code sets the frame source to a new URL, Inventory_1stQuarter.htm.
Dim myDoc As Object
Set myDoc = ActivePageWindow.FrameWindow.Document
myDoc.all.tags("frame").Item(0).src = _
"Inventory_1stQuarter.htm"
Iterating all frames in a page window
To access the properties of the frameset elements that reside in a particular frames page, you must access the FPHTMLDocument object through the Document property. The following example iterates through the frameset and frame elements for the active frameset in Microsoft FrontPage. The frameset array (myFSElements) comprises each <FRAMESET> tag on the frames page. The frames array (myFramesElements) comprises each <FRAME> tag on the frames page. The frame windows array (myFramesWindows) comprises each FPHTMLWindow2 object that points to each frame. You populate each of the arrays by iterating through their respective tags or objects. Once the arrays are populated, you change the frameSpacing property in the frameset element to "10", the borderColor property to "red", and change various other properties in the document.
Private Sub AccessFramesPage()
Dim myFPWindow As FPHTMLWindow2
Dim myFSElements() As IHTMLFrameSetElement
Dim myFramesWindows() As FPHTMLWindow2
Dim myFramesElements() As FPHTMLFrameElement
Dim myStyle As FPHTMLStyle
Dim i As Integer
Set myFPWindow = ActivePageWindow.FrameWindow
ReDim myFSElements(myFPWindow.Document.all.tags("FRAMESET").length)
ReDim myFramesElements(myFPWindow.Document.all.tags("FRAME").length)
ReDim myFramesWindows(myFPWindow.frames.length)
For i = 0 To UBound(myFSElements)
Set myFSElements(i) = _
myFPWindow.Document.all.tags("FRAMESET").Item(i)
Next i
i = 0
For i = 0 To UBound(myFramesWindows)
Set myFramesWindows(i) = myFPWindow.frames(i)
Next i
i = 0
For i = 0 To UBound(myFramesElements)
Set myFramesElements(i) = _
myFPWindow.Document.all.tags("FRAME").Item(i)
Next i
myFSElements(0).frameSpacing = "10"
myFramesElements(0).borderColor = "red"
With myFramesWindows(2).Document
.bgColor = "green"
.body.innerHTML = "<p id=""cool""> Added by FP Programmability"
Set myStyle = .all.cool.style
myStyle.backgroundColor = "white"
myStyle.display = False
myStyle.textDecorationUnderline = True
myStyle.Font = "Tahoma, 24"
myStyle.fontStyle = "italic"
End With
End Sub
Changing Meta tag content to another character set
You can change all of the content-type META tags to a different character set (Central European) as shown in the following code sample. The current character set is shown in the Language Settings dialog box (available for page properties).
Note The entire content-type META tag contains a string similar to the following:
content = "text/html; charset = windows-1252"
The character set is "windows-1252" and is the default character set for U.S. English.
Each time the program iterates through the loop, you access the next frame in the frameWindow object, which is the same as accessing each HTML frames tag in succession. However, the Frames collection does not support the For...each construct. You cannot access HTTP-EQUIV type META tags via their name; you must instead use an index as shown in the following example. The expression beginning with myContentType.Content
sets the character set to Central European.
Note FrontPage places the content type in <META> tag zero(0).
Private Sub ChangeCharSet()
Dim myFrames As IHTMLFramesCollection2
Dim myFrame As FPHTMLWindow2
Dim myHTTPEquiv As String
Dim myContentType As Object
Dim myCount As Integer
Set myFrames = ActivePageWindow.FrameWindow.frames
Set myFrame = ActivePageWindow.FrameWindow.frames(0)
myHTTPEquiv = 0
For myCount = 0 To myFrames.Length - 1
Set myFrame = myFrames(myCount)
Set myContentType = _
myFrame.Document.all.tags("meta").Item(myHTTPEquiv)
myContentType.content = _
"text/html; charset=iso-8859-2"
Next myCount
End Sub