PDA

View Full Version : Solved: Hyperlink in a Userform



phendrena
03-17-2009, 08:54 AM
Hello,

A very quick question.

How do I add a hyperlink to a userform, for example via a command button?

Thanks,

Bob Phillips
03-17-2009, 09:28 AM
ActiveWorkbook.FollowHyperlink Address:="http://example.microsoft.com", _
NewWindow:=True

phendrena
03-18-2009, 01:21 AM
Thanks xld.

On a related note, is it possible to populate a web form automatically from VBA? For example, the web page opens using the above code and then the two fields on the web page that is opened are populated by items from the excel userforms text boxes?

phendrena
03-18-2009, 01:47 AM
I have come across the following thread : http://www.vbaexpress.com/forum/showthread.php?t=21429

It has the code in post #2 which i've modified to suit :


Dim ieDoc As Object



Dim sws As SHDocVw.ShellWindows

Dim strURL As String
Dim n As Integer
'Set main URL to evaluate open IE windows
strURL = "http://www.covernotes.rsagroup.co.uk/covernotes/covernotes.nsf/main?openframeset&ITSX-7Q8ME9"
Set sws = New SHDocVw.ShellWindows
'Cycle through all open IE windows and assign the window whose URL matches strURL
For n = 0 To sws.Count - 1
If Left(sws.Item(n).LocationURL, Len(strURL)) = strURL Then
Set ieDoc = sws.Item(n).Document
sws.Item(n).Visible = True
Exit For
End If
Next n
ieDoc.all("username").Value = Me.txtUserID.Value
ieDoc.all("password").Value = Me.txtPassword.Value
ieDoc.all("Log In").Click







I've added the reference to internet controls, amended the code to suite the correct URL and fields, that is assuming the 'ieDoc.all....' refers to the fields on the webpage.


The code errors 'Object variable or With block variable not set' and highlights : ieDoc.all("formfield").Value = Me.txtUserID.Value



Any suggestions?

phendrena
03-18-2009, 02:04 AM
Ok, i sorted it code as follows :-

Private Sub lblFordLink_Click()
Dim ieDoc As Object
Dim sws As SHDocVw.ShellWindows
Dim strURL As String
Dim n As Integer
ActiveWorkbook.FollowHyperlink Address:="http://www.covernotes.rsagroup.co.uk/covernotes/covernotes.nsf/main?openframeset&ITSX-7Q8ME9", NewWindow:=True
'Set main URL to evaluate open IE windows
strURL = "http://www.covernotes.rsagroup.co.uk/covernotes/covernotes.nsf/main?openframeset&ITSX-7Q8ME9"
Set sws = New SHDocVw.ShellWindows
'Cycle through all open IE windows and assign the window whose URL matches strURL
For n = 0 To sws.Count - 1
If Left(sws.Item(n).LocationURL, Len(strURL)) = strURL Then
Set ieDoc = sws.Item(n).Document
sws.Item(n).Visible = True
Exit For
End If
Next n
ieDoc.all("username").Value = Me.txtUserID.Value
ieDoc.all("password").Value = Me.txtPassword.Value
'ieDoc.all("Log In").Click <=== ????

lblFordLink.ForeColor = RGB(50, 50, 125)
lblFordLink.Font.Bold = True
lblLRMILink.ForeColor = RGB(0, 0, 255)
lblLRMILink.Font.Bold = False
lblVCILink.ForeColor = RGB(0, 0, 255)
lblVCILink.Font.Bold = False
End Sub

The only thing i can't work out is how to click the Log In button as it doesn't have a defined name

<INPUT type=submit value="Log In">
Any suggestions?

Thanks,