2017-03-13 59 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

相关问题