英文:
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 = "E:-2022\" & filename & ".pdf"
ThisWorkbook.FollowHyperlink mypath
If Not FileExists(mypath, filename) Then
MsgBox "No Documents Found ."
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
答案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() ' improve e.g. '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 = "View Document"
If Right(FolderPath, 1) <> Application.PathSeparator Then
FolderPath = FolderPath & Application.PathSeparator
End If
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
MsgBox "The path """ & FolderPath & """ doesn't exist.", _
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 "The file """ & fName & """ was not found in the folder """ _
& FolderPath & """.", vbCritical, PROC_TITLE
Exit Sub
End If
On Error Resume Next
ThisWorkbook.FollowHyperlink fPath
On Error GoTo 0
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论