本主题是针对那些熟悉 Microsoft FrontPage 但不熟悉 Microsoft Visual Basic for Application 的用户而设计的,让这些用户能对基于 FrontPage 的程序设计环境的一些基本概念背景有所了解。FrontPage Visual Basic for Applications 中的程序设计向您提供了 Microsoft Office 程序设计环境中的 HTML 工具,您可以在其中创建执行一个或一系列任务的过程。例如,您可以:
单击以下列表中的超链接,以直接转至该主题。
在 FrontPage 中创建来自 Access 数据库的表
Visual Basic 过程为开发人员提供了组织代码供模块使用的途径。不必在每个程序中重复编写相同的计算函数,您可以将代码段 (计算函数代码) 编译到一个一般的能被其他程序访问的程序中。在 Visual Basic 中,代码段是包含在起始语句 Sub 和终止语句 End Sub 中的一段过程。
以下代码示例显示了 Visual Basic 中过程的基本语法。
[Private|Public|Static] Sub procedurename(arguments)
statements
End Sub
要运行任何一段完整代码示例(代码块),请按以下步骤进行:
关闭 Visual Basic 编辑器时,您的代码将自动保存。
FrontPage Visual Basic 提供两种类型的过程,Sub 和 Function 过程。Sub 过程执行响应事件过程的代码块。例如,鼠标单击或按键,它们只执行任务而不返回任何值。
注意 Sub 过程可以是事件过程,也可以只执行任务而不响应事件。
以下过程将从当前站点获得其 FrontPage 版本号,但不把版本号返回给人任何其他过程。
Sub DisplayVersion()
Dim myWeb As WebEx
Dim myVersion As String
myVersion = "FrontPage version number: " & ActiveWeb.Application.Version
End Sub
Function 过程也执行任务,但它可以另外返回一个值,或用参数返回多个值。以下的代码示例会将 FrontPage 的版本号返回给一个调用过程。
Function ReturnVersion(vaAppVersion As Variant)
Dim varAppVersion As Variant
varAppVersion = Application.Version
ReturnVersion = varAppVersion
End Function
现在变量 ReturnVersion
包含了 FrontPage 的版本号。要在调用过程中访问该值,您可以按以下示例编写代码。
Sub GetAppVersion()
Dim myAppVersion As Variant
MsgBox "This version of FrontPage is version " _
& ReturnVersion(myAppVersion)
End Sub
另外,您也可以将表达式 ReturnVersion(varAppVersion)
指定给一个变量,并将此变量添加到消息框语句而不是函数调用。
Sub 与 Function 过程都可以被调用以执行它们的任务,这取决于过程是声明为Public 还是 Private。
注意 要在多个模块中使用上一个函数,请将其代码放在“通用”过程的“声明”节中。
用来描述 Visual Basic for Applications 中的代码的第三个术语是宏。作为不带参数的公用 Sub 过程,宏可以调用也可以不调用其他的 Sub或 Function 过程,宏可以被分配到命令栏和快捷键,也可以从“宏”对话框中运行。
Visual Basic 提供两种方式来访问过程。默认情况下,过程是 Public 的,它们可以被您的应用程序中任何模块内的任何其他过程调用。例如,假如您编写了一个在 Web 页上按文件名称列出图像的过程,您将该过程声明为 Public 的,这样就可以在整个 Web 站点使用它。不过,假如您编写了一个编辑特定数据库的过程,并希望该过程只能被管理数据库编辑任务的模块所使用。在这种情况下,您可以将该过程声明为 Private 的。声明为 Private 的过程只能被相同模块内的其他过程引用。在下列代码示例中,上面所示的函数已被声明为 Public 函数,从而可以跨模块和工程进行调用。
Public Function ReturnVersion(varCurrentAppVersion As Variant)
statements
End Function
相反,用来编辑数据库的过程应该声明为 private。
Private Function EditCustomerName(strFirstName As String)
statements
End Function
如何用编程方式运行过程?您可像使用关键字(如 Open)一样声明过程。以下过程将调用 ReturnVersion
函数并包含局部变量 (MyVersion
),该变量返回传给该过程的值。
Function TestCall(AppVersion As Variant)
Dim MyVersion As Variant
ReturnVersion(MyVersion)
End Sub
若您没有任何信息要从一个过程传至另一个过程,您可以只声明过程名称,如以下代码示例所示。
Sub TestCall2()
DisplayCompanySplashScreen
End Sub
此 TestCall2
过程调用另一个过程 (DisplayCompanySplashScreen
),这个被调用的过程没有任何参数或返回值。
若您在将值传递给调用过程时,想要一个事件(例如单击命令按钮)触发代码的执行,您可以从函数执行结果而不需传递变量。将以下加粗显示的代码添加到 ReturnVersion
函数中,可显示当前应用程序的版本号。
Function ReturnVersion(varCurrentAppVersion As Variant)
Dim varAppVersion As Variant
varAppVersion = Application.System.Version
varCurrentAppVersion = varAppVersion
DisplayMsgBox (varCurrentAppVersion)
End Function
以下 DisplayMsgBox
代码示例中的函数显示了变量varCurrentAppVersion
的值,而该变量是从 ReturnVersion
函数传递过来的。
Function DisplayMsgBox(varGotAppVersion As Variant)
Dim varDisplayAppVersion As Variant
VarDisplayAppVersion = varGotAppVersion
MsgBox "This application is version " _
& varDisplayAppVersion
End Function
事件过程现在可以开始显示从 ReturnVersion
函数传过来的值。
Private Sub CommandButton1_Click()
Dim varThisAppVersion As Variant
ReturnVersion (varThisAppVersion)
End Sub
以下过程组合了来自 Page 对象模型和 Web 对象模型中的对象,来获取打开的 Microsoft Access 数据库中的数据,并将数据插入基于 FrontPage 的 Web 页上的表格中。ParseDBTable 过程为 ParseAccessTable 函数提供参数,该函数调用以下各函数来创建并填充表格:
注意 本示例中使用的数据库是 Access 数据库Northwind.mdb。要运行本示例,您必须在 Visual Basic 编辑器中设置对 Microsoft DAO 3.6 Object Library(对象库)和 Microsoft Access Object Library(对象库)的引用。运行本示例之前您还必须打开一个 Access 数据库,而且必须在当前站点中添加名为 tmp.htm 的临时空文件。如果您使用的数据库不是 Northwind.mdb,您必须在 ParseDBTable 过程中指定数据库名称和表。
Function AddDBTableToPage(myPage As PageWindowEx, _
myTableName As String, myFields As Integer)
Dim myTable As FPHTMLTable
Dim myHTMLString As String
Dim myCount As Integer
myHTMLString = "<table border=""2"" id=""myRecordSet_" & _
myTableName & """>" & vbCrLf
myHTMLString = myHTMLString & "<tr>" & vbCrLf
For myCount = 1 To myFields
myHTMLString = myHTMLString & "<td id=""myDBField_" & _
myCount & """> </td>" & vbCrLf
Next myCount
myHTMLString = myHTMLString & "</tr>" & vbCrLf
myHTMLString = myHTMLString & "</table>" & vbCrLf
Call myPage.Document.body.insertAdjacentHTML("BeforeEnd", _
myHTMLString)
End Function
Function AddDBRow(myDBTable As FPHTMLTable)
Dim myHTMLString As String
Dim myTableRow As FPHTMLTableRow
Set myTableRow = myDBTable.rows(0)
myHTMLString = myTableRow.outerHTML
Call myDBTable.insertAdjacentHTML("BeforeEnd", myHTMLString)
End Function
Function AddMemo(myCurrentPage As PageWindowEx, myDBMemo As String, _
myBkMarkField As String, myIndex) As String
Dim myHTMLString As String
Dim myMemoBkMark As String
Dim myBookMark As FPHTMLAnchorElement
myMemoBkMark = myBkMarkField & "_" & myIndex
myHTMLString = "<a name=""" & myMemoBkMark & """> Memo #" & _
myIndex & "</a>" & vbCrLf
'Add the bookmark to the page.
Call myCurrentPage.Document.body.insertAdjacentHTML("BeforeEnd", _
myHTMLString)
Set myBookMark = myCurrentPage.Document.all(myMemoBkMark)
'Add the memo text to the page.
Call myCurrentPage.Document.body.insertAdjacentHTML("BeforeEnd", _
myDBMemo)
AddMemo = "<a href=""#" & myBookMark.Name & """>"
End Function
Function ParseAccessTable(myDBName As String, myTableName As String)
'Access/DAO Declarations.
Dim myDBApp As Access.Application
Dim myRecordSet As DAO.recordset
Dim myDBField As DAO.Field
'FrontPage Page object model declarations.
Dim myPage As PageWindowEx
Dim myTable As FPHTMLTable
Dim myTableRow As FPHTMLTableRow
Dim myTableCell As FPHTMLTableCell
'Function declarations.
Dim myCount As Integer
Dim myFieldValue As String
Dim myRecordCount As Integer
myRecordCount = 0
'Function constants.
Const myTempPage = "tmp.htm"
'Get the current Access database.
On Error GoTo AccessNotThereYet
Set myDBApp = GetObject(, "Access.Application")
'Get the database table.
On Error Resume Next
Set myRecordSet = myDBApp.CurrentDb.OpenRecordset(myTableName)
'Add a new page to the current web.
Set myPage = ActiveWeb.LocatePage(myTempPage)
myPage.SaveAs myTableName & ".htm"
'Delete the temporary file from web.
ActiveWeb.LocatePage(myTempPage).File.Delete
'Add a database-ready table to the page with the proper number of fields.
AddDBTableToPage myPage, myTableName, myRecordSet.Fields.Count
'Get a reference to the table.
Set myTable = myPage.Document.all.tags("table").Item(0)
'Populate the first row.
For myCount = 0 To myRecordSet.Fields.Count - 1
myTable.rows(0).cells(myCount).innerHTML = "<b>" & _
Trim(myRecordSet.Fields(myCount).Name) & "</b>"
Next
'Populate the rest of the table.
While Not (myRecordSet.EOF)
AddDBRow myTable
Set myTableRow = myTable.rows(myTable.rows.Length - 1)
For myCount = 0 To myRecordSet.Fields.Count - 1
Set myTableCell = myTableRow.cells(myCount)
If IsNull(myRecordSet.Fields(myCount)) Then
myFieldValue = "None"
Else
myFieldValue = Trim(myRecordSet.Fields(myCount).Value)
End If
If myRecordSet.Fields(myCount).Type = DAO.dbMemo Then
myFieldValue = AddMemo(myPage, _
myRecordSet.Fields(myCount).Value, _
myRecordSet.Fields(myCount).Name, myRecordCount)
End If
myTableCell.innerHTML = myFieldValue
Next myCount
myRecordSet.MoveNext
myRecordCount = myRecordCount + 1
Wend
myPage.Save
myDBApp.Quit
Exit Function
AccessNotThereYet:
Debug.Print Err.Number & ":" & Err.Description
Resume
End Function
Private Sub ParseDBTable()
Call ParseAccessTable("Northwind.mdb", "Products")
End Sub
FrontPage 不支持多重工程。您可以安装 Microsoft Office XP Developer 来增加 Visual Basic 编辑器的灵活性。Office XP Developer 提供了创建多重工程及编译 .dll 文件的功能,并且能更有效地管理 Visual Basic 工程。