是否有一种有效的方法在AutoCorrect.Entries中查找MS Word中的名称,以检查它是否存在(在我用该名称添加新条目之前;我有添加该条目的代码,它可以工作,但如果它存在,则替换该条目)
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发布于 2022-06-15 02:27:23
下列各点应能发挥作用。
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。
If AutoCorrectEntryExist(name) = True Then
' code here to use if already used
End If您可以跳过函数中的msgBox。如果你有很多条目,这是不会很快的。
顺便说一下,“名字”对你的变量来说是个糟糕的名字,海事组织。
https://stackoverflow.com/questions/72619065
复制相似问题