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