【问题标题】:Check Word 2007 bookmark content and do something if it exists检查 Word 2007 书签内容并在存在时执行某些操作
【发布时间】:2014-05-24 19:41:11
【问题描述】:

是否可以搜索书签内的内容,如果存在,请执行操作。

例如,如果有一个带有名为 Bookmark1 的书签的 word 文档。 Bookmark1 的封闭文本是通过突出显示文本“Entered Text Goes Here”创建的。我想创建一个宏来检查书签内的文本是否已更改,如果没有更改,则删除文本、书签、之前的分节符。

下面的代码执行此操作,只是它会删除书签,即使文本不同,因为它正在查找书签的名称,而不是其内容。

If ActiveDocument.Bookmarks.Exists("Bookmark1") = True Then
    ActiveDocument.Bookmarks("Bookmark1").Select
    Selection.Delete
    With Selection
        .EndKey Unit:=wdStory
        .TypeBackspace
        .Delete
    End With
End If

我真的希望 If 语句可以这样说: 如果 Bookmark1 中的文本 = "Entered Text Goes Here" 则执行以下所有操作,否则退出。

有什么想法吗?

Word 2007。

【问题讨论】:

  • 试试If ActiveDocument.Bookmarks("Bookmark1").range = "Entered Text Goes Here" then...

标签: vba ms-word word-2007


【解决方案1】:

如果您的文档按照我的想法设置,则以下内容应该可以工作,否则您需要尝试一下:

'TestTxt is the default text in the bookmark (assuming that you are not including the paragraph mark in the bookmark)
Dim TestTxt As String: TestTxt = "Enter text here"
'DMRng is the range of the the bookmark you are looking at
Dim BMRng As Range: Set BMRng = ThisDocument.Bookmarks("Bookmark1").Range


If BMRng.Text = TestTxt Then
        'Start is the beginning of the bookmark - 1 (as the character before hand should be your section break?!)
        BMRng.SetRange Start:=BMRng.Start - 1, End:=BMRng.End
        BMRng.Delete
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2019-06-16
    • 2011-10-11
    • 2011-01-31
    • 2018-08-10
    相关资源
    最近更新 更多