How do I pass a value to Internet Explorer?

  • Thread starter Carlos Chalhoub
  • Start date
C

Carlos Chalhoub

Hi listmates,

How can I send the content of a highlighted text in Ms Word (2000) to an
input box on a webpage? So far, I have been able to capture the highlighted
text (SearchWord) and fire up IE. I've researched the subject in the
newsgroup, but this question does not seem to have been asked before.

Thanks
Carloe

Here is the code:

Sub SearchInWebster()
SearchWord = Word.Selection
x% = Shell("C:\Program files\Internet Explorer\iexplore.exe
http://www.websters-online-dictionary.org/", 1)
End Sub
 
S

supersub

Hi there,
I changed my code a little bit, but I am still stuck on how to pass
"SearchWord" to IE. I went through the InternetExplorer Object Library
searching for a method or an event that would let me enter a value in
a field on IE, but to no avail.

Dim ie As InternetExplorer
searchword = Word.Selection
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate2 "http://www.websters-online-dictionary.org/"
ie.Visible = True

Any help is appreciated.

Carlos
 
J

Jay Freedman

Hi Carlos,

I don't know whether you can find the magic syntax of the URL to the
dictionary, but here's a thread that shows how to do something similar with
Google:

http://groups.google.com/[email protected]

The question is whether the dictionary site's HTML is written in a way that
allows passing the searchword in the URL, or whether it requires putting the
word into the input box and invoking the JavaScript function. If it's the
latter, I don't think there's any way to automate it -- instead you should
look for a different dictionary site that does support parameters in the
URL.
 
C

Carlos Chalhoub

After wasting 3 days doing research, I was finally able to figure it out.

Here goes:


Sub Webster()
Dim ie As InternetExplorer
On Error GoTo ErrorHandler
Selection.Copy
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate2 "http://www.websters-online-dictionary.org/"
Do
' Loop until the entire page is loaded then print.
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.ExecWB OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT
SendKeys "{ENTER}"
ErrorHandler:
Exit Sub
End Sub
 
C

Carlos Chalhoub

Thanks for the reply, Jay. I had a small problem with my code and had to
change it a bit and was wondering if you could help me.

Right now, the user has to select a word or an expression, then run the
macro on it. I want a message box to display asking him for a selection. I
put the message box in the errorHandler where it displays fine if no
selection is made; however, even when a selection is made, the macro runs,
then reads the message box and displays it. I thought that a macro goes to
the errorhandler only if there is an error, but I guess not. What am I doing
wrong?

Sub Webster()
Dim ie As InternetExplorer
On Error GoTo ErrorHandler
Selection.Copy
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate2 "http://www.websters-online-dictionary.org/"
Do
' Loop until the entire page is loaded then print.
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.ExecWB OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT
SendKeys "{ENTER}"
ErrorHandler:
MsgBox "Please make a selection."
End Sub
 
J

Jay Freedman

Hi Carlos,

The "ErrorHandler:" line is only a label -- it doesn't have any function
like "don't come in here if there's no error". You need an Exit Sub
statement just before it to prevent execution from proceeding into the
MsgBox line. Besides that, there's nothing in the macro that would trigger
an error if the Selection is empty (although other errors could occur, such
as failing to create the IE object). Instead, you need to test that before
you continue on...

Sub Webster()
Dim ie As InternetExplorer
On Error GoTo ErrorHandler

If Selection.Type <> wdSelectionNormal Then
MsgBox "Please make a selection.", , "Webster"
Exit Sub
End If

Selection.Copy
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate2 "http://www.websters-online-dictionary.org/"
Do
' Loop until the entire page is loaded then print.
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.ExecWB OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT
SendKeys "{ENTER}"
Exit Sub

ErrorHandler:
MsgBox Err.Number & vbCr & Err.Description, , "Error"
End Sub
 
C

Carlos Chalhoub

Thanks Jay. IE is not opening in a maximized window. Do you know the proper
syntax for it?
 
J

Jay Freedman

Hi Carlos,

Ten minutes ago I didn't know, but you can get anything you want (within
reason) on Google...

There isn't anything built into either VBA or the Internet Explorer object
to maximize the window, but there is a Windows API call that does it. First,
place the following declaration of the function at the top of the code
window, before any Sub or Function statement:

Public Declare Function ShowWindow& Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Integer)

Then add this constant declaration after the Dim statement:

Const SW_MAXIMIZE = 3

Finally, add this line after the ie.Visible statement:

ShowWindow ie.hwnd, SW_MAXIMIZE

This tip came from a page on www.utteraccess.com that's apparently no longer
available but is cached by Google at
http://64.233.167.104/search?q=cach...l+InternetExplorer.Application+maximize&hl=en
 
C

Carlos Chalhoub

Thanks Jay,

I guess we learn every day :)

Here's the complete code for those who want to do similar things:

---------------------------------------------------
Public Declare Function ShowWindow& Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Integer)

Sub Webster()
Dim ie As InternetExplorer
Const SW_MAXIMIZE = 3

On Error GoTo ErrorHandler

If Selection.Type <> wdSelectionNormal Then
MsgBox "Please make a selection.", , "Webster Online Dictionary"
Exit Sub
End If

Selection.Copy
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate2 "http://www.websters-online-dictionary.org/"
Do
' Loop until the entire page is loaded then print.
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.ExecWB OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT
SendKeys "{ENTER}"
ie.Visible = True
ShowWindow ie.hwnd, SW_MAXIMIZE
Exit Sub

ErrorHandler:
MsgBox err.Number & vbCr & err.Description, , "Error"
End Sub
------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top