PDA

View Full Version : Opening URL with Fragment Identifier from VBA Excel



HeMor
12-08-2015, 08:19 AM
By FollowHyperlink I can open a web page with a Fragement Identifier (i.e. with an ending part after a '#' sign pointing to a specific portion of the web page).
Works lika a charm.

However if I try the same thing for a file URL for example the same web page stored as a file on C:\ it does not work. The Fragment identifier is completely ignored. My default browser opens the page, but ignores the fragment identifier.

Is this a known bug/limitation?
Is there some work-around?

Here is a code snippet for illustration. My default browser (both IE and Chrome) ignores myFragment. I don't think it is passed on at all:

Option Explicit
Private Sub CommandButton1_Click()
Dim aFile As String
aFile = "C:/myFile.html#myFragment"
ActiveWorkbook.FollowHyperlink aFile, NewWindow:=True
End Sub

Kenneth Hobs
12-09-2015, 03:41 PM
Welcome to the forum!

Check that aFile exits and then add the prefix string "File://" to aFile.

HeMor
12-10-2015, 12:21 AM
Thanks for your welcome and for your suggestions! Appreciate it!

However, my problem is not to locate or to open the file in my default browser. That works fine, both with and without a 'File:" prefix (followed by three slashes).
Problem is that the fragment part (after the '#') is ignored. It does not appear in the browser. Therefore the html file opens at the very top. not at the section I want (specified by the fragment).

I'm pretty sure this is a VBA Excel problem.
- I can write the adress with a fragment manually in the browser with the desired result
- I can open the file from another scripting language (DOORS/DXL) with the desired result.
- And the fragment works fine if a call a http webpage. (html-wise identical to the file).

HeMor
12-10-2015, 03:26 AM
I managed to solve my problem by a cmd.exe detour.
Still annoying though that it does not work directly from VBA!

For the moment I haven't figured out how to use the default browser, so I explicitly calls Internet Explorer.

NOTE: The forum doesn't let me post links, so in the code below I had to replace '///' characters with a placeholder. Sorry for that.



Option Explicit
Private Sub CommandButton1_Click()
Dim cmdLine As String
cmdLine = "start iexplore.exe """File:[Three slashes]C:/myFile.html#myFragment""
Shell "cmd.exe /c " & cmdLine
End Sub

Kenneth Hobs
12-10-2015, 04:20 AM
I see now. I have not done that. Have you tried setting it in ExtraInfo?

HeMor
12-10-2015, 04:47 AM
Yes, I made some unsuccessful attempts using ExtraInfo or SubAdress. In both cases the html file opens, but ExtraInfo and SubAdress seems to be ignored.

Here is what I tried:


ActiveWorkbook.FollowHyperlink aFile, NewWindow:=True, ExtraInfo:="myFragment"

Kenneth Hobs
12-10-2015, 06:49 AM
I don't have time to play with that right now.

I would expect that a call to your web browser and then pass your string as a command line parameter value in a Shell() might work.

HeMor
12-10-2015, 06:53 AM
Thanks, yes that's what I did (see 4th post in the thread from earlier today)