在 Microsoft FrontPage 中以编程方式设计动态元素

   

Microsoft Internet Explorer 中的对象、方法和属性通常是设计用于实时运行。在 Microsoft FrontPage 中,大部分元素仅设计用于设计阶段。您可以以编程方式将内容添加到现有文档中、访问网页上所选的内容、创建脚本元素或修改现有表格。单击下列链接之一可直接移到那个主题。

使用 TextRange 将内容添加到文档中

使用 TextRange 访问用户所选内容

创建脚本元素

访问表格

使用 TextRange 将内容添加到文档中

您可以通过创建 TextRange 对象并向 Web 页添加新内容,以编程方式将 HTML 内容添加到 Web 页中,如下例所示。TextRange 对象是从 myDocument body 对象创建的。如果 myClearPage 中的值为真,那么 <Body></Body> 标记之间的所有内容都会替换为新的 HTML 内容,否则新内容会追加到原始内容之后。

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

使用 TextRange 访问用户所选内容

您可以使用 IHTMLTxtRange 对象来选择 HTML 对象,或者控制用户在指定文档中的选择。下列示例将背景色应用于当前所选内容。

Private Sub ApplyStyleToSelection()
    Dim myRange As IHTMLTxtRange

    Set myRange = ActiveDocument.selection.createRange

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

向网页添加脚本

在 FrontPage 2000 的 Visual Basic 中编写脚本很容易。只要将脚本加载到一个字符串变量中并将该字符串追加到网页的 <Head> 标记之后即可。(使用 IHTMLElement 对象可以访问 <Head> 标记。)字符串添加到网页之后,就成为有效的脚本元素,并且可以通过 FPHTMLScriptElement 对象对其进行访问和修改。下列代码将脚本元素添加到当前网页,验证脚本已经添加,添加带有“确定”和“取消”按钮的用户查询,然后在 Visual Basic 编辑器中的“立即”窗口中打印一些脚本元素属性。

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

访问表格

任何创建过表格并以 HTML 方式使用过其内容的用户将会发现使用 Visual Basic 来访问表格很容易。下列程序访问当前网页上的表格,并插入一个单元格。

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