2012-04-11 80 views
2

我有偶爾有褻瀆的電視腳本,必須引起第三方的注意。我建立了一個搜索特定單詞的宏,臨時對它們進行破壞,使它們不再被重複找到,並列出它們,並列出它們在宏中發生的時間......問題:即使沒有運行它,我也知道它會只能找到第一個單詞......有時他們會說同一個單詞20次...我需要列出每個出現的時間碼和時間碼。不要替換,或突出顯示。只需列出單詞。我到目前爲止...任何幫助表示讚賞。Word宏查找褻瀆,並創建一個出現名單

 Sub Macro7() 
' 
' Macro7 Macro 
' 
' 
    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Text = "dog" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.Copy 

    ' places cursor inside the word so I can disfigure it 

    Selection.MoveLeft Unit:=wdCharacter, Count:=1 
    Selection.MoveRight Unit:=wdCharacter, Count:=1 

    ' xxx1 temporarily disfigures the word so it isn't re-found over and over 

    Selection.TypeText Text:="xxx1" 

    ' goes to end of document and pastes the word there, 
    ' to be joined by the matching timecode to be found next 

    Selection.EndKey Unit:=wdStory 
    Selection.PasteAndFormat (wdPasteDefault) 
    Selection.Find.ClearFormatting 
    ' returns to last instance of word and finds time code 
    ' immediately preceeding it 

    With Selection.Find 
     .Text = "xxx1" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.MoveLeft Unit:=wdCharacter, Count:=1 
    Selection.Find.ClearFormatting 
    With Selection.Find 

     'this is finding the time code 

     .Text = "^?^?:^?^?:^?^?:^?^?" 
     .Replacement.Text = "" 
     .Forward = False 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 

    ' copies the time code value and goes to bottom of document 
    ' to paste it with the word previously found 

    Selection.Copy 
    Selection.EndKey Unit:=wdStory 
    Selection.TypeText Text:=vbTab 
    Selection.PasteAndFormat (wdPasteDefault) 
    Selection.TypeParagraph 
    Selection.Find.ClearFormatting 

    ' returns to the word just found 

    With Selection.Find 
     .Text = "xxx1" 
     .Forward = False 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.MoveRight Unit:=wdCharacter, Count:=1 


    ' begins the process for the next word "cat" 

    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Text = "cat" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute 
    Selection.Copy 

    ' places cursor inside the word so I can disfigure it 
    ' etc etc etc 

End Sub 
+0

由於沒有人對我的問題進行過嘗試,所以我認爲這個詞不夠清楚。一個例子,它搜索的內容是:01:03:05:22等等等等等等等等等等等等等等等等等。下一行01:09:44:22等等等等等等(我用'狗'和'貓')從腳本 – CJG 2012-04-13 18:08:29

+0

講更多等等等等沒人能幫忙嗎?真? – CJG 2012-05-02 22:42:17

回答

0

如果將內容放入Excel,可能會更容易。例如,假設每個時間代碼和相關文本位於Sheet1的列A中的單個CELL中,以下宏將在指定的TARGET出現的所有時間代碼的J列中生成一個列表。可以擴展宏以查找其他目標,並將這些關聯時間代碼的列表輸出到不同的列中。

Sub FindTarget() 
    Range("C1").Select 
    ActiveCell.FormulaR1C1 = "=IF(ISERROR(IF(SEARCH(""TARGET"",RC[-2]),""TRUE"",""FALSE"")),"""",IF(SEARCH(""TARGET"",RC[-2]),""TRUE"",""FALSE""))" 
    Range("D1").Select 
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""true"",LEFT(RC[-3],8),"""")" 
    Range("C1:D1").Select 
    Selection.AutoFill Destination:=Range("C1:D9999"), Type:=xlFillDefault 
    Columns("D:D").Select 
    Selection.Copy 
    Columns("J:J").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("J1"), _ 
     SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Sheet1").Sort 
     .SetRange Range("J1:J9999") 
     .Header = xlGuess 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Columns("C:D").Select 
    Application.CutCopyMode = False 
    Selection.ClearContents 
    Range("K1").Select 
End Sub 
+0

這個宏根本沒有意義......我在C1中放置了褻瀆詞,並在C4,C7等中放了良性詞......在A1,A4,A7放置了定時碼...在你的宏中改變了TARGET這個詞,以一個匹配的褻瀆,並運行宏...它只是抹去列C中的所有內容......在J列中沒有任何內容......我需要這個工作在WORD中......無論如何......謝謝試。但第二至第四行似乎沒有做任何與我的任務有關的事情。 – CJG 2012-05-22 21:11:21

+0

@CJG爲了讓事情變得簡單,我製作了宏,這樣整個行(時間碼和相關文本)進入A列中的一個單元格。假設每個時間代碼/關聯文本由段落分隔,這就是它如何直接從Word中粘貼到Excel中。我更新了我的答案以使其更清晰。 – subcortical 2012-05-23 00:42:50

+0

雖然仔細檢查了宏,但我意識到find()函數區分大小寫。將其更改爲不區分大小寫的search()。 – subcortical 2012-05-23 01:04:58

0

這些單詞必須留在文檔中嗎?還是可以將它們複製/粘貼到新的單詞文檔中?

+0

單詞可以複製/粘貼到新文檔中... – CJG 2012-05-22 20:38:14