Programming Dynamic Elements

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The objects, methods, and properties in Microsoft Internet Explorer are usually designed for run-time use. In Microsoft FrontPage, most of the elements are designed for design-time only. You can programmatically add content to an existing document, access selections on a page, create a scripting element, or modify an existing table. Click one of the following links to move directly to that subject.

Adding Content to a Document Using a TextRange

Accessing User Selections Using a TextRange

Creating a Scripting Element

Accessing Tables

Adding Content to a Document Using a TextRange

You can programmatically add HTML content to a web page by creating a text range object and adding the new content to the page as shown in the following example. The text range object is created from the myDocument body object. If the value in myClearPage is true, then the entire content between the <Body></Body> tags is replaced with the new HTML content, otherwise the new content is appended to the original content.

Public Function AddHTMLToPage(myDocument As Object, _
    myHTMLText As String, myClearPage As Boolean) As Boolean
Dim myRange As Object
Dim myBodyText As Object

On Error GoTo CannotAddHTML

'Create a TextRange object
If myClearPage Then
    Set myRange = _
        myDocument.all.tags("BODY").Item(0).createTextRange
    'Clear the current document
        Call myRange.pasteHTML("")
    myRange.collapse False
    Set myRange = Nothing
End If

Set myBodyText = myDocument.all.tags("BODY").Item(0)
myBodyText.innerHTML = myBodyText.innerHTML & myHTMLText & vbCrLf

AddHTMLToPage = True
Exit Function

CannotAddHTML:
AddHTMLToPage = False
End Function

Sub AddNewHTML()
Dim myHTMLString as String
Dim myBodyElement As Object

myHTMLString = "<B> <I> New Sale on Vintage Wines! </I> </B>" & vbCr

If AddHTMLToPage(ActivePageWindow.Document, myHTMLString, True) Then
    Set myBodyElement = _
      ActivePageWindow.Document.all.tags("BODY").Item(0)
End If
End Sub

Accessing User Selections Using a TextRange

You can use the IHTMLTxtRange object to select HTML objects or manipulate a user selection on a specified document. The following example applies a background color to the current selection.

Private Sub ApplyStyleToSelection()
Dim myRange As IHTMLTxtRange

Set myRange = ActiveDocument.selection.createRange

myRange.parentElement.style.backgroundColor = "SkyBlue"
End Sub

Adding Script to a Page

Scripting in Visual Basic for FrontPage 2000 is easy. Just load the script into a string variable and append the string to the <Head> tag for the page. (The <Head> tag is accessed using an IHTMLElement object). Once the string has been added to the page, it is a valid scripting element and can be accessed through the FPHTMLScriptElement object and modified. The following code adds a script element to the current page, verifies that the script was added, adds a query to the user with OK and Cancel buttons, and then prints some of the script element properties to the Immediate window in the Visual Basic Editor.

Private Sub CreateAScript()
Dim myScriptElement As FPHTMLScriptElement
Dim myHTag As IHTMLElement
Dim myBodyTag As IHTMLElement
Dim myBodyString As String
Dim myHTMLString As String
Dim myText As String

'Build a script tag construct.
myHTMLString = myHTMLString & "<script language=""VBScript"">" _
  & vbCrLf
myHTMLString = myHTMLString & "Function doOK" & vbCrLf
myHTMLString = myHTMLString & _
  "msgbox ""Please wait, an order form is being generated...""" & _
  vbCrLf
myHTMLString = myHTMLString & "End Function" & vbCrLf & vbCrLf
myHTMLString = myHTMLString & "Function doCancel" & vbCrLf
myHTMLString = myHTMLString & _
  "msgbox ""Exiting ordering process.""" & vbCrLf
myHTMLString = myHTMLString & "End Function" & vbCrLf
myHTMLString = myHTMLString & "</script>" & vbCrLf

'Build a call tag construct.
myBodyString = "<CENTER>" & vbCrLf
myBodyString = myBodyString & _
  "<BUTTON onclick=""doOK()"">OK</BUTTON>" & vbTab
myBodyString = myBodyString & _
  "<BUTTON onclick=""doCancel()"">Cancel</BUTTON>" & vbCrLf
myBodyString = myBodyString & "</CENTER>"

'Add text to the document
myText = "I'd like to order some vintage wines."

'Access the <HEAD> tag.
Set myHTag = ActivePageWindow.Document.all.tags("HEAD").Item(0)

'Append the script element to the <Head> tag(myHTag).
myHTag.innerHTML = myHTag.innerHTML & myHTMLString

'Verify that the script element was added.
If ActivePageWindow.Document.scripts.length = 1 Then
    'Access the script element just added.
    Set myScriptElement = ActivePageWindow.Document.scripts(0)

    'Print script element properties to the Immediate window.
    'JScript only: the next statement gets the FOR= attribute from
    'the JScript, otherwise an empty string prints in the Immediate
    'window.
    Debug.Print myScriptElement.htmlFor

    'Retrieve the content of the script.
    Debug.Print myScriptElement.outerHTML

    'Check scripting language.
    Debug.Print myScriptElement.language
End If

'Add a query to the user and call the script element.
ActiveDocument.body.insertAdjacentHTML "BeforeEnd", _
  "<B><I>" & myText & "</B></I><P>" & myBodyString
End Sub

Accessing Tables

Anyone who has created tables and worked with their contents in HTML will find it easy to use Visual Basic to access tables. The following program accesses a table on the current page and inserts a cell into the table.

Sub AccessTables()
Dim myTable As FPHTMLTable
Dim myRow As FPHTMLTableRow
Dim myCell As FPHTMLTableCell

'Get the table.
Set myTable = ActiveDocument.all.tags("TABLE").Item(0)

'Get the first row.
Set myRow = myTable.rows(0)
MsgBox myRow.cells.Length

'Get the first cell.
Set myCell = myRow.cells(0)
MsgBox myCell.Width

'Add a new cell to the first row.
Set myCell = myTable.rows(0).insertCell(myRow.cells.Length)
End Sub