Programming Dynamic Elements in Microsoft FrontPage

Microsoft FrontPage Visual Basic

Programming Dynamic Elements in Microsoft FrontPage

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.

Adding content to a document using a TextRange

Accessing user selections using a TextRange

Adding script to a page

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 and adding the new content to the page as shown in the following example. The text range is created from the BODY element of the myDocument variable. If the value in the myClearPage parameter is True, then the entire content between the opening and closing BODY element 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 IHTMLTxtRange
    Dim myBodyText As FPHTMLBody

    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 FPHTMLBody

    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 Microsoft FrontPage Visual Basic for Applications is easy. Just load the script into a String variable and insert the String to the HEAD element for the page. (The HEAD element 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 in the Immediate window of 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 element.
     Set myHTag = ActivePageWindow.Document.all.tags("HEAD").Item(0)

    'Append the script element to the HEAD element (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 Microsoft Visual Basic to access tables. The following program accesses a table on the current page and inserts a cell.

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