Condividi tramite


How to: Resize Bookmark controls

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You set the size of a Bookmark control when you add it to a Microsoft Office Word document. You can also resize it at a later time.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

There are three ways to resize a bookmark:

  • Add or remove text in the Bookmark control.

    Whenever you add text in a bookmark, the size of the bookmark automatically increases to contain the new text. When you delete text, the size of the bookmark automatically decreases.

  • Change the Start and End properties of the Bookmark control.

    This is useful if you are changing the size by only a few characters.

  • Recreate the Bookmark control.

    This is useful if there is a substantial change in the size or location of a bookmark.

    In document-level projects, you can add Bookmark controls to the document in your project at design time or at run time. In VSTO Add-in projects, you can add Bookmark controls to any open document at run time. For more information, see How to: Add Bookmark controls to Word documents.

    Note

    Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.

Change the start and end properties

To resize a bookmark in a document-level project at design time

  1. Select the bookmark in the Properties window.

  2. Increase or decrease the value of the Start property.

  3. Increase or decrease the value of the End property.

To resize a bookmark in a document-level project at run time

  1. Modify the Start and End properties of a Bookmark you created at run time or at design time.

    The following code example adds five characters to the start of a bookmark named SampleBookmark. This code assumes that there are at least five characters of text before the bookmark.

    this.SampleBookmark.Start = this.SampleBookmark.Start - 5;
    
    Me.SampleBookmark.Start = Me.SampleBookmark.Start - 5
    

    The following code example adds five characters to the end of the same bookmark. This code assumes that there are at least five characters of text after the bookmark.

    this.SampleBookmark.End = this.SampleBookmark.End + 5;
    
    Me.SampleBookmark.End = Me.SampleBookmark.End + 5
    

To resize a bookmark in a VSTO Add-in project at run time

  1. Modify the Start and End properties of a Bookmark you created at run time.

    The following code example creates a Bookmark that contains the text in the first paragraph of the active document, and then removes five characters from the start and end of the Bookmark.

    
    Dim VstoDocument As Microsoft.Office.Tools.Word.Document = _
        Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    
    vstoDocument.ActiveWindow.View.ShowBookmarks = True
    vstoDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
    Dim firstParagraph As Word.Range = vstoDocument.Paragraphs(1).Range
    firstParagraph.Text = "123456789abcdefghijklmnopqrstuvwxyz"
    
    Dim sampleBookmark As Microsoft.Office.Tools.Word.Bookmark = _
        vstoDocument.Controls.AddBookmark(firstParagraph, "bookmark1")
    sampleBookmark.Start = sampleBookmark.Start + 5
    sampleBookmark.End = sampleBookmark.End - 5
    
    Microsoft.Office.Tools.Word.Document vstoDocument =
        Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
    
    vstoDocument.ActiveWindow.View.ShowBookmarks = true;
    vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
    Word.Range firstParagraph = vstoDocument.Paragraphs[1].Range;
    firstParagraph.Text = "123456789abcdefghijklmnopqrstuvwxyz";
    
    Microsoft.Office.Tools.Word.Bookmark sampleBookmark =
        vstoDocument.Controls.AddBookmark(firstParagraph, "bookmark1");
    sampleBookmark.Start = sampleBookmark.Start + 5;
    sampleBookmark.End = sampleBookmark.End - 5;
    

Recreate the bookmark

You can resize a bookmark in a document-level project by adding a new bookmark that has the same name as the existing bookmark, but that has a different size.

To recreate a bookmark in a document-level project at design time

  1. Select the text to be included in the new Bookmark control.

  2. On the Insert menu, click Bookmark.

  3. In the Bookmark dialog box, select the name of the bookmark that you want to resize and click Add.

See also