2

Struggling to find a solution to this one. From Visual Basic (VBA in Excel more specifically) I'm able to call an Internet Explorer window by title using

AppActivate ("My Page Title - Windows Internet Explorer")

And it works great each time.

I can open a new window and send a url to it using..

Dim ie As Object
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://websiteurl"

And that also works okay But it opens a new browser each time and I want it to always calls the same window.

So can i Set ie to equal the same page each time. So instead of

Set ie = New InternetExplorer

It does something like

Set ie = ACTIVE InternetExplorer

(though that doesn't seem to exist). Is there some way of setting ie to be the same as AppActivate ("My Page Title - Internet Explorer") ?

Thanks

Full Code here:

Sub Find_Recordings()
Dim MyAppID, ReturnValue

AppActivate ("My Page Title - Windows Internet Explorer")

SendKeys ("^a")
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys ("^c")
Application.Wait (Now + TimeValue("0:00:01"))

AppActivate ("Microsoft Excel")
Sheets("DataSearcher").Select
Range("K1").Select
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon: = False

Range("A1").Select

Dim ie As Object
Set ie = New InternetExplorer
ie.Visible = True     ie.Navigate "http://wwwmyurl"

Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

ie.Document.getElementById("searchdata1").Value = Range("J1")
ie.Document.getElementById("library").Value = "RECORDINGS"
ie.Document.searchform.Submit



End Sub
brettdj
  • 54,857
  • 16
  • 114
  • 177
paj
  • 59
  • 2
  • 3
  • 8
  • 1
    Can you use the MSXML object to post directly to the website and skip the automation altogether - this would be much faster – SWa Mar 15 '12 at 16:19
  • I'm not familiar with that. Is that front end or back end? I'm restricted to front end I'm afraid. Just want the macro to copy what a user can do manually, but faster. Cheers – paj Mar 16 '12 at 09:40
  • 1
    It's front End, I posted an example of how to do it here http://stackoverflow.com/questions/9486847/close-javascript-alert-using-vba-automation/9516425#9516425 Can you post the url of your actual site? – SWa Mar 16 '12 at 10:23
  • It's actually an intranet site. Will look at your link, thank you – paj Mar 16 '12 at 15:16
  • I've tried @Kyle's idea on another problem that I had myself. It didn't work for my specific issue, but I did play with it to get the hang of it, and it is a nice solution. – Gaffi Mar 16 '12 at 17:55

1 Answers1

1

You could try something like this, drawing heavily on reusing Internet Explorer COM Automation Object to identify an instance of IE with then specific web page active that you are looking for.

Change
strURL = "http://www.theage.com.au/"
to

"My Page Title - Windows Internet Explorer" as necessary

VBA

Sub Test()
Dim ShellApp As Object
Dim ShellWindows As Object
Dim IEObject  As Object
Dim strURL As String
strURL = "http://www.theage.com.au/"
Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()
Dim i
For i = 0 To ShellWindows.Count - 1
    If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
        If ShellWindows.Item(i).LocationURL = strURL Then
            Set IEObject = ShellWindows.Item(i)
            MsgBox "IE instance with " & strURL & " found"
            Exit For
        End If
    End If
Next
End Sub
Community
  • 1
  • 1
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • Thanks BrettDJ. I tried this and adjusted the URL as you suggested. Doesn't seem to work though. I'm not sure where it sits and what it replaces in my current code. I've added the full code of the sub in my original question. Further help would be most welcome. Thanks – paj Mar 15 '12 at 14:59
  • @brettdj Did you mean to say change `strURL` to another URL, and not `"My Page..."`? – Gaffi Mar 15 '12 at 15:06
  • @paj yes, I meant change `StrURL` to the url that you wanted to locate as being open in IE – brettdj Mar 16 '12 at 00:59
  • You can find a variant at: http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window.html – Julien Kronegg Dec 20 '12 at 11:00