【问题标题】:Search and open a Word file by name from the cell从单元格中按名称搜索并打开 Word 文件
【发布时间】:2018-11-12 13:01:50
【问题描述】:

我的代码可以搜索并打开与单元格中的值同名的 Word 文件(“B9”):

Sub find()
    Dim f As String, folder As String, file_name As String
    Set wordapp = CreateObject("word.Application")
    folder = ThisWorkbook.Path & "\" 'Folder
    file_name = LCase(Range("B9")) & ".docx" 'Cell with file name
    f = Dir(folder)
    While Not Len(f) = 0
        If LCase(f) = file_name Then
            wordapp.documents.Open folder & f
            wordapp.Visible = True
            Exit Sub
        End If
        f = Dir()
    Wend
End Sub

如何修改此代码,以便它获取一个值来搜索不是来自特定单元格的文件,而是检查整个列“B”,如果它没有找到具有相同单元格名称的文件,则通知没有这样的文件。

有人可以帮我解决这些问题吗?谢谢。

【问题讨论】:

标签: vba excel


【解决方案1】:

试试下面的。

Sub find()
    Dim f As String, folder As String, file_name As String

    Set wordapp = CreateObject("word.Application")
    folder = ThisWorkbook.Path & "\" 'Folder
    f = Dir(folder)
    While Not Len(f) = 0
        If Not (IsError(Application.VLookup(LCase(f), Worksheets("Sheet1").Range("B:B"), 1, True))) Then
            wordapp.documents.Open folder & f
            wordapp.Visible = True
            Exit Sub
        End If
        f = Dir()
    Wend
End Sub

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 2017-11-25
    相关资源
    最近更新 更多