使用 MS Word 宏搜索文本文件

发布于 2024-07-17 17:10:15 字数 301 浏览 3 评论 0原文

感谢您对我的上一个问题的帮助。 根据建议,我能够读入 Word 文档中的文本行。 然后我能够继续解析该行以提取文件名。

我现在尝试使用该文件名启动文件搜索。 有人对如何使用 MS Word 宏启动文本文件搜索有任何建议吗? 我需要找到该文件,将其打开,然后在文本文件中进行搜索。

任何帮助将不胜感激。 谢谢你!

Thanks for the help on my previous question. With the suggestion I was able to read in the line of text from the Word document. I was then able to proceed to parse the line to pull out the file name.

I am now trying to initiate a file search using that file name.
Does anyone have any suggestions on how to use MS Word Macro to initiate a text file search?
I will need to find the file, open it, and then search within the text file.

Any help will be appreciated.
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-07-24 17:10:16

如果您是从 Word 执行此操作,最简单的方法是在 Word 中打开文本文件并使用 Word 的搜索功能。 这样您就不需要实现自己的搜索功能。

只需录制此操作的宏:在 Word 中打开文本文件并搜索特定字符串 (Ctrl-F)。 结果看起来像这样:

Sub Macro1()

    Documents.Open FileName:="YourFile.txt", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
        Encoding:=1252

    Selection.Find.ClearFormatting

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

    Selection.Find.Execute

    If Not Find Is Nothing Then
        ' your action here '
    End If
End Sub

然后您可以编辑录制的宏以满足您的需要。

编辑:我发现我部分误解了你的问题。 我的答案仅涵盖如何在文本文件中搜索特定字符串,而不是如何在文件系统中搜索文件列表。

If you are doing it from Word, the easiest way would be to open the text file in Word and use Word's search functionality. That way you don't need to implement your own search functionality.

Just record a macro of this action: Open a text file in Word and search for a specific string (Ctrl-F). The result looks something like this:

Sub Macro1()

    Documents.Open FileName:="YourFile.txt", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
        Encoding:=1252

    Selection.Find.ClearFormatting

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

    Selection.Find.Execute

    If Not Find Is Nothing Then
        ' your action here '
    End If
End Sub

You can then edit the recorded macro to suit your needs.

Edit: I see that I partly misunderstood your question. My answer only covers how to search a text file for a specific string, not how to search the file system for a list of files.

嗼ふ静 2024-07-24 17:10:15

要进行搜索,请使用 Application.FileSearch:

FileSearch 对象

阅读时,使用 FileSystemObject

搜索应该从那里开始,InStr 就是一个简单的问题。

To search, use Application.FileSearch:

FileSearch Object

Reading, use FileSystemObject

Searching should be a simple matter of InStr from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文