简体   繁体   English

如果包含VBA的各种短语,如何删除ms-word文档?

[英]How to delete a ms-word document if it contains various phrases with VBA?

I am very new to VBA and I am aware this contains errors. 我是VBA的新手,我知道其中包含错误。

In MS-Word developer, I would like to go up one directory, search all *.docx files for multiple strings, if any of the strings exist within the *docx file, I would like to delete the file. 在MS-Word开发人员中,我想上一个目录,在所有* .docx文件中搜索多个字符串,如果* docx文件内存在任何字符串,我想删除该文件。

Sub loopFiles()

Dim fso As New FileSystemObject
Dim fil As File
Dim fold As Folder
Dim yourfolder As String
Dim UpOneDir As String

Set fold = fso.GetFolder(Application.ActiveWorkbook.Path)

For Each fil In fold.Files

UpOneDir = Left(fil.Path, Len(fil.Path) - Len(fil.Name) - 1 - Len(Split(Left(fil.Path, Len(fil.Path) - Len(fil.Name) - 1), "\")(UBound(Split(Left(fil.Path, Len(fil.Path) - Len(fil.Name) - 1), "\")))))

Sub FindIt()
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Text = "Aircraft Survey","Cc:","UTAS","Inserted in the word document is a pdf file"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
            Do While .Execute

            If InStr(1, fil.Text, "Aircraft Survey","Cc:","UTAS","Inserted in the word document is a pdf file") > 0 Then
                Application.Workbook.Open fil.Path
                ActiveWorkbook.Close

            fil.Delete True
        End If
    Loop
End With
End Sub
End Sub

Here's how I would do it. 这就是我要做的。 You might want to test this by commenting out the oFile.Delete and replace it with a MsgBox oFile.Name so it doesn't just delete files. 您可能需要通过注释掉oFile.Delete并将其替换为MsgBox oFile.Name以便它不仅删除文件。 Once you're happy that it's definitely finding the files you require, use the delete command. 一旦满意地找到了所需的文件,请使用delete命令。

Option Explicit


Sub loopFiles()
    Dim oFso: Set oFso = CreateObject("Scripting.FileSystemObject") ' create FileSystemObject
    Dim oFolder: Set oFolder = oFso.GetFolder(oFso.GetParentFolderName(ThisDocument.Path))    ' Set folder to the Parent of the folder containing this file
    Dim oFile, oDocument, textVal, bFound
    Dim TextValues: TextValues = Array("Aircraft Survey", "Cc:", "UTAS", "Inserted in the word document is a pdf file") ' create array of terms to check

    For Each oFile In oFolder.Files
        If Right(oFile.Name, 5) = ".docx" Then  ' if file is a *.docx
            Set oDocument = Documents.Open(oFile.Path)  ' Open it
            bFound = False                              ' Initialise
            For Each textVal In TextValues  ' check for each text value
                With oDocument.Content.Find
                    .Text = textVal
                    .Forward = True
                    .Execute
                    If .Found = True Then       ' if found then set bFound
                        bFound = True
                        Exit For                ' and exit the loop, no need to keep looking
                    End If
                End With
            Next
            oDocument.Close wdDoNotSaveChanges  ' close file and don't save any changes
            If bFound = True Then               ' If the text was found in the file
                oFile.Delete                    ' delete the file
            End If
        End If
    Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MS Word / VBA-如何遍历文档并查找2个字符串以删除它们之间的所有内容? - MS Word / VBA - how can I loop through the document and look for 2 strings to remove everything in between them? 在熊猫中,如何从单词列表或单词集中选择数据框中的短语? - In pandas, how to select phrases in a dataframe from word list or word set? 如何提取特定单词后有字数限制的短语? - How to extract phrases with word limit after specific Word? vbscript中最快的方法,检查字符串是否包含许多单词/短语列表中的单词/短语 - Fastest Way in vbscript to Check if a String Contains a Word/Phrase from a List of Many Words/Phrases 代码性能,使用VBA写入Word文档 - Code performance, writing into Word document using VBA 如何使用VBA将MS Word中的部分文本移到右侧? - How I can move part of text in MS Word with VBA to right side? 如何使用Adobe XI Pro删除基于PDF短语的页面? - How to delete pages based on phrases in PDF using Adobe XI Pro? Excel VBA - 删除最多* word *的字符串内容 - Excel VBA - delete string content up to *word* 如何检查字符串是否包含单词? - How to check if a string contains a word? MS Word VBA,突出显示字符串首次出现的代码 - MS Word VBA, code to highlight first occurrence of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM