How Can I Open a Web Browser and Link to Site

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcole
    New Member
    • Feb 2020
    • 2

    How Can I Open a Web Browser and Link to Site

    Can I use VBA or a macro to open a web browser and link to a web site. The hyperlink is in a table.
    Last edited by NeoPa; Feb 13 '20, 08:52 AM. Reason: Tidied up the English.
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    How to open IE
    Add the following to the "reference"
    Microsoft HTML Object Library
    Microsoft Internet Controls
    Code:
    Dim objIE As InternetExplorer
    Dim URL As String
    Set objIE = CreateObject ("Internetexplorer.Application")
    objIE.Visible = True
    URL = "https://your_url"
    objIE.navigate URL

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32656

      #3
      Hi SioSio.

      We're fast getting to the stage where IE will no longer be available as ubiquitously as it was previously. There are various other browsers available now, and one (Edge) is even from Microsoft. Do you have the CreateObject() code for any of these others?

      That would certainly enhance your answer.

      Comment

      • GazMathias
        Recognized Expert New Member
        • Oct 2008
        • 228

        #4
        Alternative

        Hi,

        Here is one solution to the unknown browser problem.

        Windows knows what your default browser is, so you simply use a batch file to open the url.

        The command in the batch file is:

        Code:
        start %1
        You then call that batch file with the Shell command.

        Code:
        Private Sub Example(ByVal strUrl as String)
          Shell "c:\url.bat " & strUrl
        End Sub
        Where c:\url.bat is the path to your actual batch file.

        This is equivalent to typing a URL into Start -> Run. This approach can be used to start anything, really.

        It could be possible to accomplish the whole thing from the Shell call but I have not researched that in all honesty.

        You do, however, introduce the need to distribute that batch file with your solution or you can make your solution create it. You could check that batch file exists and create it if it doesn't in your URL calling function or on intital startup if it is likely to be a high frequency use case.

        A potential downside to this approach is that beyond the initial load your solution no longer has any control over the browser so you can not automate it if that is your goal. The chromium engine is the way to go in that case, you wouldn't believe how much modern software is just chromium with a wrapper.


        Gaz

        Comment

        • rollerbladegirl
          New Member
          • Jan 2020
          • 69

          #5
          It looks like you are using VBA, which reminds me that in the past I programmed in VB6(sp5). I looked and I have a couple of links that you might find useful.


          Huge amount of VB6(sp5) on that site that you might find useful for VBA.


          And another page that I had saved years ago, and now find that it is a broken link. Does not open for me on the internet. It is on the site
          http://www.vbforums.com .

          If you want me to show the code here I can. It is a long list with a lot of code examples.

          I think that this site could use it for VBA references. I did not write it, and please note that I gave a partial reference that I did not manage at this time to get to open.

          Examples:

          Navigating to a site
          Popup browser using your own form
          Check if word/string is found on the page
          Making page on startup
          Regular Browser Functions
          Advanced browser functions
          Changing web browser’s Font Size
          Disabling functions appropriately (Back/Forward)
          Disabling functions appropriately (page setup/print preview/print setup)
          Removing Right Click Menu From the browser control
          Grab all links on the page
          Save Page
          Open Page
          Auto Submit
          Using A ProgressBar With The Webbrowser
          Setting a Control in a Webbrowser to focus
          Checkbox in a page, how to control it
          Custom Right Click Menu

          There is a lot there.

          Maybe some of that may help if the site editors would like me to post that code here.

          It does not show on that list but VB6(sp5) can hook and subclass FireFox tabs even though there is only one (1) windows handle to the entire FireFox program. I have seen it done, but you might have to program that one in yourself: catch the new tab as it is an instance before it is added to the single overall one handle then subclass that instance and Firefox in general does not know that anything is happening. Then the web pages can be opened with custom adjustments internal to them, and inside of VB6(sp5). All with VB6(sp5). The thing is, if this can be done with VB6(sp5) then it probably can be done with VBA with some adjustments.

          Comment

          • SioSio
            Contributor
            • Dec 2019
            • 272

            #6
            Hi NeoPa.
            In CreateObject(), Edge doesn't seem to be selectable.

            There is also a way to use Selenium VBA.
            Seleniumbasic : A Selenium based browser automation framework for VB.Net, VBA and VBScript


            After downloading Selenium,
            add "Selenium Type Library" in the reference settings.

            It supports the following WebDriver.
            EdgeDriver
            ChromeDriver
            FirefoxDriver
            OperaDriver
            PhantomJSDriver

            The new ChromeDriver must be obtained from the following site.


            Code tested in Chrome
            Code:
            Dim driver As New ChromeDriver
            driver.Get "http: // your_url"
            driver.Quit
            Last edited by NeoPa; Feb 16 '20, 11:01 AM. Reason: Fixed [CODE] tags.

            Comment

            • dcole
              New Member
              • Feb 2020
              • 2

              #7
              Thank you SioSio

              but im trying to use a link or web address that is in a table that was entered be a user in a form. is there any other way i can do this with out making the URL Static in the code

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32656

                #8
                Yes, there certainly is. We're not here simply to do it for you though. You need to jump in with something to show you're not simply using us to get your work done for you. We've seen very little from you so far.

                I can tell you that SioSio's code passes a simple string to the driver.Get() call. That string is simple to create and requires only that your web address be appended to the string "https://" in order to work (or http://" if the site doesn't handle secure browsing - some still don't).

                That should be simple enough for you. If not then at least have a go at it and show us what you've tried and what the attempt resulted in.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32656

                  #9
                  Originally posted by GazMathias
                  GazMathias:
                  Here is one solution to the unknown browser problem.
                  Hi Gaz.

                  Nice solution, but hard to interface into a table containing the data. Not impossible, but certainly hard.

                  I love the idea of the BAT (or CMD) file but to get that to be flexible and powerful by linking it to a table and thus creating the file on the fly is a lot more complicated than you may imagine. I know. I've done it. I've even given presentations on how to do it with some example code. Most struggled to get to grips even with most of the code already written and ready to use.

                  I'm happy to share it but not happy to explain it using the written word when it appears so hard to grasp even when presented with the finished article.

                  It's an idea I like, but don't believe really has legs in a situation such as this. Great to throw in ideas though, of course.

                  Comment

                  • SioSio
                    Contributor
                    • Dec 2019
                    • 272

                    #10
                    Access the record set URL value field and set its value to the browser URL.

                    Comment

                    • GazMathias
                      Recognized Expert New Member
                      • Oct 2008
                      • 228

                      #11
                      NeoPa:
                      Nice solution, but hard to interface into a table containing the data. Not impossible, but certainly hard.
                      You only need to pass the field data to the function, which takes in a string. The URL is passed to Start via the %1 argument in the batch file. No file creation needed.

                      NeoPa:
                      linking it to a table and thus creating the file on the fly is a lot more complicated than you may imagine
                      It is not necessary to do that to get this working.

                      By creating files on the fly I am referring to initialisation of dependencies the solution needs in first run wizards or startup tests.

                      Apologies, when I post I assume that these posts will be read by many other people long down the road with similar goals arriving via Google, et al.

                      @dcole
                      Native Access 'Hyperlink' fields open your default browser when you click them in forms and datasheets by default, assuming your source table is Access and you have the ability to refactor it.

                      Gaz

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32656

                        #12
                        Hi Gaz.

                        I must have rushed this without considering it fully. You're right of course. An elegant solution as it allows the system to select the browser based on your selected preferences.

                        I wasn't thinking past the idea of creating a new Batch/CMD file to run every time but your ealier post already illustrated the parameter approach.

                        Comment

                        • GazMathias
                          Recognized Expert New Member
                          • Oct 2008
                          • 228

                          #13
                          Hi NeoPa,

                          Thanks, I admit that it is only a half solution in that the browser no longer has any connection to the Access instance that called it so no further automation can be accomplished and as alluded to in my prior post, Hyperlink fields can accomplish this natively.

                          For me, the usefulness in this approach is that it demonstrates one method of reaching outside of the limitations of the Access runtime without resorting to far more complicated measures, such as writing COM visible wrapper DLLs. One such example is using Shell to call an FTP powershell / python script to send a file you just had Access create.

                          Gaz

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32656

                            #14
                            To be fair that part was always above and beyond the requirements stated in the OP. Now I better understand the suggestion it would probably be my preference. What is good though is that both approaches have been covered. SioSio went with handling it within the code and you went with the command file one.

                            All-in-all a good, helpful thread to have available for searchers :-)

                            Comment

                            • SioSio
                              Contributor
                              • Dec 2019
                              • 272

                              #15
                              Microsoft Edge started successfully using Shell object.
                              Code:
                              Dim wUrl As String
                              Dim url As String
                              url = "https://your_url"
                              wUrl = "microsoft-edge:" & url & ""
                              With CreateObject("Shell.Application")
                                  .ShellExecute wUrl
                              End With

                              Comment

                              Working...