超链接与文件存在

huangapple go评论65阅读模式
英文:

Hyperlink with file exist

问题

我正试图在文本框中查看名为文件的PDF文档,并添加一个按钮来查看此文件。

当PDF文件在路径中时,它会打开。
当它不在那里时,当我点击按钮时什么也不会发生。

如果找不到PDF文件,我需要添加一个消息框弹出。

我添加了文件存在的功能。我不知道这是否是正确的方法,或者是否有一种更简单的方法在文件不存在时添加消息框到超链接中。

我的代码问题在于:
如果文件存在,它会正常打开,但会显示消息框"未找到文档"。
如果文件不存在,它也会显示消息框"未找到文档"。

Sub viewdoc()

On Error Resume Next

Dim mypath As String
Dim filename As String

filename = frmDELEGATION.txtLedger.Value

mypath = "E:-2022\" & filename & ".pdf"

ThisWorkbook.FollowHyperlink mypath

If Not FileExists(mypath, filename) Then
    MsgBox "未找到文档。"
    Exit Sub
End If

End Sub


Function FileExists(ByVal mypath As String, ByVal filename As String) As Boolean

    FileExists = (Dir(mypath & filename & ".pdf") <> "")
    
End Function
英文:

I am trying to view a pdf document with name of file in a textbox and a button to view this file.

When the pdf file is in the path it opens.
When it is not there when I click the button nothing happens.

I need to add a msgbox to pop out if the pdf file is not found.

I added File exist function. I don't know if it is the correct method or if there is an easier way to add a msgbox to hyperlink if file is not there.

The problem with my code is that:
If the file is there, it opens normally but shows the msgbox "No Documents Found."
If the file is not there it shows the msgbox "No Documents Found." as well.

Sub viewdoc()

On Error Resume Next

Dim mypath As String
Dim filename As String

filename = frmDELEGATION.txtLedger.Value

mypath = &quot;E:-2022\&quot; &amp; filename &amp; &quot;.pdf&quot;

ThisWorkbook.FollowHyperlink mypath

If Not FileExists(mypath, filename) Then
    MsgBox &quot;No Documents Found .&quot;
    Exit Sub
End If

End Sub


Function FileExists(ByVal mypath As String, ByVal filename As String) As Boolean

    FileExists = (Dir(mypath &amp; filename &amp; &quot;.pdf&quot;) &lt;&gt; &quot;&quot;)
    
End Function

答案1

得分: 2

Option Explicit

Sub ViewDoc1() ' 改进,例如'ViewLedger'!
    ViewDocument "E:-2022", frmDELEGATION.txtLedger.Value
End Sub

Sub ViewDocument( _
        ByVal FolderPath As String, _
        ByVal FileBaseName As String, _
        Optional ByVal FileExtension As String = ".pdf")
    
    Const PROC_TITLE As String = "查看文档"

    If Right(FolderPath, 1) <> Application.PathSeparator Then
        FolderPath = FolderPath & Application.PathSeparator
    End If
    
    If Len(Dir(FolderPath, vbDirectory)) = 0 Then
        MsgBox "路径 " & FolderPath & " 不存在。", _
            vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    If Left(FileExtension, 1) <> "." Then
        FileExtension = "." & FileExtension
    End If
    
    Dim fName As String: fName = FileBaseName & FileExtension
    Dim fPath As String: fPath = FolderPath & fName
 
    If Len(Dir(fPath)) = 0 Then
        MsgBox "文件 " & fName & " 未在文件夹 " _
            & FolderPath & " 中找到。", vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    On Error Resume Next
        ThisWorkbook.FollowHyperlink fPath
    On Error GoTo 0

End Sub
英文:

View Document

<!-- language: lang-vb -->

Option Explicit

Sub ViewDoc1() &#39; improve e.g. &#39;ViewLedger&#39;!
    ViewDocument &quot;E:-2022&quot;, frmDELEGATION.txtLedger.Value
End Sub

Sub ViewDocument( _
        ByVal FolderPath As String, _
        ByVal FileBaseName As String, _
        Optional ByVal FileExtension As String = &quot;.pdf&quot;)
    
    Const PROC_TITLE As String = &quot;View Document&quot;

    If Right(FolderPath, 1) &lt;&gt; Application.PathSeparator Then
        FolderPath = FolderPath &amp; Application.PathSeparator
    End If
    
    If Len(Dir(FolderPath, vbDirectory)) = 0 Then
        MsgBox &quot;The path &quot;&quot;&quot; &amp; FolderPath &amp; &quot;&quot;&quot; doesn&#39;t exist.&quot;, _
            vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    If Left(FileExtension, 1) &lt;&gt; &quot;.&quot; Then
        FileExtension = &quot;.&quot; &amp; FileExtension
    End If
    
    Dim fName As String: fName = FileBaseName &amp; FileExtension
    Dim fPath As String: fPath = FolderPath &amp; fName
 
    If Len(Dir(fPath)) = 0 Then
        MsgBox &quot;The file &quot;&quot;&quot; &amp; fName &amp; &quot;&quot;&quot; was not found in the folder &quot;&quot;&quot; _
            &amp; FolderPath &amp; &quot;&quot;&quot;.&quot;, vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    On Error Resume Next
        ThisWorkbook.FollowHyperlink fPath
    On Error GoTo 0

End Sub

huangapple
  • 本文由 发表于 2023年5月22日 08:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定