[英]VBA in Word: If then to toggle bookmarks hidden/unhidden
This VBA code seems like it should work to toggle hidden = True/ False on bookmarks in Word, but it doesn't.这段 VBA 代码似乎应该可以在 Word 中的书签上切换 hidden = True/False,但事实并非如此。
Private Sub Instructions_Click()
If ActiveDocument.Bookmarks("InstText").Range.Font.Hidden = True Then
Bookmarks("InstText").Range.Font.Hidden = False
Else
Bookmarks("InstText").Range.Font.Hidden = True
End If
End Sub
This next one works (if you click the Instructions
button, the appropriate bookmarked text is unhidden)下一个有效(如果您单击“
Instructions
”按钮,相应的书签文本将不隐藏)
Private Sub Instructions_Click()
ActiveDocument.Bookmarks("InstText").Range.Font.Hidden = False
End Sub
Another idea I saw is this one:我看到的另一个想法是:
Private Sub Instructions_Click()
ActiveDocument.Bookmarks("InstText").Range.Font.Hidden = Not ActiveDocument.Bookmarks("InstText").Range.Font.Hidden
Which is supposed to flip it to whatever it's not, basically.基本上应该将其翻转为不是的任何东西。 But that doesn't work for me.
但这对我不起作用。
Can anyone tell me what's wrong with the first one?谁能告诉我第一个有什么问题? I'm guessing that the hidden property can't be used as a condition for some reason, but -- why not?
我猜隐藏属性出于某种原因不能用作条件,但是——为什么不呢? Or is it something else?
或者是别的什么? What's the best way to do this?
最好的方法是什么?
Add this code to Normal > NewMacros:将此代码添加到 Normal > NewMacros:
Sub Toggle_Bookmarks()
ActiveWindow.View.ShowBookmarks = Not ActiveWindow.View.ShowBookmarks
End Sub
Now you have a toggle in the quick Access toolbar to click on to toggle bookmarks.现在您在快速访问工具栏中有一个切换按钮,可以单击以切换书签。
This doesn't answer the question, but it is some insight for people using his question for their own problem.这并没有回答问题,但对于将他的问题用于他们自己的问题的人来说,这是一些见解。
For some reason, the if statement doesn't compute.Range.Font.Hidden = False.出于某种原因,if 语句不计算 Range.Font.Hidden = False。 It does, however, compute if you use.Range.Font.Hidden = True.
但是,如果您使用 .Range.Font.Hidden = True,它会进行计算。
So you've got to use Else: to work around it.所以你必须使用 Else: 来解决它。 Kind of a double negative situation.
有点双重消极的情况。
'SF1
If ActiveDocument.Bookmarks("SF1").Range.Font.Hidden = True Then如果 ActiveDocument.Bookmarks("SF1").Range.Font.Hidden = True 那么
Else: ActiveDocument.Bookmarks("SF1X").Range.Font.Hidden = False否则:ActiveDocument.Bookmarks("SF1X").Range.Font.Hidden = False
End If``结束如果``
In my own Word add-in, I display a list of all the bookmarks found.在我自己的 Word 加载项中,我显示了找到的所有书签的列表。 The user clicks on the bookmark (or multiple bookmarks) and click the Toggle button to switch the visibility.
用户单击书签(或多个书签)并单击切换按钮以切换可见性。
My routine loops through all the selected bookmarks and uses these two lines of code:我的例程遍历所有选定的书签并使用这两行代码:
.Bookmarks(intHideShowBookmark).Select
.Bookmarks(intHideShowBookmark).Range.Font.Hidden = Not _ .Bookmarks(intHideShowBookmark).Range.Font.Hidden
Which is using the third option you mentioned.这是使用您提到的第三个选项。 Hope that helps.
希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.