Option Explicit
#If VBA7 Or Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As LongPtr) As LongPtr
#Else
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If
Public Function FUNCCompleteForm()
Dim URL As String: URL = "https://www.access-programmers.co.uk/forums/members/"
Dim HTMLdoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
Dim IE As InternetExplorer: Set IE = Get_IE_Window(URL)
If IE Is Nothing Then Set IE = New SHDocVw.InternetExplorer
With IE
.Visible = True
.Navigate URL
SetForegroundWindow .hwnd
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 200: Wend
While .Document.ReadyState <> "complete": DoEvents: Sleep 200: Wend
Set HTMLdoc = .Document
End With
' YOUR CODE TO FILL WEB STUFF
For Each oHTML_Element In HTMLdoc.getElementsByTagName("input")
Select Case oHTML_Element.Name
Case Is = "username"
oHTML_Element.setAttribute "value", Range("myRange") 'THIS IS EXCEL, ADAPT TO ACCESS
End Select
Next
Set HTMLdoc = Nothing
Set IE = Nothing
SendKeys "{NUMLOCK}", True 'if it turns off your numlock, don't ask me why it does that
End Function
Private Function Get_IE_Window(URL As String) As SHDocVw.InternetExplorer
'Looks for a window of tab opened in the specified URL, can be http or https or nothing
'If it finds it, it returns the browser as an IE object, otherwise it returns nothing
Dim Domain As String
Dim Shell As Object
Dim IE As SHDocVw.InternetExplorer
Dim i As Variant 'Variant to index Shell.Windows.Item() array
Dim p1 As Integer, p2 As Integer
p1 = InStr(URL, "://")
If p1 = 0 Then
p1 = 1
Else
p1 = p1 + 3
End If
p2 = InStr(p1, URL, "/")
If p2 = 0 Then p2 = Len(URL) + 1
Domain = Mid(URL, p1, p2 - p1)
Set Shell = CreateObject("Shell.Application")
i = 0
Set Get_IE_Window = Nothing
While i < Shell.Windows.Count And Get_IE_Window Is Nothing
Set IE = Shell.Windows.Item(i)
If Not IE Is Nothing Then
'Debug.Print IE.LocationURL, IE.LocationName
'Debug.Print TypeName(IE)
If TypeOf IE Is SHDocVw.InternetExplorer And InStr(IE.LocationURL, "file://") <> 1 Then
If InStr(IE.LocationURL, Domain) > 0 Then
Set Get_IE_Window = IE
End If
End If
End If
i = i + 1
Wend
End Function