Programming Dynamic Elements

SharePoint Designer Developer Reference

The objects, methods, and properties in Microsoft Internet Explorer are usually designed for run-time use. In Microsoft Office SharePoint Designer, objects, methods, and properties are designed for design-time use only. You can programmatically add content to an existing page, access selections on a page, create a scripting element, or modify a table.

Adding content to a document using a TextRange object
Accessing user selections using a TextRange object
Adding script to a page
Accessing tables

Adding content to a document using a TextRange object

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 Document variable. If the value in the ClearPage parameter is True, 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(Document As Object, _
        HTMLText As String, ClearPage As Boolean) As Boolean
    Dim objRange As TextRange
    Dim objBody As BodyElement
    On Error GoTo CannotAddHTML
    'Create a TextRange object
    If ClearPage Then
        Set objRange = _
            Document.all.tags("BODY").Item(0).createTextRange
        'Clear the current document
        Call objRange.pasteHTML("")
        objRange.collapse False
        Set objRange = Nothing
    End If
    Set objBody = Document.all.tags("BODY").Item(0)
    objBody.innerHTML = objBody.innerHTML & HTMLText & vbCrLf
    AddHTMLToPage = True
    Exit Function
CannotAddHTML:
    AddHTMLToPage = False
End Function
Sub AddNewHTML()
    Dim strHTMLString As String
    Dim obBodyElement As BodyElement
    strHTMLString = "<strong> <em> New Sale on Vintage Wines! </em> </strong>" & vbCr
    If AddHTMLToPage(ActivePageWindow.Document, strHTMLString, True) Then
        Set obBodyElement = _
          ActivePageWindow.Document.all.tags("BODY").Item(0)
    End If
End Sub

Accessing user selections using a TextRange object

You can use the TextRange 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 objRange As TextRange
    Set objRange = ActiveDocument.selection.createRange
    objRange.parentElement.style.backgroundColor = "SkyBlue"
End Sub

Adding script to a page

Scripting in Office SharePoint Designer 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 by using an IHTMLElement interface object.) After the String is added to the page, it is a valid scripting element and can be accessed through the ScriptElement 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 objScriptElement As ScriptElement
    Dim objHead As IHTMLElement
    Dim objBody As IHTMLElement
    Dim strBody As String
    Dim strHTMLString As String
    Dim strText As String
    'Build a script tag construct.
    strHTMLString = strHTMLString & "<script language=""VBScript"">" _
      & vbCrLf
    strHTMLString = strHTMLString & "Function doOK" & vbCrLf
    strHTMLString = strHTMLString & _
      "msgbox ""Please wait; an order form is being generated...""" & _
      vbCrLf
    strHTMLString = strHTMLString & "End Function" & vbCrLf & vbCrLf
    strHTMLString = strHTMLString & "Function doCancel" & vbCrLf
    strHTMLString = strHTMLString & _
      "msgbox ""Exiting ordering process.""" & vbCrLf
    strHTMLString = strHTMLString & "End Function" & vbCrLf
    strHTMLString = strHTMLString & "</script>" & vbCrLf
    'Build a call tag construct.
    strBody = "<CENTER>" & vbCrLf
    strBody = strBody & _
      "<BUTTON onclick=""doOK()"">OK</BUTTON>" & vbTab
    strBody = strBody & _
      "<BUTTON onclick=""doCancel()"">Cancel</BUTTON>" & vbCrLf
    strBody = strBody & "</CENTER>"
    'Add text to the document
    strText = "I'd like to order some vintage wines."
    'Access the HEAD element.
     Set objHead = ActivePageWindow.Document.all.tags("HEAD").Item(0)
    'Append the script element to the HEAD element (objHead).
    objHead.innerHTML = objHead.innerHTML & strHTMLString
    'Verify that the script element was added.
    If ActivePageWindow.Document.scripts.length = 1 Then
        'Access the script element just added.
        Set objScriptElement = 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 objScriptElement.htmlFor
        'Retrieve the content of the script.
        Debug.Print objScriptElement.outerHTML
        'Check scripting language.
        Debug.Print objScriptElement.language
    End If
    'Add a query to the user and call the script element.
    ActiveDocument.body.insertAdjacentHTML "BeforeEnd", _
      "<strong><em>" & strText & "</em></strong><P>" & strBody
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 objTable As TableElement
    Dim objRow As TableRowElement
    Dim objCell As TableCellElement
    'Get the table.
    Set objTable = ActiveDocument.all.tags("TABLE").Item(0)
    'Get the first row.
    Set objRow = objTable.rows(0)
    MsgBox objRow.cells.Length
    'Get the first cell.
    Set objCell = objRow.cells(0)
    MsgBox objCell.Width
    'Add a new cell to the first row.
    Set objCell = objTable.rows(0).insertCell(objRow.cells.Length)
End Sub