创建框架页

在 Word 中,可以在网页设计中使用框架,使信息井然有序且易于访问。 框架页也称框架集,是一个被分成两个或更多部分的网页,其中每部分都指向另一个网页。 框架页中的框架又可指向其他框架页。 有关在 Word 用户界面中创建框架和框架页面的信息,请参阅 Word 帮助。

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

HTML 中的框架页

In HTML, frames pages and the frames they contain are built using a hierarchical set of <FRAMESET> and <FRAME> tags. A frameset can contain both frames and other framesets. For example, the following HTML creates a frameset with a frame on top and a frameset immediately below it. That frameset contains a frame on the left and a frameset on the right. That frameset contains two frames, one on top of the other.

<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 示例,请将该示例粘贴到空白文本文档中,将文档重命名为“framespage.htm”,并在 Word 或 Web 浏览器中打开文档。

Frameset 对象

Frameset 对象同时拥有两种标记的功能。 每个 Frameset 对象的类型为 wdFramesetTypeFramesetwdFramesetTypeFrame,分别表示 HTML 标记 <FRAMESET> 和 <FRAME> 。 以“Frameset”开头的属性适用于 wdFramesetTypeFrameset 类型的 Frameset 对象, (FramesetBorderColorFramesetBorderWidth 。 以“Frame”开头的属性适用于类型为 wdFramesetTypeFrame (FrameDefaultURLFrameDisplayBordersFrameLinkToFileFrameNameFrameResizableFrameScrollBarType) 的 Frameset 对象

剖析 Frameset 对象的层次结构

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

MyFrameset.ChildFramesetItem(n)

返回对应于第 n 个第一级 <FRAMESET$gt; 或 $lt 的 Frameset 对象;FRAME$gt;与 相对应的 MyFramesetFRAMESET$gt; 和 </FRAMESET$gt; 标记之间的<标记。

如果 MyFrameset 是对应于最外层$gt的 Frameset 对象;FRAMESET$gt;标记在前面的 HTML 示例中返回MyFrameset.ChildFramesetItem(1)类型为 wdFramesetTypeFrameFrameset 对象,该对象对应于名为“top”的帧。同样,MyFrameset.ChildFramesetItem(2)返回 wdFramesetTypeFrameset 类型的 Frameset 对象,该对象本身包含两个 Frameset 对象:第一个对象对应于名为“left”的帧,第二个对象的类型为 wdFramesetTypeFrameset

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

下列 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 对象访问;而对 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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。