Log in

View Full Version : Difficult with IF Statement on a Bookmark Value



JeremyS
04-24-2013, 07:36 PM
0
In Word 97, I am trying to hide parts of my document that are in a certain font if the user enters "No" on a Dropdown Box.
I have named the bookmark associated with the Drop Down box "ScheduleKRE"
I have the following VB Macro called Dynamic, but this does not seem to work.

Sub Dynamic()
Dim KRE As Range
Set KRE = ActiveDocument.Bookmarks("ScheduleKRE").Range
If KRE = "No" Then
ActiveDocument.Unprotect
Selection.Find.ClearFormatting
Selection.Find.Font.Color = 5898330
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Hidden = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.Protect (wdAllowOnlyFormFields)
End If
End Sub
Any assistance is really appreciated

fumei
04-24-2013, 08:41 PM
"but this does not seem to work. "

That does not help much. It does not work at all? It works in parts, then errors out? If so, what was the error?

I can make a good guess, but I want you to work with this. Please describe exactly.

It looks like you are finding a font COLOR, not a font. Please clarify. The more and explicit you can be the better.

BTW: when posting code please use the VBA code tags. It makes it easier to read.

JeremyS
04-24-2013, 09:01 PM
It has an error from the start: at the second line it has: 'Compile error: Object required'.








You are right - I am looking for a color, not a font itself.

fumei
04-24-2013, 09:25 PM
That sounds like there is no formfield with a bookmark named ScheduleKRE.

Or...what kind of dropdown are you using?

If it is a formfield dropdown, then use that .Result, not its bookmark range.

macropod
04-24-2013, 10:29 PM
In Word 97, I am trying to hide parts of my document that are in a certain font if the user enters "No" on a Dropdown Box.
Trying to hide content by formatting it as hidden is a waste of time, as the display and printing of hidden text is controlled by Word settings that are entirely independent of the font attributes. If you want to have any chance, embed the variable content in an IF field that references the dropdown formfield's internal bookmark for comparison purposes and set the dropdown fomrfield's 'calculate on exit' property. An advantage of this approach is that no vba code is required.

macropod
04-24-2013, 10:41 PM
Cross-posted at: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/eded75fc-d6c5-409e-af0b-ecf3be76672f
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184