Link to home
Start Free TrialLog in
Avatar of jdgressett
jdgressettFlag 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.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Else
    sec.Range.Font.Hidden = False
End If

End Sub

Open in new window

Avatar of jdgressett

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solves the problem.