Logo Background

Hide text in Word document

  • Having written a substantial Word 2003 document I would like to have a portion of the text hidden behind a button which can trigger between showing and hiding this text when the button is clicked. Can this be accomplished in Word? The computer is running Office 2003 with Windows XP.

    While Word is not designed for this type of functionality, you will be glad to know this is possible using a combination of macros, Visual Basic for Applications code and buttons within the Word document. This may sound like a difficult process and it is unfortunately quite complex. As a starting point, I would like to direct you to a website which contains an overview on how to show and hide (toggle) text within a Word document using buttons: gregmaxey.mvps.org/Toggle_Data_Display.htm . Unfortunately, this website does not provide step-by-step instructions on the exact procedure, and it took me quite a while to figure out exactly how the toggle system worked. So, I will need to provide some additional guidance on how to achieve the results outlined on the site.

    This webpage provides two methods for toggling text within a Word document. The first method uses a combination of autotext, field codes and a Visual Basic macro. The second method uses a button to change the height of a table row to display and hide text. Both these methods have their advantages and disadvantages, so as a starting point I suggest that you download the template from the website (the download link is located near the bottom of the web page) which has examples of these two methods, allowing you to determine which is the most suitable in your case. Be aware, because the document uses macros Word may give you an alert that, due to security restrictions, macros in the document have been disabled. Should this be the case, you will need to enable macros in the document, otherwise the toggle will not work correctly. To do this, go to the “Tools” menu > “Macro” > “Security”. In the Security window that appears, select the “Medium” option and click OK. Close Word and then re-open Word and the template document. A message should appear asking whether you would like to enable Macros in the document. Click “Enable Macros”. The text toggles should now work within the document. These macro security issues are also likely to arise once you implement the toggles within your document, meaning that anyone who receives your document will be presented with the security alert. You must be aware of this issue, so you can direct others who may open your document that they need to allow the macros to run.

    We will now go through the two methods which can be used to toggle between displaying and hiding text. I suggest that you create a new document on which you can experiment. When applying this to your actual document, make sure you have a backup of your original document, since it is highly likely that mistakes may be made when testing the toggle procedures and you will need to revert to a backup copy of the document. Therefore, I suggest you have multiple backup copies so you do not risk losing the original document. Once you have your backup copies we can continue with the procedure. Before commencing the procedure, open the “Toggle Events.dot” file which can be downloaded from the aforementioned website, as this contains code you will need to use within your document.

    We will first consider the method which uses a combination of a macro and AutoText to hide and display text. The premise behind this method is the text you wish to toggle on/off is stored within Word’s AutoText function. When you double-click on a button the AutoText is triggered-on and displayed, and when you double-click on the button again the AutoText is triggered off and disappears. As the first step, you need to add the text you wish to toggle as AutoText. Open the document to which you wish to add the toggle functionality. Select the text to be toggled and go to the “Insert” menu > “AutoText” > “AutoText”. In the AutoCorrect window that appears, the “AutoText” tab should be selected with the highlighted text in the Preview section. Give this text segment a name (e.g. I suggest a naming convention that all your other toggle text sections can follow, such as TextToggle1) and click “Add”. The text has now been added to the AutoText for the document. Click OK to save and close. You can now delete the text from the document itself.

    Now that the text has been configured within the AutoText function in Word we need to write the relevant code to hide and display this text. First, we will include the relevant behind the scenes macro code which will actually perform the show and hide functions. Go to the “Tools” menu > “Macro” > “Visual Basic Editor”. In the Microsoft Visual Basic editor window that appears, right-click on your document name in the left-hand pane and select “Insert” > “Module”. Be aware, there will be several documents listed (as the Visual Basic Editor displays the code for all open documents) so make sure you right-click on the correct document. You will notice that a new folder titled “Modules” appears with an entry named “Module1”. Double-click on “Module1” and a blank code window will open in the right-hand pane. We now need to copy the code from the template to perform the toggle operation. Expand the “TemplateProject (Toggle Events)” document > “Modules” and double-click on “Module1”. You will notice a new code window appears, with a lot of code. Select all this code, and then double-click on the “Module1” which you created for your document and paste the code into this module. Once this has been done, the code is now in place and you can close the Visual Basic Editor. You do not need to worry about saving the changes, as this will be done when you save your document.

    Next, we will insert a placeholder for the text which needs to be displayed/hidden. We will be using field codes for this purpose, which are special codes normally not displayed within documents but are hidden and perform special formatting functions. Position the cursor where you would like the text to be displayed, and then go to the “Insert” menu > “Field”. In the Field window that appears, select “If” from the list of Field Names. Then, on the right-hand side of the window you will see the “Advanced field properties” with a space for typing the field code. At the moment the field code should just have the text IF stated. We need to change this to include references to the text to be displayed, in addition the condition for when the text should be displayed or hidden. For the field code, type:
    IF { DOCVARIABLE ShowHide1 } = “Show” “{ AUTOTEXT TextToggle1 }”

    Next, you need to insert the toggle button to trigger the display of the text. This button will call the toggle macro code (which we copied and pasted using the Visual Basic Editor earlier) to show or hide the relevant text. The actual item which you click within the document is not a button per-se, but instead is simply a character of some description. This can be anything you like, and within the sample template you will notice a target symbol has been selected for ease of recognition. Insert the symbol you wish to function as the toggle button into the document by going to the “Insert” menu > “Symbol” and selecting the relevant symbol to insert into the document. We now need to cut that symbol out of the document, as the symbol will be reinserted within the field code when we insert the field code into the document. So, highlight the symbol and right-click then select “Cut”. Once this has been done, we are ready to insert the field code into the document. Go back to the “Insert” menu > “Field” and select the “MacroButton” field name. Then, in the “Display text” field, paste the symbol (as this will be the text which you double-click to activate the toggle macro). Finally, under “Macro name” select “CallShowHide” and click OK to insert the field codes into the document.

    At this point, you will see various field codes displayed in Word. As the field codes are being displayed, the toggling will not work. So, you need to hide the field codes by pressing the ALT-F9 combination on your keyboard. Once this has been done the field codes should disappear and all that will be left is the symbol you selected as the button to toggle the display and hide of the toggled text. In theory, if you double-click on this symbol it should toggle the text to display and double-clicking again should hide the text.

    There is a second method you can use to show and hide text, which involves using a table with two rows. The top row of the table contains the text to always be displayed (or no text, if you do not wish to have any text in the top row) and the button row contains the text to toggle. In this scenario the toggle works by collapsing the bottom row (i.e. making the row height 0 pixels) making it seem like the text has disappeared, even though the table row containing the text has simply been set with a very small height value. Unfortunately I do not have sufficient room in this column to explain how this method works, but it does follow the generally same premise as the first method. As such, you should be able to figure out how this works from exploring the provided template.

    By now you will have gathered that both methods are rather complicated, and may not work on your first attempt. I suggest that before you endeavour to attempt to implement the toggle systems within your own documents that you experiment with the templates to see how it works, and ensure you have a full understanding of how the system works.

Leave a Comment
Hi there. If this is the first time you are posting a comment it will not appear immediately, but needs to be approved. This is necessary in order to combat comment spam. However, once you have submitted a comment (which is subsequently approved) you do not need to go through this process again - the site remembers who you are and auto-approves your comments. Nifty eh? Anyway, sorry about the inconvenience that this may cause for your first comment post.