Share via


How to: Hide Text in Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can hide text in a document by setting the Hidden property of the Font for a particular range of text.

For example, you can temporarily hide the text within a Microsoft.Office.Tools.Word.Bookmark (in a document-level customization) or a Bookmark (in an application-level add-in) before sending a document to a printer.

To hide text in a Bookmark control while printing the document

  1. Create a procedure that hides all text that is in a specified range.

    Shared Sub HideText(ByVal rng As Word.Range)
        rng.Font.Hidden = True 
    End Sub
    
    static void HideText(Word.Range rng)
    {
        rng.Font.Hidden = 1;  // 1 = True
    }
    
  2. Create a procedure that unhides all text that is in a specified range.

    Shared Sub UnhideText(ByVal rng As Word.Range)
        rng.Font.Hidden = False 
    End Sub
    
    static void UnhideText(Word.Range rng)
    {
        rng.Font.Hidden = 0;  // 0 = False
    }
    
  3. Pass the range of a bookmark to the HideText method, print the document, and then pass the same range to the UnhideText method.

    The following code example can be used in a document-level customization. To use this example, run it from the ThisDocument class in your project.

    HideText(Bookmark1.Range)
    
    Me.PrintOut()
    
    UnhideText(Bookmark1.Range)
    
    HideText(bookmark1.Range);
    
    object oTrue = true;
    object oFalse = false;
    object range = Word.WdPrintOutRange.wdPrintAllDocument;
    object items = Word.WdPrintOutItem.wdPrintDocumentContent;
    object copies = "1";
    object pages = "";
    object pageType = Word.WdPrintOutPages.wdPrintAllPages;
    
    this.PrintOut(
        ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
    
    UnhideText(bookmark1.Range);
    

    The following code example can be used in an application-level add-in. This example uses the active document. To use the example, run it from the ThisAddIn class in your project.

    HideText(Bookmark1.Range)
    Me.Application.ActiveDocument.PrintOut()
    UnhideText(Bookmark1.Range)
    
    HideText(bookmark1.Range);
    
    object oTrue = true;
    object oFalse = false;
    object range = Word.WdPrintOutRange.wdPrintAllDocument;
    object items = Word.WdPrintOutItem.wdPrintDocumentContent;
    object copies = "1";
    object pages = "";
    object pageType = Word.WdPrintOutPages.wdPrintAllPages;
    
    this.Application.ActiveDocument.PrintOut(
        ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
    
    UnhideText(bookmark1.Range);
    

Compiling the Code

This code example assumes that the document contains a Microsoft.Office.Tools.Word.Bookmark control (in a document-level customization) or Bookmark control (in an application-level add-in) that is named bookmark1.

See Also

Tasks

How to: Print Documents

How to: Define and Select Ranges in Documents

How to: Reset Ranges in Word Documents

How to: Update Bookmark Text

Concepts

The Variable missing and Optional Parameters in Office Solutions