I want to open the google chrome and search "Christmas date" using excel VBA. The code I am using:
Sub test544()
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url chrome-extension://dehlblnjjkkbapjemjbeafjhjpjoifii/index.html")
End Sub
It opens chrome. Now I am trying to search "Christmas Date" on chrome by FindElementById and SendKeys as follows:
chromePath.FindElementById("keywords").SendKeys ("Christmas date")
chromePath.FindElementById("Button").Click
Which shows the following error
Is there any way to do that?
Instead of this :
chromePath.FindElementById("keywords").SendKeys ("Christmas date")
chromePath.FindElementById("Button").Click
do this :
chromePath.FindElementById("keywords").SendKeys "Christmas date"
chromePath.FindElementById("Button").Click
Is button a unique identifier in your application ?
Update 1 :
Dim bot As New WebDriver
bot.Start "chrome", "http://google.com"
Related
I have found the Macro for MS Word (as below) from the website https://excelchamps.com/blog/vba-code-search-google-chrome/
Sub GoogleSearch()
Dim chromePath As String
Dim search_string As String
Dim query As String
query = InputBox("Please enter the keywords", "Google Search")
search_string = query
search_string = Replace(search_string, " ", "+")
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url http://www.google.com/search?hl=en&q=" & search_string)
End Sub
It is expected that:
I press the Macro button, the InputBox pop-out, then I type keywords and it automatically opens Chrome to search those keywords.
If I press the Macro button mistakenly, then I press "Cancel" or "X" to close the Inputbox, Chrome will not automatically open.
I added if msgboxresult = "" then exit sub in the middle of the code. When I open the Inputbox and close it, Chrome doesn't open. But whatever I typed in the Inputbox, Chrome doesn't open and no search is conducted.
Does anyone know what codes should add to it in order to make it End Sub when I don't type anything and close the Inputbox?
Whilst (StrPtr(query) = 0) will indicate that the user pressed cancel it will not catch when the user has left the search term blank and clicked OK.
A better way of writing your routine is to ignore whether the user cancelled and check whether you have a search term to google. Simply checking that query isn't a zero length string before proceeding to launch chrome will catch both eventualities.
Sub GoogleSearch()
Const chromePath As String = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Dim query As String
query = InputBox("Please enter the keywords", "Google Search")
If Not query = vbNullString Then
query = Replace(query, " ", "+")
Shell (chromePath & " -url http://www.google.com/search?hl=en&q=" & query)
End If
End Sub
I am trying to do a very simple routine with SendKeys method in VBA to automate opening an app.
The problem is when I try to open a .exe ou .Ink (shortcut) file. The VBA compilation stops and the following message appears "Some files contain viruses that can be harmful to your computer...."
After that I need to select "Ok" or "Cancel" and the aplication doesn't work properly.
I researched a lot but couldn't find any solution. Are there any way to make the message not appear?
I have Windows 10 Home Single Language, Microsoft 365, Excel Version 2107.
See below the code please (The warning message appears in the line "SendKeys (strFile)"):
Sub Ligacao()
Dim strOrigem As String
Dim strExtensao As String
Dim strFile As String
Application.DisplayAlerts = False
strOrigem = ThisWorkbook.Path & "\"
strExtensao = ".lnk"
strFile = Dir(strOrigem & "*" & strExtensao)
Do While strFile <> ""
If Mid(strFile, 1, 3) = "Hat" Then
SendKeys (strFile)
End If
Loop
End Sub
I need to enter a site for which a proxy form has to be filled. I couldn't get through the form using Selenium VBA.
I called a VBS script from VBA to fill the form. It works when the PC is not locked.
Below is the proxy form.
Outlook VBA code to call VBS:
Dim bot as new chromedriver
bot.Start "chrome", "https://nissan.service-now.com/nav_to.do?uri=%2Fhome.do%3F"
bot.Get ("https://nissan.service-now.com/nav_to.do?uri=%2Fhome.do%3F") '//load webpage
strFileName = "d:\LocalData\Z018439\Desktop\MY\NX-AMO\VBACodes\Shell.vbs"
Set oshell = CreateObject("Wscript.shell")
oshell.Run "vbsc " & strFileName '//call vbs file
VBS code to fill proxy form:
Sub test()
Dim shell_object
Dim app_path, time
wscript.Sleep 12000
shell_object = "wscript.shell"
Set objshell = CreateObject(shell_object)
objshell.SendKeys ""
objshell.SendKeys "{Tab}"
objshell.SendKeys ""
objshell.SendKeys "{Enter}"
End Sub
When the PC is locked, keyboard function is disabled and hence sendkeys(VBS) is not working. Launch is done upon arrival of a new mail in Outlook.
How do I fill the proxy form when the PC is locked.
I'm trying to use Sendkeys for Ctrl+Shift+B but
.Sendkeys ("^+B")
is not working.
Try creating a Keys object:
Dim keyObj As Selenium.Keys
Set keyObj = New Selenium.Keys
bot.SendKeys keyObj.Control & keyObj.Shift & "B"
I'm using shell to open a windows explorer.
Let's say I go to Desktop and exit the windows explorer.
I would like to stock my path into a field to use later so that when I reopen my explorer it takes me back to the path I was at.
Here's what I have so far :
Dim Foldername As String
If ISNULL(Me.myfield.value) Then
Foldername = ""
Else
Foldername = ?
End If
Shell "C:\WINDOWS\explorer.exe """ & Foldername & "", vbNormalFocus
You could use something along these lines (not tested fully) this will grab the windows explorer path. Or look at using office dialogs, if you are just getting a file path perhaps...
' Using Microsoft Internet Controls Reference
Sub c()
Dim w As SHDocVw.ShellWindows
Set w = New SHDocVw.ShellWindows
For i = 0 To w.Count - 1
If w(i).Name = "Windows Explorer" Then
currentdb.Execute "delete * from [mystorage]"
currentdb.Execute "insert into [mystorage] (lastpath) values('" & w(i).LocationURL & "')"
' me.txtPrevExplorer.value=w(i).LocationURL
End If
Next i
End Sub