我的VBA中有以下行:
Selection.GoTo What:=wdGoToComment, Which:=wdGoToNext, Count:=1, Name:=Last_chosen
Last_chosen是取自userform下拉列表的作者名称,debug.print显示它正确读入宏。
问题是该查找工作正常几次,然后由于某种原因它找到了属于不同作者的注释。即使Last_chosen作者在文档中有更多评论。一旦发生这种情况,它会继续发现错误的评论,有时来自多位作者,即使Last_chosen没有从最初的要求作者改变。
我试图在评论中隐藏一些作者的评论显示标记|审核人员在Word定义作者时出现故障(?),但VBA有时仍会发现错误的评论,这表明没有故障。
谢谢大家。
这是所有代码。
Public Last_chosen As String
Public Form_chosen As Integer
'****************
Sub Next_chosen_comment()
'This is where the user first specifies a new author
'to search for their next comment
Dim Re_peat As String
'Dim Cho_sen As String
Re_peat = "N"
'Cho_sen = ""
Last_chosen = ""
Call Next_chosen(Re_peat, Last_chosen)
End Sub
'****************
Sub Repeat_search_next()
'This is where the user repeats the same search
'i.e. jumps to the next comment of the same author
'without having to choose again from the dropdown
'via the Next_chosen_comment() macro
Dim Re_peat As String
'Dim Cho_sen As String
Debug.Print Last_chosen
Re_peat = "Y"
'Cho_sen = Last_chosen
'Call Next_chosen(Re_peat, Cho_sen)
Call Next_chosen(Re_peat, Last_chosen)
Re_peat = "N"
End Sub
'****************
Sub Next_chosen(Repeat_nxt As String, Last_chosen As String)
If ActiveDocument.Comments.Count < 1 Then
MsgBox "There are no comments.", vbOKOnly, "********NO COMMENTS********"
Exit Sub
End If
Debug.Print "Repeat_nxt: " & Repeat_nxt
If Repeat_nxt = "Y" Then ' If this is a repeat search (called from Repeat_search_next() macro)
GoTo Repeat_next
End If
Comment_dropdown.Show 'Not a repeat search
'so show the userform containing the 8 dropdown values
If Form_chosen = -1 Then 'Cancelled userform
Exit Sub
End If
Select Case Form_chosen 'Set the author to look for
Case 0
Chosen = "Contractions"
Case 1
Chosen = log_words"
Case 2
Chosen = "US_to_UK"
Case 3
Chosen = "Other"
Case 4
Chosen = "Spaces"
Case 5
Chosen = "Ampersand"
Case 6
Chosen = "Duplicate"
Case 7
Chosen = "Style"
End Select
Last_chosen = Chosen 'Sets Last_chosen from the dropdown
'in case user wants to subsequently repeat the same find
'using the Repeat_search_next() macro
Repeat_next:
Debug.Print "Last_chosen: " & Last_chosen
Selection.GoTo What:=wdGoToComment, Which:=wdGoToNext, Count:=1, Name:=Last_chosen
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
End Sub
'*****************Userform****************
Private Sub UserForm_Initialize()
With Drop_down
.AddItem "Contractions"
.AddItem "-log words"
.AddItem "US to UK changes"
.AddItem "Other changes"
.AddItem "Multiple spaces"
.AddItem "Ampersands"
.AddItem "Duplicate paragraphs"
.AddItem "Non-RFP styles"
End With
End Sub
'****************
Private Sub OK_btn_Click()
Form_chosen = Drop_down.ListIndex
Unload Me
End Sub
'****************
Private Sub Cancel_btn_Click()
Form_chosen = -1
Unload Me
End Sub
答案 0 :(得分:0)
在第一次检查您的代码时,我发现了以下缺陷。
Last_chosen
,它与私有变量Cho_sen
和Chosen
保持一致。我没有找到这些混淆的地方,但我没有找到这样的观点,我会消除这两个私有变量并仅与公众变量合作。您可以节省大量代码并消除任何混淆的可能性。Repeat
的私有变量。 &#34;重复&#34;是保留使用VBA的关键词。选择&#34;重复&#34;并按F1以获取更多信息。同样,我怀疑这是你的问题的根源,但使用保留字作为变量名可能会导致意外的,看似无关的,通常无法识别的错误。最好选择其他名称我现在怀疑另一种情况。您正在评论中寻找某些单词。如果您正在寻找的单词在另一位作者的评论文本中找到,会发生什么?换句话说,您可能需要使用代码来确保找到的匹配实际上代表作者的名称。
请修正以上两点并评论我的怀疑。如果此后错误仍然存在,我会将您的代码破解成更小的部分。 : - )
同时,我建议您在代码中实现以下简化。首先,创建一个像这样的函数: -
Function DD_Items(Optional ByVal Idx As Integer = -1) As Variant
Dim Fun As Variant ' function return value
Dim Items As String
Items = "Contractions,-log words,US to UK changes,Other changes,Multiple spaces," & _
"Ampersands,Duplicate paragraphs,Non-RFP styles"
Fun = Split(Items, ",")
If Idx >= 0 Then
If Idx > UBound(Fun) Then Idx = 0 ' mistaken Idx
Fun = Fun(Idx)
End If
DD_Items = Fun
End Function
当您在没有参数的情况下调用此函数时,它将返回一个数组,否则返回参数指定的数组元素。使用此功能加载下拉列表。
Private Sub UserForm_Initialize()
With Drop_down
.List = DD_Items
.ListIndex = 0
End With
End Sub
最后,使用此代码替换Select语句。
Last_Chosen = DD_Items(Form_Chosen)