Refresh Web Page via Window Handle - Without SendKeys

JohnPoole

Active Member
Joined
Jun 9, 2005
Messages
267
Hi all, I have some VBA which gives me the Window Handle of some Chrome Windows.... That should be all I need to send it a refresh request - handle is stored in variable hWnd - I want to avoid the F5 Sendkeys method as I don't want to lose window focus .. All I'm seeing online after multiple Google searches is to use Selenium, but even this doesn't seem to get around my actual issue of just sending a refresh directly to Chrome... An example of what I have so far:

VBA Code:
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Public Function Refresh(MyHandle)

Debug.Print "Refreshing " & MyHandle

   Dim hWnd As Long

   hWnd = MyHandle

   If hWnd > 0 Then SetForegroundWindow hWnd '''''''''''''This is the bit I want to replace...

   Application.SendKeys ("{F5}")     '''''''''''''This is the bit I want to replace...

End Function

Sub Test_Refresh()

Call Refresh(4264404)

End Sub


Is their a better solution? Or will I really need Seleniun to accomplish this?

Thanks in advance,

John
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You could always set the window focus to something else after having refreshed the page with this code. Would that still be a problem?
 
Upvote 0
Hi Dan, thanks for the reply, I had thought about doing it the way you suggested and just apply focus back to whatever window it was previously, which I may have to do, but it's not my preferred solution. If this isn't possible in normal VBA, can anyone confirm it's definitely possible with the Selenium plug in?

Thanks,

John
 
Upvote 0
Yes, it appears Selenium/WebDriver has a refresh method. There is a way of connecting with, and controlling, Chrome etc from VBA without having to install Selenium, but it's new and I haven't looked at it properly yet. I would expect that Refresh would be available there too.

of just sending a refresh directly to Chrome
It isn't as straight forward as you're making it sound. Chrome isn't really designed to take instructions from other programs, and VBA has no idea what Chrome is. If you were using Internet Explorer, or the WebBrowser control, then this would be very simple, but I assume that they are not options.

Another option might be UIAccessibility, which can be a way of interfacing with programs (both windows and controls) in much the same way as you're using the windows handle.

If you could give a bit more information about what you're doing, it might help in coming up with a solution.
 
Upvote 0
Solution
Thanks Dan. It is as simple (or hard as the case maybe) as sending the refresh to Chrome. I'll take a look at Selenium and UIAccessibility and post my answer if I get it working.
 
Upvote 0
I've installed what I believe is the latest version of Selenium, though it's still a couple of years old... The examples I've tried all seem to be failing with Excel 2019.. so i may try to do some more research time permitting. I implemented changing focus back to the current window in the meantime until I can work out a better solution. Ill post a solution if I get one, with or without Selenium.
 
Upvote 0
Hmm - that's not great.
I wrote a quick guide re: installing Selenium here - the key point is that you need to make sure that the version of ChromeDriver you're using matches the version of Chrome you're trying to use.
It is as simple (or hard as the case maybe) as sending the refresh to Chrome.
It really isn't - it should be though.
If the above doesn't work, let me know - we can look into bypassing Selenium altogether and look at controlling the webdriver directly.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top