Macro for Conditional Text

A co-worker has asked me about creating an interactive document where they could show/hide portions of a contract based on which services the client elects to purchase.  

So I found this awesome set of macros (http://www.abelard.com.au/words-3-2.pdf), but they're slightly out-dated.  They don't completely work, and I'm not sure if it's the tagging or the processing of the tags that's not working properly.  It might be that I'm copying/pasting it wrong?  I'm pasting it at the end of the post for you to take a look at.  I can see the tags when I complete that part of the process, but then when I go to process the tags, no matter what I put in the text box it just automatically hides everything on the page.  

But my main question here is twofold.  Is this the best way to go about using conditional text in word?  For tech writers out there, I'd like to mimic the type of functionality that exists in FrameMaker for conditional formatting.  If so, I'm also wondering how to display, like it shows in the article, a Display Content window from which the user can select the tags to display.  So for example, if you take a series of paragraphs with JQ, JA, SQ, and SA tags, you could select to display any combination of these tags.  

Sub AddTagToParagraphs()
 Dim newTag As String, oldTag As String
 Dim myPara As Paragraph
 oldTag = TagPara(Selection.Paragraphs(1), "")
 newTag = UCase(InputBox("Type tag for selected paragraph(s).", "Single Source ", oldTag))
 For Each myPara In Selection.Paragraphs
 oldTag = TagPara(myPara, newTag)
 Next myPara
 Set myPara = Nothing
End Sub

Sub ProcessTags()
 Dim TagsToShow As String
 Dim myPara As Paragraph
 Dim myTag As String
 Dim i As Integer
 Dim hidePara As Boolean
 TagsToShow = UCase(InputBox("Type tags to make visible. Leave blank to show all", "Single Source"))
 ActiveWindow.View.ShowHiddenText = True
 ActiveDocument.Range.Font.Hidden = False
    For Each myPara In ActiveDocument.Paragraphs
    myTag = TagPara(myPara, "")
        If myTag <> "" Then hidePara = False
    For i = 1 To Len(TagsToShow)
         If InStr(myTag, Mid(TagsToShow, i, 1)) = 0 Then hidePara = True
    Next i
    If hidePara Then
    myPara.Range.Font.Hidden = True
    End If
    Next myPara
     ActiveWindow.View.ShowHiddenText = False
     ActiveWindow.ActivePane.View.ShowAll = False
 Set myPara = Nothing
End Sub

Function TagPara(myPara As Paragraph, myTag As String) As String
 Dim pos1 As Integer
 Dim pos2 As Integer
 Dim myText As String
 Dim myRange As Range
 Set myRange = myPara.Range
 myText = myRange.Text
 pos1 = InStr(myText, "{")
 pos2 = InStr(myText, "}")
 If pos1 > 0 And pos2 > pos1 Then
 myRange.SetRange Start:=myPara.Range.Start + pos1 - 1, End:=myPara.Range.Start + pos2
 Else
 myRange.MoveEnd unit:=wdCharacter, Count:=-1
myRange.Collapse direction:=wdCollapseEnd
 End If
 If myTag <> "" Then myRange.Text = "{" & myTag & "}"
 myText = myRange.Text
 If Len(myText) > 1 Then TagPara = Mid(myText, 2, Len(myText) - 2) Else
TagPara = ""
 myRange.Font.Hidden = True
 Set myRange = Nothing
End Function


Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I like macros, but not at this level :-)--I'm a writer, not a programmer. I need simpler. I need to swap SAE/metric bits of text within a paragraph, as well as images and even callouts and their corresponding arrows. For two years I kept separate SAE/metric documents, until yesterday I realized Hey: why not just create two character styles called Metric Text and SAE Text, apply those styles to all the respective numbers (include the preceding space of text, not the trailing space, b/c Word will automatically drop the trailing space of a style application), and then simply apply or remove the Hidden property of the style? Applying the styles was easy after assigning each to a hotkey (Ctrl+Alt+M for Metric, Ctrl+Alt+N for Not :-) ). I wrote a short macro that turns the Hidden property on/off in those two styles, and assigned the macro to a button in a custom toolbar: one for SAE, one for Metric. I also wrote a tiny macro to toggle Show/Hide Text, because though I normally like to see the hidden text (gotta proof that too), having two graphics appearing where there should only be one really messes up pagination, plus moves them out from under their callouts. Here's the VB code for changing the Hidden property and toggling hidden text; the styles are up to you. It's a time-saving, graceful, low-code, one-click solution to conditional text. Take that, Adobe! :-)

Sub Metric()
    With ActiveDocument.Styles("Metric Text").Font
        .Hidden = False
    End With
    With ActiveDocument.Styles("SAE text").Font
        .Hidden = True
    End With
End Sub

Sub ToggleHiddenText()
    If ActiveWindow.View.ShowHiddenText = True Then
        ActiveWindow.View.ShowHiddenText = False
    Else: ActiveWindow.View.ShowHiddenText = True
    End If
End Sub

Eric Muench, Technical Writer Extraordinaire, 7/17/2015

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 15, 2024 Views 1,705 Applies to: