【问题标题】:Building the correct path for followhyperlink为followhyperlink构建正确的路径
【发布时间】:2020-10-16 04:06:19
【问题描述】:

大图

  1. 浏览列表并为列表中的每个项目创建一个选项卡(工作中)
  2. 在列表中创建一个超链接,链接到关联的工作表 (Working)
  3. 在每个工作表上创建基本标题信息并超链接回索引表(工作)
  4. 为索引表中相应单元格中列出的每个参考文献插入一个按钮(工作)
  5. 为打开 pdf、doc 或 docx 文件的按钮单击添加超链接(不工作,正在进行中)

当前问题

根据按钮名称,文件将存储在 3 个目录中的 1 个目录中。虽然按钮名称是文件名的唯一部分,但文件名和扩展名可能在 doc 和 docx 之间有所不同。

我有三种按钮名称格式

F-1010
F-0400-01
928

在第一种情况下,我可以生成确切的完整文件名,因为文件都是F-1010.pdf 格式

在第二种情况下,文件名将以按钮名称开头,然后是附加文本,然后是 Word 文档扩展名的变体:F-0400-01 abc def.docF-0400-01 abc def.docx

在第三种情况下,文件名将以 OPSS 开头,有时后面是一些文本,然后是按钮名称,然后是一堆文本,并以 .pdf 结尾:OPSS 928 abc.pdfOPSS.MUNI 928 abc.pdf

我尝试在字符串中使用通配符,但这不起作用。

Sub btnClick()

Dim btnName As String
Dim FPath As String
    
    'btnName = Application.Caller
    btnName = "F-0400-01" 'assigned name for testing purposes
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = "P:\2019\1234 Folder\08. Working\Specifications\Section F" & btnName & "*.doc*"
            Else
                FPath = "P:\2019\1234 Folder\10. Construction\01. Tender\F\" & btnName & ".pdf"
            End If
        Else
            FPath = "P:\2019\1234 Folder\10. Construction\01. Tender\OPSS\OPSS*" & btnName & "*.pdf"
        End If
    
    ThisWorkbook.FollowHyperlink FPath
   
End Sub

第二种和第三种情况的错误

我阅读了this questionthis question 来了解我的位置

问题

如何正确构建路径?如何打开各种文件类型?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    找到this question,使用了Rex的函数解决方案。

    虽然转置部分给我一个类型不匹配的错误,但我在函数结束时遇到了问题。所以我修改了代码如下:

    Sub btnClick()
    
    Dim btnName As String
    Dim FPath As String
    Dim basePath As String
    Dim sLink As String
    
        basePath = "P:\2019\1234 Folder"
        'btnName = Application.Caller
        btnName = "180"
            If Left(btnName, 1) = "F" Then
                If Num_Characters_In_String(btnName, "-") = 2 Then
                    FPath = basePath & "\08. Working\Specifications\Section F\"
                Else
                    FPath = basePath & "\10. Construction\01. Tender\F\"
                End If
            Else
                FPath = basePath & "\10. Construction\01. Tender\OPSS\"
            End If
        sLink = fileName(FPath, "*" & btnName & "*", ".*")
        ThisWorkbook.FollowHyperlink sLink
       
    End Sub
    

    以及修改后的文件名sub如​​下:

    Function fileName(path As String, sName As String, ext As String) As String
    
    'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
    'sName is the string to search. ? and * are wildcards.  ? is for single char
    'example sName = "book?" or sName ="March_*_2014*"
    'ext is file extention ie .pdf .xlsm .xls? .j*
    
    Dim file As Variant 'Store the next result of Dir
    Dim fname() As String 'Dynamic Array for result set
    ReDim fname(0 To 0)
    Dim i As Integer ' Counter
    i = 0
    
    ' Use dir to search and store first result
    fname(i) = path & Dir(path & "\" & sName & ext)
    i = i + 1
    
    'Load next result
    file = Dir
    
    While file <> "" 'While a file is found store that file in the array
      ReDim Preserve fname(0 To i) As String
      fname(i) = path & file
      file = Dir
    Wend
    
    'fileName = Application.Transpose(fname) 'Print out array
    fileName = fname(0)
    End Function
    

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多