创建框架页

   

在 Microsoft Word 中,可在 Web 页的设计中使用框架,以便组织信息并使其易于访问。框架页也称为框架集,它是被分成两个或更多框架的 Web 页,其中的每个框架都指向其他的 Web 页。框架页中的框架也可指向其他的框架页。有关在 Word 用户界面中创建框架和框架页的详细信息,请参阅创建框架和框架页

框架和框架页用一系列 HTML 标记创建。通过检查其 HTML 标记,可更好地理解带有框架和框架页的 Visaul Basic 对象模块。

HTML 中的框架页

在 HTML 中,框架页及其所包含的框架用一系列分级的 <FRAMESET> 和 <FRAME> 标记建立。框架集可包含框架和其他的框架集。例如,下列 HTML 建立了一个框架集,其上部有一个框架,在该框架下面紧接着有一个框架集;该框架集的左侧包含一个框架,右侧包含一个框架集;而该框架集又包含两个框架,其中一个在另一个的上方。

<FRAMESET ROWS="100, *">
    <FRAME NAME=top SRC="banner.htm">
    <FRAMESET COLS="20%, *">
        <FRAME NAME=left SRC="contents.htm">
        <FRAMESET ROWS="75%, *">
            <FRAME NAME=main SRC="main.htm">
            <FRAME NAME=bottom SRC="footer.htm">
        </FRAMESET>
    </FRAMESET>
</FRAMESET>

注意  若要更好地理解上述 HTML 示例,可将该示例粘贴到空白的文本文档中,将文档重命名为“framspage.htm”,然后在 Word 或 Web 浏览器中打开该文档。

Frameset 对象

Frameset 对象同时拥有两种标记的功能。每个 Frameset 对象是 wdFramesetTypeFramesetwdFramesetTypeFrame 类型,它们分别代表 HTML 标记 <FRAMESET> 和 <FRAME>。以“Frameset”开头的属性应用于 wdFramesetTypeFrameset 类型(FramesetBorderColorFramesetBorderWidth)的 Frameset 对象。以“Frame”开头的属性应用于 wdFrameserTypeFrame 类型(FrameDefaultURLFrameDisplayBordersFrameLinkToFileFrameNameFrameResizableFrameScrollbarType)的 Frameset 对象。

剖析 Frameset 对象的分级结构

因为框架页定义为一系列分级的 HTML 标记,所以访问 Frameset 对象的对象模型也是分级的。可使用 ChildFramesetItemParentFrameset 属性剖析 Frameset 对象的分级结构。例如:

MyFrameset.ChildFramesetItem(n)

对应于和 MyFrameset 相关的 <FRAMESET> 和 </FRAMESET> 标记之间的第 n 个一级 <FRAMESET> 或 <FRAME> 标记,返回一个 Frameset 对象。

在上面的 HTML 示例中,如果 MyFrameset 是对应于最外面的 <FRAMESET> 标记的 Frameset 对象,则 MyFrameset.ChildFramesetItem(1) 将返回与名为“top”的框架相对应的 wdFramesetTypeFrame 类型的 Frameset 对象。类似地,MyFrameset.ChildFramesetItem(2) 返回一个 wdFramesetTypeFrameset 类型的 Frameset 对象,它本身包含两个 Frameset 对象:第一个对象对应于名为“left”的框架,第二个对象是 wdFramesetTypeFrameset 类型的对象。

wdFramesetTypeFrame 类型的 Frameset 对象没有子框架,而 wdFramesetTypeFrameset 类型的 Frameset 对象至少有一个子框架。

下列 Visual Basic 示例显示了上面 HTML 示例中定义的四个框架的名称。

Dim MyFrameset As Frameset
Dim Name1 As String
Dim Name2 As String
Dim Name3 As String
Dim Name4 As String

Set MyFrameset = ActiveWindow.Document.Frameset

With MyFrameset
    Name1 = .ChildFramesetItem(1).FrameName
    With .ChildFramesetItem(2)
        Name2 = .ChildFramesetItem(1).FrameName
        With .ChildFramesetItem(2)
            Name3 = .ChildFramesetItem(1).FrameName
            Name4 = .ChildFramesetItem(2).FrameName
        End With
    End With
End With

Debug.Print Name1, Name2, Name3, Name4

单个框架和整个框架页

若要返回与框架页上特定框架相关的 Frameset 对象,可使用 Pane 对象的 Frameset 属性。例如:

ActiveWindow.Panes(1).Frameset

返回与框架页上第一个框架相对应的 Frameset 对象。

框架页本身是一篇文档,该文档与构成单独框架内容的文档是分开的。对与框架页相关的 Frameset 对象的访问通过与之对应的 Document 对象进行;而对该对象的访问则通过显示框架页的 Window 对象进行。例如:

