VBA in FrontPage

Introduction

This article gives you an introduction to VBA in FrontPage.

My First VBA Macro

My first task that I wanted to be able to do was to change the title at the top of my menu on every page. Of course we have our critical files backed up before running experimental code on them.  Still the best procedure is to write code which operates on one file, and fully debug it before using it on the full set.

Here is the subroutine I wrote to make the change to the active file.

    Sub ChangeMenuTitle()
    Dim fpDoc As FPHTMLDocument
    Set fpDoc = ActiveDocument
    Dim myTable As FPHTMLTable
    Set myTable = fpDoc.all.tags("table")(0)
    myTable.Id = "menu"
    Dim strRow As String
    Dim myRow As FPHTMLTableRow
    Set myRow = myTable.rows(0)
    Dim myCell As FPHTMLTableCell
    Set myCell = myRow.cells(0)
    strRow = myCell.innerText
    If strRow = "ATL & COM Notebook" Then
        myCell.all.tags("i")(0).innerText = "Components Notebook"
        strRow = myCell.innerText
    End If
End Sub

  

This is working code, not optimized and not very reusable, but useful for learning purposes, and getting the job done.  First I obtain the ActiveDocument object, then I obtain the first table.  I know that the table I want to work with is the first on the page so I use fpDoc.all.tags("table")(0) to obtain the first table on my page.  Since I want to write smarter code in the future, I set the Id for this table to "menu".  Then I go through a two step process, to obtain the first row in the table, then the first cell in the row.  Once I have this cell I check to see that the contents are "ATL & COM Notebook", to make sure I am changing the right text.  Now I change the text inside the "<I> tags to "Components Notebook".

Now that we have code which successfully makes the change to one page, lets write the code to apply this change to all the pages in the site.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous