VBA macro to call JavaScript function

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!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
After adding the "Microsoft Internet Controls" library to my VBA references (if you don't you'll get an error when calling "Set ie = New InternetExplorerMedium"), I got this to work using the following code in place of the last code block above:

'launch intranet page and wait for IE to load
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.NAVIGATE "isis"
While ie.Busy
DoEvents
Wend

'execute JS function in address bar
SendKeys "%d"
SendKeys JavaString
SendKeys "{enter}"
Application.Wait (Now + TimeValue("00:00:03"))

'close and unload ie
ie.Quit
Set ie = Nothing
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,469
Members
452,516
Latest member
archcalx

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