alanadams20
New Member
- Joined
- Nov 13, 2013
- Messages
- 2
Hi everyone,
I'm new to VBA and I'm looking to write a macro that calls a JavaScript function via Internet Explorer because I'm also lazy.
My work computer is networked to my phone so I'm able to dial phone numbers by clicking a link on an internal webpage that calls a JavaScript function and dials the number associated with that link. What I'm trying to do is call that function directly in the URL bar of IE but pass in a phone number from a spreadsheet so I can highlight the number in excel and execute the macro to automatically dial the number.
Here's the code I have so far:
Public Sub CallNumber()
Dim Col As Long, Row As Long
Dim rawNum As String
Dim JavaString As String
Dim ie As Object
Row = ActiveCell.Row
Col = 4 'column with phone number
rawNum = Cells(Row, Col).Value
num = Strip(rawNum) 'strips formatting
JavaString = "javascript:CallNumber('81" & num & "');"
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.Visible = True
ie.NAVIGATE JavaString
When I call the macro it opens IE but then for some reason, instead of calling the JavaScript function, it opens the downloads window so it seems to be interpreting the call as a file to be downloaded.
I also tried navigating to our internal webpage before calling execScript (with a hard-coded phone number for testing):
ie.NAVIGATE "isis"
Do Until ie.readystate = 4
DoEvents
Loop
ie.document.parentWindow.execScript "CallNumber('81xxxxxxxxxx')"
This produced a run-time error with the following message:
"Method 'Document' of object 'IWebBrowser2' failed"
Any thoughts on how to get this working?
Thanks!
I'm new to VBA and I'm looking to write a macro that calls a JavaScript function via Internet Explorer because I'm also lazy.
My work computer is networked to my phone so I'm able to dial phone numbers by clicking a link on an internal webpage that calls a JavaScript function and dials the number associated with that link. What I'm trying to do is call that function directly in the URL bar of IE but pass in a phone number from a spreadsheet so I can highlight the number in excel and execute the macro to automatically dial the number.
Here's the code I have so far:
Public Sub CallNumber()
Dim Col As Long, Row As Long
Dim rawNum As String
Dim JavaString As String
Dim ie As Object
Row = ActiveCell.Row
Col = 4 'column with phone number
rawNum = Cells(Row, Col).Value
num = Strip(rawNum) 'strips formatting
JavaString = "javascript:CallNumber('81" & num & "');"
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.Visible = True
ie.NAVIGATE JavaString
When I call the macro it opens IE but then for some reason, instead of calling the JavaScript function, it opens the downloads window so it seems to be interpreting the call as a file to be downloaded.
I also tried navigating to our internal webpage before calling execScript (with a hard-coded phone number for testing):
ie.NAVIGATE "isis"
Do Until ie.readystate = 4
DoEvents
Loop
ie.document.parentWindow.execScript "CallNumber('81xxxxxxxxxx')"
This produced a run-time error with the following message:
"Method 'Document' of object 'IWebBrowser2' failed"
Any thoughts on how to get this working?
Thanks!