Results 1 to 16 of 16

Thread: Jump to a bookmark in an html page from a VB6 form

  1. #1

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Jump to a bookmark in an html page from a VB6 form

    Hi everyone,

    I need to open an html page "C:\test.htm" by clicking a VB6 button. I use the ShellExecute API for this and it works wonderfully.
    I actually need to take this one step further though, and automatically jump to a specified bookmark in the page as well. My comon sense guess would be to provide the bookmark reference the usual way: "C:\test.htm#bmk". But that doesn't work - the page no longer even opens.

    Can this be done?

    Note that I cannot use paths to specific browsers in this (i.e. something like "C:\Program Files\Google\Chrome.exe /o C:\test.htm#bmk" - and I don't claim that this syntax is correct, it is merely an example). The application will be distributed among many different users, each with his/her own browser that can be installed anywhere.

    Thanks in advance!

    Cooz

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Jump to a bookmark in an html page from a VB6 form

    At least on my first test, ShellExecute works for me. I just put the following in Form1's click event, ran it, and clicked the form:

    Code:
    
    Option Explicit
    '
    Private Declare Function ShellExecuteW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    '
    
    Private Sub Form_Click()
        '                                                                                                                                            v <-- jumping to bookmark
        Const sUrl As String = "http://www.vbforums.com/showthread.php?868035-Jump-to-a-bookmark-in-an-html-page-from-a-VB6-form&p=5331629&viewfull=1#post5331629"
        ShellExecuteW hWnd, StrPtr("open"), StrPtr(sUrl), 0&, StrPtr("c:\\"), vbNormalFocus
    End Sub
    
    

    Cooz, can you provide a specific page that doesn't work?

    Elroy

    EDIT1: As a note, I do have Chrome specified as my default browser. But it doesn't seem that that should matter. It should be providing the URL just as it would be as an argument on the command line, which should work for whatever browser is the default.

    EDIT2: It also works if I surround the URL in quotes, which probably isn't a bad idea. Here's the code where I did that:

    Code:
    
    Option Explicit
    '
    Private Declare Function ShellExecuteW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    '
    
    Private Sub Form_Click()
        '                        v <-- embedded quote (and also closing on the end)                                                                    v <-- jumping to bookmark
        Const sUrl As String = """http://www.vbforums.com/showthread.php?868035-Jump-to-a-bookmark-in-an-html-page-from-a-VB6-form&p=5331629&viewfull=1#post5331629"""
        ShellExecuteW hWnd, StrPtr("open"), StrPtr(sUrl), 0&, StrPtr("c:\\"), vbNormalFocus
    End Sub
    
    
    Last edited by Elroy; Nov 9th, 2018 at 09:19 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    Whoa Elroy. That is one fast response.

    Yep, your code works. Your ShellExecute looks slightly different from mine (I don't use StrPtr for example - which shouldn't make any difference but who knows) so I tried your version.
    And then it doesn't seem to work when the .htm file is located on your hard drive. Can you confirm this? Internet access cannot be guaranteed for my users so we have to install the htm file on a local or perhaps network drive for them.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    I also tried surrounding the url with quotes like so:

    Chr(34) & slHtmlFile & "#" & slBmk & Chr(34)

    But that doesn't seem to help either.
    Last edited by Cooz; Nov 9th, 2018 at 09:43 AM.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Jump to a bookmark in an html page from a VB6 form

    You can always look up the program and then run it passing the document as an argument. You could just use the Shell() function or if you insist (why?) you can use ShellExecute() instead.

    Code:
    Option Explicit
    
    Private Const WIN32_NULL As Long = 0
    Private Const MAX_PATH As Long = 260
    
    Private Enum HINSTANCE
        SE_ERR_FNF = 2& 'file not found
        SE_ERR_PNF = 3& 'specified path invalid
        SE_ERR_ACCESSDENIED = 5& 'access denied
        SE_ERR_OOM = 8& 'out of memory
        SE_ERR_NOASSOC = 31& 'no file association
    End Enum
    
    Private Declare Function FindExecutable Lib "shell32" Alias "FindExecutableW" ( _
        ByVal lpFile As Long, _
        ByVal lpDirectory As Long, _
        ByVal lpResult As Long) As HINSTANCE
    
    Private Sub Form_Load()
        Dim Result As String
        Dim HINSTANCE As HINSTANCE
        Dim Pos As Long
    
        Result = Space$(MAX_PATH)
        'This must be an actual file, here we'll look at one in the current directory:
        HINSTANCE = FindExecutable(StrPtr("x.htm"), WIN32_NULL, StrPtr(Result))
        Select Case HINSTANCE
            Case Is > 32
                Pos = InStr(Result, vbNullChar)
                If Pos = 0 Then Pos = Len(Result) + 1
                Label1.Caption = Left$(Result, Pos - 1)
            Case SE_ERR_FNF
                Label1.Caption = "Not found"
            Case SE_ERR_PNF
                Label1.Caption = "Path invalid"
            Case SE_ERR_ACCESSDENIED
                Label1.Caption = "Access denied"
            Case SE_ERR_OOM
                Label1.Caption = "Out of memory"
            Case SE_ERR_NOASSOC
                Label1.Caption = "No file association"
            Case Else
                Label1.Caption = "Other error " & CStr(HINSTANCE)
        End Select
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            Label1.Move 0, 0, ScaleWidth, ScaleHeight
        End If
    End Sub
    Name:  sshot.png
Views: 624
Size:  1.6 KB

  6. #6

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    Hi dilettante,

    No, I don't insist on using ShellExecute... I just want to get the job done.
    I'm not too happy about Shell though, as it is known to have issues - and I thought I couldn't use it anyway because of the browser specification.
    But I didn't know about FindExecutable! I'll give it a try, thanks!

  7. #7

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    ----

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Jump to a bookmark in an html page from a VB6 form

    Hmmm, interesting. Cooz, you're right. The bookmarks don't seem to work locally.

    Here's the test code I put together: (Others will need to patch up the actual path, as it's on a folder on my desktop.)

    Code:
    
    Option Explicit
    '
    Private Declare Function ShellExecuteW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    '
    
    Private Sub Form_Click()
        Const sUrl As String = """file:///C:/Users/Elroy/Desktop/HtmlTest/Testing.htm#bookmark"""
        ShellExecuteW hWnd, StrPtr("open"), StrPtr(sUrl), 0&, StrPtr("c:\\"), vbNormalFocus
    End Sub
    
    

    And here's my little test html code I threw together:

    Code:
    <html>
    <head></head>
    <body>
    <a href="#bookmark">Hyperlink Bookmark Code</a> </p>
    
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    asdfasdf </p>
    
    <a id="bookmark"></a>
    
    after bookmark </p>
    after bookmark </p>
    after bookmark </p>
    after bookmark </p>
    after bookmark </p>
    
    
    </body>
    </html>

    For me, it worked, but it didn't execute the bookmark. Also, at first, it didn't work at all. However, I remembered that I had MS-Word set as my default program for opening .HTM type files, and that was the first problem. I change that to Chrome, and it started partially working. However, the bookmark still didn't work.

    I'm guessing that, in the process of figuring out what kind of file it is (i.e., html) that ShellExecute strips off the "#bookmark" from the end. But that's a guess. I'm now thinking that Dilettante's FindExecutable approach may be the best way to fix this.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Jump to a bookmark in an html page from a VB6 form

    Wow, this is a bit more of a nettlesome problem than it should be.

    Here's some code with Dil's approach incorporated into it:

    Code:
    
    Option Explicit
    '
    Private Declare Function ShellExecuteW Lib "shell32" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    Private Declare Function FindExecutable Lib "shell32" Alias "FindExecutableW" (ByVal lpFile As Long, ByVal lpDirectory As Long, ByVal lpResult As Long) As Long
    '
    
    Private Sub Form_Click()
        Const MAX_PATH      As Long = 260
        Const WIN32_NULL    As Long = 0
        Const sUrl          As String = "C:\Users\Elroy\Desktop\HtmlTest\Testing.htm#bookmark"
        '
        Dim sFile   As String
        Dim sProg   As String
        Dim idx1    As Long
        Dim idx2    As Long
        Dim h       As Long
        Dim pos     As Long
        Dim sCmd    As String
        '
        idx1 = InStrRev(sUrl, "#")
        idx2 = InStrRev(LCase$(sUrl), ".htm")
        '
        If idx1 > idx2 Then
            sFile = Left$(sUrl, idx2 + 3)
        Else
            sFile = sUrl
        End If
        '
        sProg = Space$(MAX_PATH)
        h = FindExecutable(StrPtr(sFile), WIN32_NULL, StrPtr(sProg))
        If h > 32 Then
            pos = InStr(sProg, vbNullChar)
            If pos = 0 Then pos = Len(sProg) + 1
            sProg = """" & Left$(sProg, pos - 1) & """"
            sCmd = sProg & " " & """" & sUrl & """"
    
    
            'Shell sCmd
            ShellExecuteW hWnd, StrPtr("open"), StrPtr(sCmd), 0&, StrPtr("c:\\"), vbNormalFocus
    
    
        Else
            MsgBox "error"
        End If
    End Sub
    

    As you can see, I tried both the Shell and ShellExecuteW approach. Shell "sort of" worked, but the "#" sign kept getting URL escaped as a "%23", and then the bookmark jump didn't work. I'm not sure where that URL escape was happening, but I'm guessing somewhere within Shell. Or maybe Chrome does this when reading command line arguments. I'm just not sure.

    I also tried ShellExecuteW. To test, I just plugged sProg into it, and it worked fine. In other words, there was no problem opening Chrome. However, I appended the file argument, and then nothing at all worked ... no Chrome, no html file opened, no nothing.

    So, the problem now seems to be finding a way to prevent that "#" sign from being URL escaped.

    Elroy

    EDIT1: Here's the URL I'd get in Chrome when trying Shell:
    file:///C:/Users/Elroy/Desktop/HtmlTest/Testing.htm%23bookmark

    After I got to Chrome, I could replace the %23 with # and then it'd work.
    Last edited by Elroy; Nov 9th, 2018 at 11:30 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Jump to a bookmark in an html page from a VB6 form

    Hmmm, I got ShellExecuteW to work correctly:

    Code:
    
    Option Explicit
    '
    Private Declare Function ShellExecuteW Lib "shell32" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    Private Declare Function FindExecutable Lib "shell32" Alias "FindExecutableW" (ByVal lpFile As Long, ByVal lpDirectory As Long, ByVal lpResult As Long) As Long
    '
    
    Private Sub Form_Click()
        Const MAX_PATH      As Long = 260
        Const WIN32_NULL    As Long = 0
        Const sUrl          As String = "C:\Users\Elroy\Desktop\HtmlTest\Testing.htm#bookmark"
        '
        Dim sFile   As String
        Dim sProg   As String
        Dim idx1    As Long
        Dim idx2    As Long
        Dim h       As Long
        Dim pos     As Long
        '
        idx1 = InStrRev(sUrl, "#")
        idx2 = InStrRev(LCase$(sUrl), ".htm")
        '
        If idx1 > idx2 Then
            sFile = Left$(sUrl, idx2 + 3)
        Else
            sFile = sUrl
        End If
        '
        sProg = Space$(MAX_PATH)
        h = FindExecutable(StrPtr(sFile), WIN32_NULL, StrPtr(sProg))
        If h > 32 Then
            pos = InStr(sProg, vbNullChar)
            If pos = 0 Then pos = Len(sProg) + 1
    
    
            sProg = """" & Left$(sProg, pos - 1) & """"
            sFile = """" & sUrl & """"
            ShellExecuteW hWnd, StrPtr("open"), StrPtr(sProg), StrPtr(sFile), StrPtr("c:\\"), vbNormalFocus
    
    
        Else
            MsgBox "error"
        End If
    End Sub
    
    
    

    I wasn't putting the file into the correct ShellExecuteW argument.

    However, I've still got the same problem with the "#" being escaped to "%23" which prevents the bookmark jump.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Jump to a bookmark in an html page from a VB6 form

    Does the same thing happen if another browser is associated with the extension (.htm, .html)? I wonder whether Chrome or Shell32 is escaping it thinking you have query parameters rather than a page fragment marker.

    I suppose you could try passing the string to a dummy program that just displays or logs its command line arguments. Seems very unlikely to me that either the ShellExecute() or a VB6 Shell() call would play these sorts of reindeer games.


    Peeking in the registry it appears that Chrome wants something like:

    Code:
    "{chrome path}\Chrome.exe" -- "url"
    Last edited by dilettante; Nov 9th, 2018 at 08:16 PM.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Jump to a bookmark in an html page from a VB6 form

    Hmm, yes, I tend to agree, but I think Chrome is likely to be an extremely popular browser, one that Cooz could expect to run into.

    I tried all three of the following variations...

    Code:
    
            sFile = """" & sUrl & """"
    
            or
    
            sFile = " -- " & """" & sUrl & """"
    
            or
    
            sFile = "-- " & """" & sUrl & """"
    

    ...and they were all treated exactly the same way by Chrome, failing with an escaped "#". I'll let Cooz or others play around with setting other browsers as their default.

    However, here's something else that's weird. I pasted the following as the URL in a new Internet Shortcut, and then double-clicked it...

    http://www.vbforums.com/showthread.php?866129-constituent-controls-vs-owner-drawn-ActiveX-controls&p=5316863&viewfull=1#post5316863

    ...and it worked perfectly. No escaped "#", it just worked. With that success, I created a shortcut with the following URL, and double-clicked it...

    file:///C:/Users/Elroy/Desktop/HtmlTest/Testing.htm#bookmark

    ...and this time, it failed, showing the following URL in the Chrome address bar...

    file:///C:/Users/Elroy/Desktop/HtmlTest/Testing.htm

    ...This time, it didn't show an escaped "#". It just stripped it, and showed the requested file from the top.

    So, apparently, Chrome has a problem with jumping to bookmarks that are in a local file. I've got no idea what the work-around would be.

    Cooz, Good Luck,
    Elroy
    Last edited by Elroy; Nov 10th, 2018 at 11:03 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    Elroy, dilettante,

    Too bad it apparently can't be done - I guess I'm stuck with supplying one file per topic then and that's a bummer. Or maybe - just maybe - I can get through that internet access is required.

    Thanks for all the effort you put into this!

    Kind regards,
    Cooz

  14. #14
    Addicted Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    205

    Re: Jump to a bookmark in an html page from a VB6 form

    Fyi: Jumping to bookmarks works fine for local files with Firefox and WWW_OpenURL

  15. #15
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Jump to a bookmark in an html page from a VB6 form

    If these are just simple static HTML pages you can use the HTML Help Workshop to package them all up into a CHM file and distribute that with your app. Then you can navigate to all the bookmarks you want inside those pages.

  16. #16

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    The Netherlands
    Posts
    42

    Re: Jump to a bookmark in an html page from a VB6 form

    Quote Originally Posted by OptionBase1 View Post
    If these are just simple static HTML pages you can use the HTML Help Workshop to package them all up into a CHM file and distribute that with your app. Then you can navigate to all the bookmarks you want inside those pages.
    Well, that may be an option then! I'll look into it, thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width