【发布时间】:2014-08-06 23:38:15
【问题描述】:
我使用代码来提取文件路径,以便将 Excel 文档中的条目链接到其原始文件。代码工作正常,除了链接不起作用,这不是因为代码。我知道的原因是只有一种超链接方法始终有效。我知道这不是由无效字符引起的,因为我有删除指定字符并重命名文件的代码。如果我在超链接之前手动删除它们也没关系。 我想知道问题是什么,以便我可以让我的代码正常工作。
通过代码提取的文件路径: \SRV006#SRV006\Am\Master Documents\PC 2.2.11 Document For Work (DFWs)\DFWS 添加到 DFW Track\DFW 和 PO 1234567.pdf
将鼠标悬停在超链接上,将显示此路径: file:///\SRV006\ - SRV006\Am\Master Documents\PC 2.2.11 Document For Work (DFWs)\ DFWS 添加到 DFW Track\ DFW 和 PO 1234567.pdf
通过右键单击“编辑超链接”显示的文件路径: \SRV006#SRV006\Am\Master Documents\PC 2.2.11 Document For Work (DFWs)\DFWS 添加到 DFW Track\DFW 和 PO 1234567.pdf
链接复制为路径并粘贴(也在 Word 文档中测试): "\SRV006#SRV006\Am\Master Documents\PC 2.2.11 Document For Work (DFWs)\DFWS 添加到 DFW Track\DFW 和 PO 1234567.pdf"
如果在“添加超链接”对话框中添加,路径仍然不起作用: \SRV006#SRV006\Am\Master Documents\PC 2.2.11 Document For Work (DFWs)\DFWS 添加到 DFW Track\DFW 和 PO 1234567.pdf
这是唯一有效的超链接。
通过右键单击添加超链接手动超链接后有效的链接路径: DFWS%20 added%20to%20DFW%20Track\DFW%20and%20PO%201234567.pdf
'Functions that gets the FileName from the path:
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
'Function that replaces Bad Characters and renames the file.
Function Replace_Filename_Character(ByVal Path As String, _
ByVal OldChr As String, ByVal NewChr As String)
Dim FileName As String
'Input Validation
'Trailing backslash (\) is a must
If Right(Path, 1) <> "\" Then Path = Path & "\"
'Directory must exist and should not be empty.
If Len(Dir(Path)) = 0 Then
Replace_Filename_Character = "No files found."
Exit Function
'Old character and New character must not be empty or null strings.
ElseIf Trim(OldChr) = "" And OldChr <> " " Then
Replace_Filename_Character = "Invalid Old Character."
Exit Function
ElseIf Trim(NewChr) = "" And NewChr <> " " Then
Replace_Filename_Character = "Invalid New Character."
Exit Function
End If
FileName = Dir(Path & "*.*") 'Use *.xl* for Excel and *.doc for Word files
Do While FileName <> ""
Name Path & FileName As Path & Replace(FileName, OldChr, NewChr)
FileName = Dir
Loop
Replace_Filename_Character = "Ok"
End FunctionSnippet Renaming the file:
'Rename the file
Dim Ndx As Integer
Dim FName As String, strPath As String
Dim strFileName As String, strExt As String
Const BadChars = "@!$/'<|>*- — " ' put your illegal characters here
If Right$(vrtSelectedItem, 1) <> "\" And Len(vrtSelectedItem) > 0 Then
FilenameFromPath = GetFilenameFromPath(Left$(vrtSelectedItem, Len(vrtSelectedItem) - 1)) + Right$(vrtSelectedItem, 1)
End If
FName = FilenameFromPath
For Ndx = 1 To Len(BadChars)
FName = Replace$(FName, Mid$(BadChars, Ndx, 1), "_")
Next NdX
GivenLocation = _
"\\SRV006\#SRV006\Am\Master Documents\PC 2.2.11 Document For Work(DFWs) \DFWS added to DFW _
Track\" 'note the trailing backslash
OldFileName = vrtSelectedItem
NewFileName = GivenLocation & FName & strExt
strExt = ".pdf"
On Error Resume Next
Name OldFileName As NewFileName
On Error GoTo 0
Sheet7.Range("a50").Value = NewFileName
'pastes new file name into cellA UserForm looks at filepath that was extracted and uses that as
the filepath for the hyperlink, and a textbox on the UserForm as the text to display on the
hyperlink.
'UserForm Snippet that links the filepath to the the entry:
Sheet1.Hyperlinks.Add _
Anchor:=LastRow.Offset(1, 0), _
Address:=TextBox19.Value, _
TextToDisplay:=TextBox1.Value
【问题讨论】: