首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查AutoCorrect.Entries中是否存在条目

检查AutoCorrect.Entries中是否存在条目
EN

Stack Overflow用户
提问于 2022-06-14 14:50:12
回答 1查看 38关注 0票数 0

是否有一种有效的方法在AutoCorrect.Entries中查找MS Word中的名称,以检查它是否存在(在我用该名称添加新条目之前;我有添加该条目的代码,它可以工作,但如果它存在,则替换该条目)

代码语言:javascript
复制
Sub AutoCorrection()
'
' AutoCorrect Macro
'
'
Dim selected As Variant
Dim name As Variant

'selected text gets stored as the selected
selected = Selection

'Checking if selected text is less than 2 characters.
If Len(selected) < 2 Then
    MsgBox "Select text for autocorrect", vbOKOnly, "Nothing Selected"
    Exit Sub
End If

'Displaying the selected text and getting input for the name
name = InputBox(selected, "Name for this autocorrect?")

'*** In here, I want to check if this name exists in the entries before adding a new entry ***

AutoCorrect.Entries.AddRichText name:=name, Range:=Selection.Range
End Sub
EN

回答 1

Stack Overflow用户

发布于 2022-06-15 02:27:23

下列各点应能发挥作用。

代码语言:javascript
复制
Private Function AutoCorrectEntryExist(strName As String) As Boolean
    ' Charles Kenyon 2022-06-14 Reports True if an AutoCorrect Entry uses the name given in strName
    ' https://stackoverflow.com/questions/72619065/check-if-an-entry-exists-in-autocorrect-entries/72625298#72625298
    '
    Dim oEntry As Word.AutoCorrectEntry
    For Each oEntry In AutoCorrect.Entries
        If oEntry.Name = strName Then
            MsgBox prompt:=strName & " is already in use, Choose a different name.", title:="In Use!", buttons:=vbCritical
            AutoCorrectEntryExist = True
            GoTo EntryFound
        End If
    Next oEntry
    AutoCorrectEntryExist = False
EntryFound:
    Set oEntry = Nothing
End Function

你会用你想要使用的名字从你的过程中调用它。如果名称没有使用,它将报告False,如果使用,则报告为True。

代码语言:javascript
复制
If AutoCorrectEntryExist(name) = True Then
   ' code here to use if already used
End If

您可以跳过函数中的msgBox。如果你有很多条目,这是不会很快的。

顺便说一下,“名字”对你的变量来说是个糟糕的名字,海事组织。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72619065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档