Modifying Pages Programmatically

SharePoint Designer Developer Reference

This topic illustrates how to use the DesignerDocument object to work with elements within the page, either to change the contents of a page or simply to access the contents of a page.

Microsoft Visual Basic for Applications is a powerful tool that you can use to modify content in your Web pages. In the Object Browser, you can find many of the same types of components with which you may be familiar in Microsoft Internet Explorer.

Note

To view the programming objects that you can use to access HTML elements within an HTML page, select "SharePointDesignerPage" in the object library list in the Object Browser.

You can use the ActiveDocument property of the Application object or the Document property of the PageWindow object to access the page in the page window and the HTML elements within the page. For example, the following statement changes the background color for the page in the active page window.

ActivePageWindow.Document.bgColor = "DarkBlue"

The following example checks for a specific hyperlink (index.htm) within the active document. If the hyperlink is found, the procedure exits; if the hyperlink is not found, the procedure first determines whether the active document is index.htm and, if not, the procedure adds the hyperlink at the end of the document.

Private Sub VerifyIndexLink()
    Dim objDocument As DesignerDocument
    Dim objLinks As Variant
    Dim objLink As Variant
    Dim objNumberOfLinks As Integer
    Dim objAddLink As Boolean
    Dim objLinkName As String
    Dim objLinkName2 As String
    Set objDocument = ActivePageWindow.Document
    Set objLinks = objDocument.Links
    objNumberOfLinks = objLinks.length
    objLinkName = "index.htm"
    objLinkName2 = """" & objLinkName & """"
    For Each objLink In objLinks
        If objLink = objLinkName Then
            objAddLink = True
            Exit For
        End If
    Next
    If objAddLink = False And objDocument.nameProp <> "index" Then
        Call objDocument.body.insertAdjacentHTML("BeforeEnd", "<a href=" _
            & objLinkName2 & ">" & objLinkName & "</a>")
        ActivePageWindow.Save
    End If
    End Sub

Note

Note that in the last If statement the active page window is saved before exiting the procedure. This is a useful statement to add to the OnPageClose event.