Accessing Framesets with Microsoft Visual Basic

SharePoint Designer Developer Reference

Frames are an important part of the design of a Web site. Microsoft Office SharePoint Designer provides support for programming the content of frames. Click one of the following links for more information on a particular topic.

Exploring frames
Role of a frameset
Accessing HTML tags
Dynamic frame sources
Iterating all frames in a page window
Changing Meta tag content to another character set

Exploring frames

Accessing frames within a frameset in Office SharePoint Designer 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 PageWindow object and DesignerWindow objects. The equivalent expression for accessing the contents in the window associated with the active frame is shown in the following code.

Set objActiveFrame = ActivePageWindow.Document

The ActivePageWindow property accesses the page window for the frame, and the Document property accesses the DesignerDocument object that is assigned to the frame.

You may get "permission denied" errors if you try to access objects while in HTML view. To eliminate these errors when you want to add code or text to a document object, you can set the ViewMode property to PageViewNormal.

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. You can use the FrameWindow property to access the DesignerWindow object that contains the frame page document. From this object you can access the windows, documents, and frames of the frameset. In the following statement, objFrameset is a DesignerWindow object that returns the window that contains the frames page document. From this object, you can access the <FRAME> and <FRAMESET> tags, or the window and document objects.

Set objFrameset = ActivePageWindow.FrameWindow

This statement returns a DesignerWindow object, which is equivalent to accessing the frames page using the HTML tab in the page window.

Accessing HTML tags

You can access the same information that a frames tag accesses by declaring an object as a FrameElement object. Some of the properties and methods available for this object include border, borderColor, click, frameBorder, frameSpacing, innerHTML, innerText, insertAdjacentHTML, and insertAdjacentText.

Dim objFrame As FrameElement

You can access the information for a frameset tag by declaring a FrameSetSiteElement object.

Dim objFrameSet As FrameSetSiteElement

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 objDoc As DesignerDocument
Set objDoc = ActivePageWindow.FrameWindow.Document
objDoc.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 DesignerDocument object through the Document property. The following example iterates through the frameset and frame elements for the active frameset in Office SharePoint Designer. 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 DesignerWindow object that points to each frame. You populate each of the arrays by iterating through their respective tags or objects. When the arrays are populated, you change the frameSpacing property in the frameset element to "10", change the borderColor property to "red", and change various other properties in the document.

Private Sub AccessFramesPage()
    Dim objDocumentWindow As DesignerWindow
    Dim objFrameSetElements() As FrameSetSiteElement
    Dim objFrameWindows() As DesignerWindow
    Dim objFrameElements() As FrameElement
    Dim objStyle As Style
    Dim i As Integer
    
    Set objDocumentWindow = ActivePageWindow.FrameWindow
    ReDim objFrameSetElements(objDocumentWindow.Document.all.tags("FRAMESET").Length - 1)
    ReDim objFrameElements(objDocumentWindow.Document.all.tags("FRAME").Length - 1)
    ReDim objFrameWindows(objDocumentWindow.frames.Length - 1)
    
    For i = 0 To UBound(objFrameSetElements)
        Set objFrameSetElements(i) = _
            objDocumentWindow.Document.all.tags("FRAMESET").Item(i)
    Next i
    
    i = 0
    For i = 0 To UBound(objFrameWindows)
        Set objFrameWindows(i) = objDocumentWindow.frames(i)
    Next i
    
    i = 0
    For i = 0 To UBound(objFrameElements)
        Set objFrameElements(i) = objDocumentWindow _
            .Document.all.tags("FRAME").Item(i)
    Next i
    
    objFrameSetElements(0).frameSpacing = "10"
    objFrameElements(0).borderColor = "red"
    
    With objFrameWindows(2).Document
        .bgColor = "green"
        .body.innerHTML = "<p id=""cool""> Added programmatically.</p>"
        
        Set objStyle = .all.item("cool").Style
        objStyle.backgroundColor = "white"
        objStyle.display = False
        objStyle.textDecorationUnderline = True
        objStyle.Font = "Tahoma, 24"
        objStyle.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 property, which is the same as accessing each HTML frames tag in succession. However, the Frames collection does not support the For...each statement. You cannot access an HTTP-EQUIV type META tags via its name; you must instead use an index as shown in the following example. The expression beginning with objContentType.Content sets the character set to Central European.

Note

By default, Office SharePoint Designer places the content type within the <META> tag zero (0).

Private Sub ChangeCharSet()
    Dim objFrames As IHTMLFramesCollection2
    Dim objFrame As DesignerWindow
    Dim strHTTPEquiv As String
    Dim objContentType As Object
    Dim intCount As Integer
    Set objFrames = ActivePageWindow.FrameWindow.frames
    Set objFrame = ActivePageWindow.FrameWindow.frames(0)
    strHTTPEquiv = 0
    For intCount = 0 To objFrames.Length - 1
        Set objFrame = objFrames(intCount)
        Set objContentType = _
            objFrame.Document.all.tags("meta").Item(strHTTPEquiv)
        objContentType.content = _
            "text/html; charset=iso-8859-2"
    Next intCount
End Sub