插入文件路径作为超链接 Excel VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40260657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:25:13  来源:igfitidea点击:

Insert filepath as hyperlink Excel VBA

excelvbaexcel-vbahyperlink

提问by tjb1

I have a VBA set of code that generates an email and automatically sends it.

我有一组生成电子邮件并自动发送的 VBA 代码。

One of the things that I cannot get to work properly is putting a hyperlink to a specified folder location within the email.

我无法正常工作的一件事是在电子邮件中放置一个指向指定文件夹位置的超链接。

Dim fpath As String

fpath = Worksheets("MS_JRNL_OPEN_TU_FR-4333635").Range("AD5").Value

"file://" & fpath & _

Essentially the user has to input a folder location when running the Macro which is in Cell AD5, but I want this is appear as the full folder location as a hyperlink once the email is generated.

本质上,用户在运行 Cell AD5 中的宏时必须输入文件夹位置,但我希望在生成电子邮件后将其显示为完整文件夹位置作为超链接。

Any help would be greatly appreciated

任何帮助将不胜感激

回答by tjb1

If you are currently using HTMLBody in your email code, it's quite easy to do. I'll assume you are using code similar to below. Take note of strbodyand .HTMLBody. Assuming your fpathis formatted like C:\Users\tjb1\Desktop\file.docxthen you don't need to add anything else to it. The section creating the hyperlink is "<A href=" & fpath & ">test link</A>". You can change test link to say whatever you want or change the line to "<A href=" & fpath & ">" & fpath & "</A>"to use the path as the link text.

如果您当前在电子邮件代码中使用 HTMLBody,则很容易做到。我假设您正在使用类似于下面的代码。注意strbody.HTMLBody。假设您fpath的格式是这样的,C:\Users\tjb1\Desktop\file.docx那么您不需要向其中添加任何其他内容。创建超链接的部分是"<A href=" & fpath & ">test link</A>"。您可以更改测试链接以说出您想要的任何内容或更改行"<A href=" & fpath & ">" & fpath & "</A>"以使用路径作为链接文本。

Sub MailURL()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    strbody = "<A href=" & fpath & ">test link</A>"
    On Error Resume Next
    With OutMail
        .To = "[email protected]"
        .Subject = "Testing URL"
        .HTMLBody = strbody
        .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

I found the code above at MrExceland just formatted it a bit to work with your variable.

我在MrExcel 上找到了上面的代码,只是稍微格式化一下以使用您的变量。