如何检查超链接文件是否具有正确的文件路径

时间:2015-07-16 13:58:08

标签: vba excel-vba excel-2013 excel

我有一个程序,提示用户从文件浏览器窗口中选择一个文档,然后可以将它们重命名为他们想要的内容,它将显示为活动单元格中的超链接,单击时将打开链接文件。 然而我想限制他们可以链接到某个路径的文件(比如s:/ folder / folder / folder / fileLocation)。这是我的代码:

strFileName = Application.GetOpenFilename("*.*, All Files", , , , False)
If strFileName = "False" Then
    Exit Sub 'Code that responds when the user cancelled and does not want to add a file to the database
End If

strShortName = InputBox("What do you want to call this link?", "File Name", strFileName) 'Prompts the user to insert
                                                                                         'the name of the file
If strShortName = "" Then Exit Sub 'Attaches the entered name to document

    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=strFileName, TextToDisplay:=strShortName
    ActiveCell.Offset(1).EntireRow.Insert
                    'The above code creates a hyperlink using the entered name and connects it to the document
                    'that was first selected to insert into the database
prompt2:
    strdate = InputBox("Please enter the date that you received this file in the following format: MM/DD/YYYY .", "date received", strdate)
    If strdate = "" Then
        Selection.EntireRow.Delete
        Exit Sub 'Code that responds when the user cancelled and does not want to add a file to the database
    ElseIf Len(strdate) < 12 Then          'This code makes sure that the date is real and inserts it in the
        Cells(ActiveCell.Row, 8) = strdate 'correct row in the corresponding column
    Else
        MsgBox ("Please enter the date in the correct format.")  'Message box that informs the user to insert correct format
        GoTo prompt2                                             'and restarts the prompt for another attempt
    End If

End If


Else
MsgBox ("You cannot add a document here.") 'This handles if a user enters code into a column that does not correspond
Exit Sub

End If

我尝试过的解决方案如下:

If InStr(1, strFileName, "s:/folder/folder/folder/fileLocation") = 1 Then
MsgBox "This is a valid location to select file from"
End If

    If strFileName Like "s:/folder/folder/folder/fileLocation*"Then
    msgBox("This is a valid location to seclect file from")
    End If

这些都不起作用。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

可以尝试

If instr(1,strFileName,"s:/") >0 Then
    msgBox("This is a valid location to seclect file from")
End If

基本上如果搜索字符串(&#34; S:/&#34;)在strFileName中,那么它将返回&gt; 0

答案 1 :(得分:0)

使用/执行访问URL路径。 使用\访问本地路径。

你没错放/为\?