Forum Discussion
Excel VBA and starting URL by clicking Shapes ???
I have a WorkSheet with a lot of Shapes.
Each Shape can be "Loaded" with ScreenTip and a Link by a form accesable by the user by clicking the Shape.
My users can enter ScreenTip and URL and edit them too and it works nicely !
The users can too hold SHIFT and CLICK a Shape and then the URL should be shown in the browser and it 'kind of ' work too BUT ONLY when the browser is running beforehand !
If the browser is NOT running a message is presented telling the the browser could be started in SafeMode og RESET - and noe of these alternatives are acccepable.
I use this VBA-code:
Public Sub retPARInfos(s As String)
Dim frm As UserForm, Sh As Shape
Set frm = frmRetPARInfos
Set Sh = ActiveSheet.Shapes(s)
If IsKeyPressed(eksKeyboardShift) Then ' go to the linked address
findLinkTilVielsen Sh
Else
frm.txbInfos = Sh.Hyperlink.ScreenTip
frm.txbLinkVielsen = Sh.Hyperlink.Address
frmRetPARInfos.Show
Sh.Hyperlink.ScreenTip = frm.txbInfos
Sh.Hyperlink.Address = frm.txbLinkVielsen
End If
End Sub
Public Sub findLinkTilVielsen(Sh As Shape)
If Trim(Sh.Hyperlink.Address) = "" Then
visFejlMeddelelse "No link is provided !"
Else
On Error GoTo notOK
Sh.Hyperlink.Follow NewWindow:=True
' Dim Scr_hDC As Long
' Scr_hDC = GetDesktopWindow()
' ShellExecute Scr_hDC, "Open", Trim(Sh.Hyperlink.Address), "", "", 1
GoTo goAhead
notOK: On Error GoTo 0
visFejlMeddelelse "Link is corrupt !"
goAhead:
End If
End Sub
I have tried another method (commentes out in the code) but is only gives me some of the Address back ???
What do I do wrong here ?
- I think I found the cause for starting in SafeMode - if you start FireFox (or any other browser, I think) while pressing the SHIFT-key the browser is FORCED to start in SafeMode ... that's my problem !
I have to choose another key to press when I want the users to open the link !
5 Replies
- NikolinoDEGold Contributor
As far as I could understand...
It seems like you are having trouble with opening a URL in a browser from an Excel VBA macro when the browser is not already running. One possible solution could be to use the Shell function to open the URL in the default browser.
Here’s an example:
Sub OpenURL(URL As String) Dim browserPath As String browserPath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" Shell (browserPath & " " & URL) End Sub
This code specifies the path to the Internet Explorer browser and uses the Shell function to open the URL. You can modify the browserPath variable to specify the path to a different browser if desired.
You can call this function from your findLinkTilVielsen subroutine like this:
Public Sub findLinkTilVielsen(Sh As Shape) If Trim(Sh.Hyperlink.Address) = "" Then visFejlMeddelelse "No link is provided !" Else On Error GoTo notOK OpenURL Sh.Hyperlink.Address GoTo goAhead notOK: On Error GoTo 0 visFejlMeddelelse "Link is corrupt !" goAhead: End If End Sub
This should open the URL in the default browser, even if the browser is not already running.
I hope this helps!
- keldsorBrass ContributorTHX , it seems to work nicely, BUT ...
I want to use THE DEFAULT browser and NOT Edge ... in my case I use Firefox but it should be posible to just reference 'the default browser' - rigth ?- NikolinoDEGold Contributor
If you want to open the URL in the default browser for each user, you can use the FollowHyperlink method of the Application object instead of using the Shell function.
Here’s an example:
Sub OpenURL(URL As String) Application.FollowHyperlink URL End Sub
This code uses the FollowHyperlink method to open the URL in the default browser for the current user. You can call this function from your findLinkTilVielsen subroutine like this:
Public Sub findLinkTilVielsen(Sh As Shape) If Trim(Sh.Hyperlink.Address) = "" Then visFejlMeddelelse "No link is provided !" Else On Error GoTo notOK OpenURL Sh.Hyperlink.Address GoTo goAhead notOK: On Error GoTo 0 visFejlMeddelelse "Link is corrupt !" goAhead: End If End Sub
This should open the URL in the default browser for the current user, even if the browser is not already running.
I hope this helps!
I know I don't know anything (Socrates)