admin管理员组文章数量:1122832
I have a small vab script that opens a new tab in firefox for a specific site and inputs some data there. I have to do this over and over and noticed that each second time the tab is opened, the focus got lost - and it is really exactly each second time.
I wonder if this is a problem with vba or with firefox and what could be done to avoid this. For the moment I just use one more vba line to set the focus to the newly opened tab, but I do not understand where the issue occurs in the first place.
You can test this with the following vba code:
Sub Test_Firefox()
'Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -safe-mode"
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
'Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
End Sub
Opening the tab once or three times, the focus is in the search of google, but opening it two times the focus is lost and I can not tell where it is.
I tried firefox in safe mode, different firefox versions (ESR and release) and two laptops (but all with Microsoft 365).
I have a small vab script that opens a new tab in firefox for a specific site and inputs some data there. I have to do this over and over and noticed that each second time the tab is opened, the focus got lost - and it is really exactly each second time.
I wonder if this is a problem with vba or with firefox and what could be done to avoid this. For the moment I just use one more vba line to set the focus to the newly opened tab, but I do not understand where the issue occurs in the first place.
You can test this with the following vba code:
Sub Test_Firefox()
'Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -safe-mode"
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
'Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
End Sub
Opening the tab once or three times, the focus is in the search of google, but opening it two times the focus is lost and I can not tell where it is.
I tried firefox in safe mode, different firefox versions (ESR and release) and two laptops (but all with Microsoft 365).
Share Improve this question asked Nov 21, 2024 at 20:28 user38913user38913 11 silver badge1 Answer
Reset to default 1For me it happens with the 4th one. And I believe it is not a VBA issue. It is more with how Firefox handles it.
Try giving Firefox sometime to handle the commands. Something like this. This will put the foucs on the Google Search Box everytime.
Option Explicit
Sub Test_Firefox()
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Wait 1 '<~~ Wait for 1 second
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Wait 1
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Wait 1
Shell """C:\Program Files\Mozilla Firefox\firefox.exe"" -new-tab www.google.de"
Wait 1
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
I would also recommend not using this method. Use the Selenium WebDriver for Firefox. That is more reliable. One more thing... Your code would fail if the user installed Firefox using Microsoft Store and not online/offline installer. In such a case there will be no FireFox.Exe
at C:\Program Files\Mozilla Firefox
.
本文标签: excelWhy is focus lost when the same tab is opend twice via vbaStack Overflow
版权声明:本文标题:excel - Why is focus lost when the same tab is opend twice via vba - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307355a1933280.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论