希望有人可以提供幫助。Word VBA查找與特定作者的下一條評論 - 有時會發現其他作者的評論
我在VBA以下行:
Selection.GoTo What:=wdGoToComment, Which:=wdGoToNext, Count:=1, Name:=Last_chosen
Last_chosen是從下拉列表中用戶窗體採取的作者姓名和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
因此,Word將它的頭部認爲「Last_Chosen」是一個存在但未被選擇的名稱,並且一旦確信未選擇被選中就會堅持該定罪。這種行爲有兩種可能的原因。一,Word有一個錯誤,大意是評論的「Name」屬性與錯誤的評論相關聯。這看起來不太可能,因爲評論是一個集合,任何混合集合的「名稱」屬性也會在其他地方顯現。 – Variatus
另一個可能的原因是,'Last_Chosen'變量如何分配其值。這種可能性需要檢查你的整個代碼。我說「全」,因爲你已經看到「無處不在」。因此,假設這樣的錯誤隱藏在你不期望找到的地方是安全的。現在,如果你更願意認爲第一個原因是原因,我們無法幫助你。如果您想考慮第二種可能性,請通過將您的代碼添加到您的問題來啓用我們的幫助。 – Variatus
@ Variatus - 感謝您的建議 - 我將很快發佈代碼 – hmm