在Word Doc&amp ;;中查找基于具有可变内部字符的开始/结束字符的字符串更换

时间:2017-03-13 20:40:24

标签: excel-vba ms-word find vba excel

我目前正在使用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 = 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

1 个答案:

答案 0 :(得分: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我选择了一个Word文档,其中包含一个段落Sdaf saf <<agfjgadf>> fdsdfsd,并将字符串更改为Sdaf saf fdsdfsd

注意:当前编写的代码会保留已编辑的文档,并保留未保存的文档。我不确定这是否有意。