VBA 中的超链接在 FilePath 中的空格处停止

Hyperlink stops at spaces in FilePath in VBA

我想在通过 VBA 生成的电子邮件中提供指向共享驱动器路径的超链接。在我的 excel 中,邮件分发列表、邮件主题和文件路径是动态的,因此 VBA 从该特定单元格中选择这些详细信息。由于 FilePath 有 'space',超链接没有占用整个路径。请在下面查看我的代码,让我知道我哪里错了-

Sub Draft_Mail()
Dim OutApp As Object
Dim OutMail As Object
Dim Filepath As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Filepath = Worksheets("Macro").Range("F7")
    
With OutMail
.To = Worksheets("Macro").Range("F9").Text
.Cc = Worksheets("Macro").Range("F11").Text
.Subject = Worksheets("Macro").Range("F13").Text
.Attachments.Add Filepath
.htmlBody = "Hello-" & "<br/>" & "<br/>" & "Please find attached Reconciliation for " & Worksheets("Macro").Range("F5").Text & ". Click on this link to open the file-  " & "<br/>" & "<br/>" & "<A href=" & Filepath & ">" & Filepath & "</A>" & "<br/>" & "<br/>" & "Regards"
.Display

End With
End Sub

href 需要在引号内,像这样...

"<A href='" & Filepath & "'>" & Filepath & "</A>"

或者,您可以用 url 编码版本 %20 替换文件路径中的任何空格,如下所示...

"<A href=" & Replace(Filepath, " ", "%20") & ">" & FilePath & "</A>"