How to: Hide Text in Documents
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Hide Text in Documents


Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

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 Bookmark control before printing your document.

To hide text in a Bookmark control while printing the document

  1. Create a procedure that hides the text of the given range.

    static void HideText(Microsoft.Office.Interop.Word.Range rng)
        rng.Font.Hidden = 1;  // 1 = True
  2. Create a procedure that unhides the text of a given range.

    static void UnhideText(Microsoft.Office.Interop.Word.Range rng)
        rng.Font.Hidden = 0;  // 0 = False
  3. Pass the range of a Bookmark control to the HideText method, print the document, and then pass the same range to the UnhideText method.

    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;
        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);

Compiling the Code

This code example assumes you have an existing Bookmark control named Bookmark1 on the document.

See Also

© 2015 Microsoft