在 VBA 中打开超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40343468/
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
Open hyperlink within the VBA
提问by Mosi
I have an excel file. In the first sheet there are rows of hyperlinks (Relative files paths = files are located next to the excel file).
我有一个excel文件。在第一张表中有几行超链接(相对文件路径 = 文件位于 Excel 文件旁边)。
In the VBA, I will obtain the cell that contain hyperlinks. How can I open the file which is followed by hyperlink in the VBA? Actually I want to open the hyperlink (open the file actually)
在 VBA 中,我将获得包含超链接的单元格。如何在 VBA 中打开后跟超链接的文件?其实我想打开超链接(实际上是打开文件)
回答by Gary's Student
If the hyperlink in the worksheet is in a =hyperlink()formula we:
如果工作表中的超链接在=hyperlink()公式中,我们:
- grab the formula
- parse it to get the url
- follow the hyperlink:
- 抓住公式
- 解析它以获取url
- 按照超链接:
Here is the code:
这是代码:
Sub ClickByVba()
ary = Split(ActiveCell.Formula, Chr(34))
ActiveWorkbook.FollowHyperlink Address:=ary(1)
End Sub
This will work for both links to the web and links to files.
If the link is to a file, the file will be opened (if not already open)and the jump will be made.
这适用于网络链接和文件链接。
如果链接是指向文件,则该文件将被打开(如果尚未打开)并进行跳转。
回答by cyberponk
As I commented on Gary′s answer, the ActiveWorkbook.FollowHyperlink method works, but it will ask for your credentials every time you open the link, if your website requires a login.
正如我对 Gary 的回答所评论的,ActiveWorkbook.FollowHyperlink 方法有效,但如果您的网站需要登录,则每次打开链接时它都会要求您提供凭据。
If you wish to open a website and have your credentials saved, you can use this other function:
如果您想打开一个网站并保存您的凭据,您可以使用其他功能:
Sub OpenHyperlink(ByVal link As String)
'Escape chars that cmd.exe uses
link = Replace(link, "^", "^^")
link = Replace(link, "|", "^|")
link = Replace(link, "&", "^&")
'Open default web browser
Shell "CMD.EXE /C START " & link, vbHide
End Sub
回答by David Zemens
Have a look at the Hyperlinkobject reference:
看看Hyperlink对象引用:
https://msdn.microsoft.com/en-us/library/office/ff835563.aspx
https://msdn.microsoft.com/en-us/library/office/ff835563.aspx
You should be able to open the file with the .Followmethod, or if that doesn't work you can always grab the .Addressproperty and pass that to the Workbooks.Openmethod.
您应该能够使用该.Follow方法打开文件,或者如果这不起作用,您可以随时获取该.Address属性并将其传递给该Workbooks.Open方法。
Also bookmark the Excel Object Model Referencefor future use :)
还将Excel 对象模型参考加入书签以备将来使用 :)
I don't think this works on formula hyperlinks, though, so if that's your situation then instead, do:
不过,我认为这不适用于公式超链接,因此,如果这是您的情况,请执行以下操作:
Sub h()
Dim cl As Range
Dim h As String
Set cl = Range("E13") 'Modify to the cell containing your =HYPERLINK formula
h = Replace(cl.Formula, "=HYPERLINK(", "")
h = Replace(h, ")", "")
h = Replace(h, Chr(34), "")
h = Split(h, ",")(0)
Dim wb As Workbook
Set wb = Workbooks.Open(h)
End Sub


