2017-05-04 93 views
0

希望有人可以提供幫助。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 
+0

因此,Word將它的頭部認爲「Last_Chosen」是一個存在但未被選擇的名稱,並且一旦確信未選擇被選中就會堅持該定罪。這種行爲有兩種可能的原因。一,Word有一個錯誤,大意是評論的「Name」屬性與錯誤的評論相關聯。這看起來不太可能,因爲評論是一個集合,任何混合集合的「名稱」屬性也會在其他地方顯現。 – Variatus

+0

另一個可能的原因是,'Last_Chosen'變量如何分配其值。這種可能性需要檢查你的整個代碼。我說「全」,因爲你已經看到「無處不在」。因此,假設這樣的錯誤隱藏在你不期望找到的地方是安全的。現在,如果你更願意認爲第一個原因是原因,我們無法幫助你。如果您想考慮第二種可能性,請通過將您的代碼添加到您的問題來啓用我們的幫助。 – Variatus

+0

@ Variatus - 感謝您的建議 - 我將很快發佈代碼 – hmm

回答

0

初次檢查您的代碼時,我發現以下缺陷。

  1. 你有一個公共變量Last_chosen這是保持與私有變量Cho_senChosen對齊。我沒有找到這些被混淆的地方,但並不是尋找這樣一個觀點,我會消除這兩個私人變量,只與公衆一起工作。您可以節省大量代碼並消除任何混淆的機會。
  2. 您有一個名爲Repeat的私有變量。 「重複」是爲使用VBA保留的關鍵詞。選擇「重複」並按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) 
+0

感謝您的幫助和建議。我做了一些更改: – hmm

+0

您不需要將'Last_Chosen'作爲參數傳遞給您的子例程。由於它是一個'Public'變量,它在該模塊中的任何地方都可用。 – Variatus

+0

對不起,我被電話打斷了。很明顯,如果你有一個參數'Last_Chosen',並且一個名稱相同的公共變量VBA應該被允許反抗。消除參數,並使用子中的'Last_chosen'變量。 – Variatus