ActiveWindow.Document.Frameset

返回当前窗口中框架页的 Frameset 对象。

注意  使用框架页时,ActiveDocument 属性返回与活动窗格的框架相关的 Document 对象,而不是整个框架页。

从头创建一个框架页及其内容

本示例新建一个带有三个框架的框架页,将文本添加到每个框架,并为每个框架设置背景颜色。它将两个超链接插入左侧的框架中:第一个超链接打开主框架的 One.htm 文档,而第二个超链接在整个窗口中打开名为 Two.htm 的文档。为了使超链接正常工作,必须创建名为 One.htm 和 Two.htm 的文件或将更改现有文件的名称。

注意  创建每个框架时,Word 将新建一个文档,其内容将装载于新的框架中。本示例在保存框架页时自动保存与三个框架相关的文档。

Sub FramesetExample1()

    ' Create new frames page.
    Documents.Add DocumentType:=wdNewFrameset

    With ActiveWindow
        ' Add text and color to first frame.
        Selection.TypeText Text:="BANNER FRAME"
        With ActiveDocument.Background.Fill
            .ForeColor.RGB = RGB(204, 153, 255)
            .Visible = msoTrue
        End With

        ' Add new frame below top frame.
        .ActivePane.Frameset.AddNewFrame _
            wdFramesetNewFrameBelow
        ' Add text and color to bottom frame.
        .ActivePane.Frameset.FrameName = "main"
        Selection.TypeText Text:="MAIN FRAME"
        With ActiveDocument.Background.Fill
            .ForeColor.RGB = RGB(0, 128, 128)
            .Visible = msoTrue
        End With

        ' Add new frame to left of bottom frame.
        .ActivePane.Frameset.AddNewFrame _
            wdFramesetNewFrameLeft
        ' Set the width to 25% of the window width.
        With .ActivePane.Frameset
            .WidthType = wdFramesetSizeTypePercent
            .Width = 25
            .FrameName = "left"
        End With
        ' Add text and color to left frame.
        Selection.TypeText Text:="LEFT FRAME"
        With ActiveDocument.Background.Fill
            .ForeColor.RGB = RGB(204, 255, 255)
            .Visible = msoTrue
        End With
        Selection.TypeParagraph
        Selection.TypeParagraph
        ' Add hyperlinks to left frame.
        With ActiveDocument.Hyperlinks
            .Add Anchor:=Selection.Range, _
                Address:="one.htm", Target:="main"
            Selection.TypeParagraph
            Selection.TypeParagraph
            .Add Anchor:=Selection.Range, _
                Address:="two.htm", Target:="_top"
        End With
       
        ' Activate top frame.
        .Panes(1).Activate
        ' Set the height to 1 inch.
        With .ActivePane.Frameset
            .HeightType = wdFramesetSizeTypeFixed
            .Height = InchesToPoints(1)
            .FrameName = "top"
        End With

        ' Save the frames page and its associated files.
        .Document.SaveAs FileName:="default.htm", _
            FileFormat:=wdFormatHTML
    End With

End Sub

创建显示现有文件内容的框架页

本示例创建一个类似于上述示例的框架页,但为每个框架设置了指向现有文档的默认 URL,以便在框架中显示文档内容。要让本示例工作,必须创建名为 Main.htm、Left.htm、和 Banner.htm 的文件或更改现有的文件名。

Sub FramesetExample2()
   
    ' Create new frames page.
    Documents.Add DocumentType:=wdNewFrameset

    With ActiveWindow
        ' Add new frame below top frame.
        .ActivePane.Frameset.AddNewFrame _
            wdFramesetNewFrameBelow
        ' Set the name and initial page for the frame.
        With .ActivePane.Frameset
            .FrameName = "main"
            .FrameDefaultURL = "main.htm"
        End With
       
        ' Add new frame to left of bottom frame.
        .ActivePane.Frameset.AddNewFrame _
            wdFramesetNewFrameLeft
        With .ActivePane.Frameset
            ' Set the width to 25% of the window width.
            .WidthType = wdFramesetSizeTypePercent
            .Width = 25
            ' Set the name and initial page for the frame.
            .FrameName = "left"
            .FrameDefaultURL = "left.htm"
        End With
   
        ' Activate top frame.
        .Panes(1).Activate
        With .ActivePane.Frameset
            ' Set the height to 1 inch.
            .HeightType = wdFramesetSizeTypeFixed
            .Height = InchesToPoints(1)
            ' Set the name and initial page for the frame.
            .FrameName = "top"
            .FrameDefaultURL = "banner.htm"
        End With

        ' Save the frameset.
        .Document.SaveAs FileName:="default.htm", _
            FileFormat:=wdFormatHTML
    End With

End Sub