Avatar of jdgressett
Flag for United States of America asked on

I need a Microsoft Word 2003 macro that hides or reveals text based on content of bookmark

I have had a do-it-yesterday Word 2003 project dumped on me and I am not a Word macro wizard.  I need to make a section of text  visible if a bookmark contained in that section has non-blank text in it; otherwise, that same block of text must disappear from view; it must reappear if the content of the bookmark goes back to non-blank text.

This is easy to do manually - I select the text that I want to hide, right-click, select font, and check the box to hide the text, or uncheck it, as needed. I just need to automate this based on the content of the bookmark.
Microsoft OfficeMicrosoft Word

Avatar of undefined
Last Comment

8/22/2022 - Mon

Not sure how you want to designate the target text. Since  you mention sections, I have written this for Section 1.
Sub HideText()
Dim sec As Section

Set sec = ActiveDocument.Sections(1)
If Len(ActiveDocument.Bookmarks("MyBookmark").Range) = 0 Then
    sec.Range.Font.Hidden = True
    sec.Range.Font.Hidden = False
End If

End Sub

Open in new window


This gets me much closer to what I need.  I don't know enough about Word 2003 document structure to identify segments or know how they are numbered. All of the Word documents that need this are created from specialty templates. In all of these templates, the main structure of the document is a header, footer, and main document body. In all of these, the structure is made entirely of Word tables, and all text and bookmarks are inside table cells. The particular table that has the bookmark that I need to use to control visibility is a simple one with one row and two columns (but that might change), with the bookmark being the only content in the second column. I need to cause the entire table and the blank line separating it from the next table to disappear when there is nothing in the bookmark

 Also, there are several templates in C:\Program Files (x86)\Microsoft Office\OFFICE11\STARTUP that define macros. I would like to get this to run when the document opens, but I can't get anything in the Document_Open in the specialty templates to run. This may be to the way that Word is started - it is embedded in an application which spawns Word and stuffs text into the bookmarks so that the documents are created with a substantial amount of content when they are first opened, and then the users will add even more. When the document is saved, control passes back to the application.

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

This solves the problem.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy