Share via


建立框架頁

在 Word 中,您可以在網頁設計中使用框架,讓您的資訊井然有序且容易存取。 框架頁也稱為框架組,它是分成兩個或更多區段的網頁,其中每個區段都指向其他網頁。 框架頁中的框架也能指向其他框架頁。 如需在 Word 使用者介面中建立框架和框架頁的相關資訊,請參閱 Word 說明。

框架和框架頁是用一系列的 HTML 標籤所建立的。 如果想更加了解運用框架和框架頁的 Visaul Basic 物件模型,就需要對 HTML 標籤做進一步的探討。

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 或網頁瀏覽器中開啟檔。

Frameset 物件

Frameset 物件同時包含兩種標記的功能。 每個 Frameset 物件的類型為 wdFramesetTypeFramesetwdFramesetTypeFrame,分別代表 HTML 標籤 < FRAMESET > 和 < FRAME > 。 以 「Frameset」 開頭的屬性會套用至類型為 wdFramesetTypeFramesetFrameset物件 (FramesetBorderColorFramesetBorderWidth 。 以 「Frame」 開頭的屬性會套用至FrameDefaultURL (FrameDefaultURLFrameDisplayBordersFrameLinkToFileFrameNameFrameResizableFrameScrollBarType) 類型的Frameset物件。

遍歷 Frameset 物件的階層式結構

因為框架頁定義為階層式的 HTML 標籤,所以存取 Frameset 物件的物件模型也是階層式的。 可使用 ChildFramesetItemParentFrameset 屬性來往返 Frameset 物件的階層式結構。 例如:

MyFrameset.ChildFramesetItem(n)

會傳回對應至第n個第一層 < FRAMESET$gt; 或 $lt; 的Frameset物件;FRAME$gt;FRAMESET$gt; 與 < /FRAMESET$gt; 之間 < 標記,對應至 MyFrameset

如果 MyFrameset 是對應至最外層$gt的Frameset物件;FRAMESET$gt;上述 HTML 範例中的 tags 會 MyFrameset.ChildFramesetItem(1) 傳回wdFramesetTypeFrame類型的Frameset物件,該物件對應至名為 「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

單個框架和整個框架頁

可使用 Pane 物件的 Frameset 屬性來傳回與框架頁上的特定框架相關的 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 支援與意見反應