我是 VBA 的超级初学者,我试图创建一个宏来识别否定单词/短语关键字并在 Word 文档中对其进行评论。我在 Stackoverflow 上找到了一个 VBA 脚本,并将其关键字替换为我自己的关键字,效果很好。然而,在某些时候,它不再让我添加更多关键字(我认为它不再让我在 100 之后输入)。有人可以看一下这个并给我一个关于如何继续添加更多关键字的解决方案吗?你会做一些类似创建一个新的
Dim (keywords2) As String
并以某种方式循环遍历两者的事情吗?预先感谢!
这是代码:
Option Explicit
Sub FindNegativeWordPhraseMacro()
Dim myDoc As Document
Dim targetRange As Range
Set myDoc = ActiveDocument
Set targetRange = Selection.Range
'--- drop a bookmark to return the cursor to it's original location
Const RETURN_BM = "OrigCursorLoc"
myDoc.Bookmarks.Add Name:=RETURN_BM, Range:=Selection.Range
'--- if nothing is selected, then search the whole document
If Selection.Start = Selection.End Then
Selection.Start = 0
targetRange.Start = 0
targetRange.End = myDoc.Range.End
End If
'--- build list of keywords to search
Dim keywords() As String
keywords = Split("basket case,binge,bipolar,black mark,black sheep,blackball,blacklist,blackmail,blind eye,blind spot,blindsided,boy,brainstorm,brave,brown bag,buffoonery,bugger,bum,bury the hatchet,cakewalk,call a spade a spade,cannibal,cat got your tongue,chief,chink,circle the wagons,climb the totem pole,colored people,crazy,cretin,crippled,drink the Kool-Aid,dumb,eenie meenie miney mo,eskimo,fallen on deaf ears,freeholder,fuzzy wuzzy,gangster,geronimo,ghetto,gip,grandfather clause,grandfathered,guru,gyp,gypped,handicapped,hip hip hooray,hold down the fort,homeless,homo,homosexual,hooligan,hysteria,illegal alien,indian giver,indian summer,inner city,lame,long time no see,low man on the totem pole,man hour,mankind,master,moron,mumbo jumbo,nazi,ninja,nitty gritty,no can do,off the reservation,on the warpath,paddy,peace pipe,peanut gallery,picnic,powwow,rain dance,red-headed stepchild,rule of thumb,sanity check,savage,scalp,sell someone down the river,shaman,sherpa,slave,sold down", ",", , vbTextCompare)
'--- search for all keywords within the user selected range
Dim i As Long
For i = 0 To UBound(keywords)
'--- set the cursor back to the beginning of the
' originally selected range
Selection.GoTo What:=wdGoToBookmark, Name:=RETURN_BM
Do
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = keywords(i)
.Execute
If .Found Then
If (Selection.Start < targetRange.End) Then
Selection.Comments.Add Selection.Range, _
Text:="Please consider using a different word or phrase as this may have a negative connotation."
Else
Exit Do
End If
Else
Exit Do
End If
End With
Loop
Next i
'--- set the cursor back to the beginning of the
' originally selected range
Selection.GoTo What:=wdGoToBookmark, Name:=RETURN_BM
End Sub
我还没有尝试做任何事情。
达到了 VBE 中代码行的最大字符数限制。
请尝试一下。
sKey1
应以逗号结尾。
'--- build list of keywords to search
Dim keywords() As String
Dim sKey1 As String, sKey2 As String
sKey1 = "one,two,three,"
sKey2 = "basket case,binge,bipolar,black mark,black sheep,blackball,blacklist,blackmail,blind eye,blind spot,blindsided,boy,brainstorm,brave,brown bag,buffoonery,bugger,bum,bury the hatchet,cakewalk,call a spade a spade,cannibal,cat got your tongue,chief,chink,circle the wagons,climb the totem pole,colored people,crazy,cretin,crippled,drink the Kool-Aid,dumb,eenie meenie miney mo,eskimo,fallen on deaf ears,freeholder,fuzzy wuzzy,gangster,geronimo,ghetto,gip,grandfather clause,grandfathered,guru,gyp,gypped,handicapped,hip hip hooray,hold down the fort,homeless,homo,homosexual,hooligan,hysteria,illegal alien,indian giver,indian summer,inner city,lame,long time no see,low man on the totem pole,man hour,mankind,master,moron,mumbo jumbo,nazi,ninja,nitty gritty,no can do,off the reservation,on the warpath,paddy,peace pipe,peanut gallery,picnic,powwow,rain dance,red-headed stepchild,rule of thumb,sanity check,savage,scalp,sell someone down the river,shaman,sherpa,slave,sold down"
keywords = Split(sKey1 & sKey2, ",", , vbTextCompare)
Debug.Print UBound(keywords)