2017-03-13 61 views
0

我目前正在使用Excel搜索基於起始和結束字符的可識別字符串的整個單詞文檔。中間的字符會有所不同。起始字符將始終爲< <,結尾的字符總是>>。示例< <要刪除的文本>>。我試圖找到所有這些實例並刪除它們,包括< < >>字符。下面是我目前的代碼,它可以刪除連續文本,但是當我嘗試添加星號來指定<和<之間的任何內容時,它不再有效。查找基於開始/結束字符變量內部字符在Word文檔中的字符串並替換

我相信它不承認*爲'任何',但我不知道如何更新以允許。任何幫助表示讚賞!

Sub AddRemoveWatermark(blWatermarkAction As Boolean, blDeleteText As Boolean, strInitialIdentifier As String, strEndingIdentifier As String) 
    'Word Variables 
    Dim wrdApplication As Word.Application 
    Dim wrdDocument As Word.Document 
    Dim wrdSection As Word.section 
    Dim strPath As String 
    Dim lngCount As Long 

    ' Open the file dialog 
    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = True 
     .Show 

     Set wrdApplication = New Word.Application 

     ' Display paths of each file selected 
     For lngCount = 1 To .SelectedItems.Count 
      strPath = .SelectedItems(lngCount) 
      Set wrdDocument = wrdApplication.Documents.Open(strPath) 

      wrdApplication.Visible = True 

      'Delete all starting << and ending >> 
      With wrdDocument.Range.Find 
       .ClearFormatting 
       .Text = strInitialIdentifier & "*" & strEndingIdentifier 
       .Forward = True 
       .Format = False 
       .MatchCase = False 
       .MatchWholeWord = False 
       '.MatchWildcards = True 
       .MatchSoundsLike = False 
       .MatchAllWordForms = False 
       .Replacement.ClearFormatting 
       .Replacement.Text = "" 
       .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 
      End With 
     Next lngCount 
    End With 
End Sub 
+0

我不知道這個問題是否應該註明的副本[這個問題](http://stackoverflow.com/q/7408485/6535336) – YowE3K

+0

如果我讀了其他問題/回答正確,它的聲音就像你可能想要一串'[\\ <]{2}[!\>] @ [\>] {2}'。 – YowE3K

+0

我試過,作爲一個字符串,它不匹配或找到任何東西。我閱讀並解釋了你引用的文章,看起來你會根據他們的邏輯是正確的,但它沒有返回任何與.Text爲「[\ <]{2}[!\>] @ [\>] {2}」的例子。「 – Allen

回答

1

下面的代碼成功地從一個Excel工作簿中運行:

Sub AddRemoveWatermark(blWatermarkAction As Boolean, blDeleteText As Boolean, strInitialIdentifier As String, strEndingIdentifier As String) 
    'Word Variables 
    Dim wrdApplication As Word.Application 
    Dim wrdDocument As Word.Document 
    Dim wrdSection As Word.section 
    Dim strPath As String 
    Dim lngCount As Long 

    ' Open the file dialog 
    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = True 
     .Show 

     Set wrdApplication = New Word.Application 

     ' Display paths of each file selected 
     For lngCount = 1 To .SelectedItems.Count 
      strPath = .SelectedItems(lngCount) 
      Set wrdDocument = wrdApplication.Documents.Open(strPath) 

      wrdApplication.Visible = True 

      'Delete all starting << and ending >> 
      With wrdDocument.Range.Find 
       .ClearFormatting 
       .Text = "[\<]{2}[!\>]@[\>]{2}" 
       .Forward = True 
       .Format = False 
       .MatchCase = False 
       .MatchWholeWord = False 
       .MatchWildcards = True 
       .MatchSoundsLike = False 
       .MatchAllWordForms = False 
       .Replacement.ClearFormatting 
       .Replacement.Text = "" 
       .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 
      End With 
     Next lngCount 
    End With 
End Sub 

FileDialog我選擇它包含一個段落說Sdaf saf <<agfjgadf>> fdsdfsd一個Word文檔,它改變了字符串是Sdaf saf fdsdfsd

注意:當前編寫的代碼會使編輯的文檔保持打開狀態並且未保存。我不確定這是否是有意的。

+0

我很高興你持久@ YowE3K,因爲在我發佈這個問題之前,我一定會搞砸的,因爲你是對的。我已將其重置爲該狀態,並且效果很好。根據您的其他評論,我確實刪除了結束代碼以關閉並保存以簡化發佈,但您在我的實施中正是這樣做的。謝謝!!! – Allen

相關問題