使用內容控制檔的某些部分

當您保護文件的某個部分時,使用者即無法變更或刪除文件中該部分的內容。 您可使用多種方法,透過內容控制項來保護 Microsoft Office Word 文件的下列部分:

  • 您可以保護內容控制項。

  • 您可以保護文件中不在內容控制項內的部分。

    適用於: 本主題中的資訊適用於 Word 的文件層級專案和 VSTO 載入宏專案。 如需詳細資訊,請參閱 Office 應用程式 lication 和項目類型所提供的功能。

保護內容控制件

您可以藉由在設計時間或運行時間設定檔層級專案中的控件屬性,防止使用者編輯或刪除內容控制件。

此外,您也可以使用 VSTO 增益集專案,保護於執行階段加入文件的內容控制項。 如需詳細資訊,請參閱 如何:將內容控件新增至 Word 檔

若要在設計階段保護內容控制項

  1. 在 Visual Studio 設計工具中裝載的檔案中,選取您想要保護的內容控制件。

  2. 在 [ 屬性] 視窗中,設定下列其中一個或兩個屬性:

    • 若要防止使用者編輯控件,請將LockContents設定True

    • 若要防止使用者刪除控件,請將LockContentControl設定True

  3. 按一下 [確定]

在運行時間保護內容控制件

  1. LockContents 內容控制件的屬性設定為 true ,以防止使用者編輯控件,並將 屬性設定 LockContentControltrue 以防止使用者刪除控制項。

    下列程式碼範例示範在文件層級專案中,使用兩個不同 RichTextContentControl 物件的 LockContentsLockContentControl 屬性。 若要執行這個程式碼,請將程式碼加入專案的 ThisDocument 類別中,並從 AddProtectedContentControls 事件處理常式呼叫 ThisDocument_Startup 方法。

    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        deletableControl = this.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = this.Paragraphs[2].Range;
    
        editableControl = this.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it";
        editableControl.LockContentControl = true;
    }
    

    下列程式碼範例示範在 VSTO 增益集專案中,使用兩個不同 RichTextContentControl 物件的 LockContentsLockContentControl 屬性。 若要執行這個程式碼,請將程式碼加入專案的 ThisAddIn 類別中,並從 AddProtectedContentControls 事件處理常式呼叫 ThisAddIn_Startup 方法。

    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        Microsoft.Office.Tools.Word.Document vstoDocument = 
            Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
    
        vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = vstoDocument.Paragraphs[1].Range;
    
        deletableControl = vstoDocument.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = vstoDocument.Paragraphs[2].Range;
    
        editableControl = vstoDocument.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it.";
        editableControl.LockContentControl = true;
    }
    

保護不在內容控制件中檔的一部分

您可以將文件區域放入 GroupContentControl 中,防止使用者變更該區域。 這在下列案例中很有用:

  • 您想要保護不含內容控制項的區域。

  • 您想要保護已包含內容控制項的區域,但是想要保護的文字或其他項目不在內容控制項內。

注意

如果建立的 GroupContentControl 包含內嵌內容控制項,則不會自動保護這些內嵌內容控制項。 若要防止使用者編輯內嵌的內容控制件,請使用 控件的LockContents 屬性。

若要在設計階段保護文件的區域

  1. 在 Visual Studio 設計工具中裝載的檔案中,選取您想要保護的區域。

  2. 按一下 [功能區] 上的 [開發人員] 索引標籤。

    注意

    如果 [開發人員] 索引標籤沒有顯示,您必須先使其顯示。 如需詳細資訊,請參閱 如何:在功能區上顯示開發人員索引標籤。

  3. 在 [ 控件] 群組中,按兩下 [ 群組 ] 下拉式按鈕,然後按兩下 [ 群組]。

    這樣會在專案的 ThisDocument 類別中,自動產生內含保護區域的 GroupContentControl。 表示群組控件的框線在設計時間是可見的,但在運行時間沒有可見的框線。

在運行時間保護檔的區域

  1. 以程式設計方式選取想要保護的區域,然後呼叫 AddGroupContentControl 方法以建立 GroupContentControl

    下列文件層級專案的程式碼範例會將文字加入文件的第一個段落,然後選取此段落,再執行個體化 GroupContentControl。 若要執行這個程式碼,請將程式碼加入專案的 ThisDocument 類別中,並從 ProtectFirstParagraph 事件處理常式呼叫 ThisDocument_Startup 方法。

    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
    private void ProtectFirstParagraph()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        range1.Text = "You cannot edit or change the formatting of text " +
            "in this sentence, because this sentence is in a GroupContentControl.";
        range1.Select();
        groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
    }
    

    下列 VSTO 增益集專案的程式碼範例會將文字加入使用中文件的第一個段落,然後選取此段落,再執行個體化 GroupContentControl。 若要執行這個程式碼,請將程式碼加入專案的 ThisAddIn 類別中,並從 ProtectFirstParagraph 事件處理常式呼叫 ThisAddIn_Startup 方法。

           private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
           private void ProtectFirstParagraph()
           {
               Microsoft.Office.Tools.Word.Document vstoDocument =
                   Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
    
               vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
               Word.Range range1 = vstoDocument.Paragraphs[1].Range;
               range1.Text = "You cannot edit or change the formatting of text " +
                   "in this sentence, because this sentence is in a GroupContentControl.";
               range1.Select();
    
               groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1");
           }