2012-11-20 27 views
8

Tôi đã tạo macro MS Word để tìm kiếm văn bản nhất định (được chỉ định bằng mã đánh dấu), cắt văn bản và chèn nó vào chú thích mới, sau đó xóa mã đánh dấu chú thích. Bây giờ tôi muốn macro lặp lại cho đến khi nó không tìm thấy bất kỳ mã đánh dấu nào khác trong văn bản.
Đây là vĩ mô bên dướiLặp lại Microsoft Word VBA cho đến khi không có kết quả tìm kiếm nào được tìm thấy

Sub SearchFN() 

'find a footnote 
Selection.Find.ClearFormatting 
With Selection.Find 
    .Text = "&&FB:*&&FE" 
    .Replacement.Text = "" 
    .Forward = True 
    .Wrap = wdFindContinue 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchKashida = False 
    .MatchDiacritics = False 
    .MatchAlefHamza = False 
    .MatchControl = False 
    .MatchByte = False 
    .MatchAllWordForms = False 
    .MatchSoundsLike = False 
    .MatchFuzzy = False 
    .MatchWildcards = True 
End With 
Selection.Find.Execute 

'cut the footnote from the text 
Selection.Cut 

'create a proper Word footnote 
With Selection 
    With .FootnoteOptions 
     .Location = wdBottomOfPage 
     .NumberingRule = wdRestartContinuous 
     .StartingNumber = 1 
     .NumberStyle = wdNoteNumberStyleArabic 
    End With 
    .Footnotes.Add Range:=Selection.Range, Reference:="" 
End With 

'now paste the text into the footnote 
Selection.Paste 

'go to the beginning of the newly created footnote 
'and find/delete the code for the start of the note (&&FB:) 
    Selection.Find.ClearFormatting 
Selection.Find.Replacement.ClearFormatting 
With Selection.Find 
    .Text = "&&FB:" 
    .Replacement.Text = "" 
    .Forward = False 
    .Wrap = wdFindContinue 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchKashida = False 
    .MatchDiacritics = False 
    .MatchAlefHamza = False 
    .MatchControl = False 
    .MatchByte = False 
    .MatchAllWordForms = False 
    .MatchSoundsLike = False 
    .MatchFuzzy = False 
    .MatchWildcards = True 
End With 
Selection.Find.Execute 
With Selection 
    If .Find.Forward = True Then 
     .Collapse Direction:=wdCollapseStart 
    Else 
     .Collapse Direction:=wdCollapseEnd 
    End If 
    .Find.Execute Replace:=wdReplaceOne 
    If .Find.Forward = True Then 
     .Collapse Direction:=wdCollapseEnd 
    Else 
     .Collapse Direction:=wdCollapseStart 
    End If 
    .Find.Execute 
End With 

'do same for ending code (&&FE) 
With Selection.Find 
    .Text = "&&FE" 
    .Replacement.Text = "" 
    .Forward = True 
    .Wrap = wdFindContinue 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchKashida = False 
    .MatchDiacritics = False 
    .MatchAlefHamza = False 
    .MatchControl = False 
    .MatchByte = False 
    .MatchAllWordForms = False 
    .MatchSoundsLike = False 
    .MatchFuzzy = False 
    .MatchWildcards = True 
End With 
Selection.Find.Execute 
With Selection 
    If .Find.Forward = True Then 
     .Collapse Direction:=wdCollapseStart 
    Else 
     .Collapse Direction:=wdCollapseEnd 
    End If 
    .Find.Execute Replace:=wdReplaceOne 
    If .Find.Forward = True Then 
     .Collapse Direction:=wdCollapseEnd 
    Else 
     .Collapse Direction:=wdCollapseStart 
    End If 
    .Find.Execute 
End With 

Selection.HomeKey Unit:=wdStory 
'now repeat--but how??  

End Sub 

Trả lời

11

Tốt câu hỏi thế này, bạn có thể lặp thông qua toàn bộ tài liệu sử dụng Selection.Find.Found kết quả.

Việc bạn làm là bắt đầu tìm kiếm và nếu bạn tìm thấy kết quả chỉ đi vào vòng lặp trong khi kết quả Selection.Find.Found là đúng. Một khi bạn đã có thông qua những điều này, bạn đã hoàn tất. Các mã sau đây nên làm các trick độc đáo cho bạn.

Sub SearchFN() 
    Dim iCount As Integer 

    'Always start at the top of the document 
    Selection.HomeKey Unit:=wdStory 

    'find a footnote to kick it off 
    With Selection.Find 
     .ClearFormatting 
     .Text = "&&FB:*&&FE" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchKashida = False 
     .MatchDiacritics = False 
     .MatchAlefHamza = False 
     .MatchControl = False 
     .MatchByte = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchFuzzy = False 
     .MatchWildcards = True 
     .Execute 
    End With 

    'If we find one then we can set off a loop to keep checking 
    'I always put a counter in to avoid endless loops for one reason or another 
    Do While Selection.Find.Found = True And iCount < 1000 
     iCount = iCount + 1 

     'Jump back to the start of the document. Since you remove the 
     'footnote place holder this won't pick up old results 
     Selection.HomeKey Unit:=wdStory 
     Selection.Find.Execute 

     'On the last loop you'll not find a result so check here 
     If Selection.Find.Found Then 

      ''================================== 
      '' Do your footnote magic here 
      ''================================== 

      'Reset the find parameters 
      With Selection.Find 
       .ClearFormatting 
       .Text = "&&FB:*&&FE" 
       .Replacement.Text = "" 
       .Forward = True 
       .Wrap = wdFindContinue 
       .Format = False 
       .MatchCase = False 
       .MatchWholeWord = False 
       .MatchKashida = False 
       .MatchDiacritics = False 
       .MatchAlefHamza = False 
       .MatchControl = False 
       .MatchByte = False 
       .MatchAllWordForms = False 
       .MatchSoundsLike = False 
       .MatchFuzzy = False 
       .MatchWildcards = True 
      End With 
     End If 
    Loop 
End Sub 
Các vấn đề liên quan