Switching Internet Explorer Windows with VBA - windows

I have been working on an Excel macro to automate an Internet Explorer process at work. I'm incredibly close to figuring everything out, but at the end, the macro clicks on a "Download Report" button, which launches a new window. I need to initiate a few things on this new window but I cannot figure out how to change the focus of the macro to the new window. I have found a few things online, but they have been unsuccessful. I know there are ways to toggle between browser windows by telling Excel to look for the name of the browser window, and to activate it, but parts of the name of the new window are constantly changing (it's a unique URL for that particular window).
I was wondering if there might be a way to have the Macro look for just the first Nine letters of the window and switch to that window? Similar to the LEFT function in excel? The reason being is that the first 9 characters of the URL never change. The name of the original window (the one I'm trying to switch from) never changes, so maybe I could tell Excel to look for the the Browser Window that is NOT named the original one? Or any other ideas?
Thanks in advance for any help

Is the VBA AppActivate command what you are looking for?

You will need to add the references to the Internet Controls and shell. You just need to loop through the IE windows and look at the left hand side of the url. See below
Function GetInternetWindow(urlToLookFor as String) As InternetExplorer
Dim winShell As Shell
Dim ie As InternetExplorer
Set winShell = New Shell
'loop through the windows and look at the urls
For Each ie In winShell.Windows
If Left(ie.LocationURL, 9) = urlToLookFor Then
GetInternetWindow = ie
Exit For
End If
Next
End Function

Related

Windows: Open program in specific screen location

At work, I have a set of batch files and scripts which I use to automate all the programs I need to open in the morning (and others to close them all down at night) in order to save time and effort. The problem is, the windows all open in whatever location they so choose, since most of the information is cleared out overnight on the Virtual Desktop.
I have seen people talking of desktop managers and additional programs that make such tasks easier, but due to the restrictions at play, I do not have the ability to install programs like that. I also do not have access to edit registry files, and I can never be sure if the registry files will stay the same or be wiped and reimaged at night. So re-opening at the stored previous location seems to be out. This means that AutoHotKey , cmdow , and most .exe program options are out of the running.
Essentially what I am looking for is a way to reposition open windows, or open windows in a specific position, using either batch files or vbs. Preferably with a location relative to the screen rather than other windows, as I use multiple monitors.
I'm open to using powershell or potentially other options, but those would likely fall outside my experience. It seems like what I'm asking is either nonexistent or impossible, but I'm hoping maybe someone has an idea. I don't mind complicated code, but I have no idea where to begin on this one guys, any suggestions?
It look like someone as already answer that kind of question here :
Other post on set many programs window size and position
You can also use C# and map powershell on it using cmdlet in powershell and c# as backend. Here is an exemple in c# and just google how to make cmdlet in powershell.
Example in c#
Batch files can't do this. A VBScript can sort of do this using Windows Script Host Object Model.
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run(strCommand, [intWindowStyle: See below], [bWaitOnReturn: TRUE/FALSE])
Set WshShell = Nothing
intWindowStyle values:
0 - Hides the window and activates another window.
1 - Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 - Activates the window and displays it as a minimized window.
3 - Activates the window and displays it as a maximized window.
4 - Displays a window in its most recent size and position. The active window remains active.
5 - Activates the window and displays it in its current size and position.
6 - Minimizes the specified window and activates the next top-level window in the Z order.
7 - Displays the window as a minimized window. The active window remains active.
8 - Displays the window in its current state. The active window remains active.
9 - Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 - Sets the show-state based on the state of the program that started the application.
Not sure about PowerShell. :/

RStudio lost focus when start up

When I start up RStudio on Windows, it lost focus. I have to click on it before I could input anything to the console or script window. I know that Ctrl-1 to focus on the Script Window, and Ctrl-2 to focus on the console window; however, both of these two shortcuts will work as long as I click on RStudio first.
Is there anything I need to set in RStudio?
Software Environment:
RStudio Version 0.99.446
Windows 7 Ultimate 64-bit
As a kludge, you can write a VBScript which first opens RStudio and then switches the focus to it:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "C:\Program Files\RStudio"
WshShell.Run """C:\Program Files\RStudio\bin\rstudio.exe"""
WScript.Sleep 200 'milliseconds
focused = WshShell.AppActivate("RStudio")
i = 1
Do While Not focused And i < 10
WScript.Sleep 100
focused = WshShell.AppActivate("RStudio")
i = i + 1
Loop
Save this to the desktop in a file with a name like ToRStudio.vbs. Then, when you click on the icon, it opens up Rstudio and then tries to repeatedly switch focus to the RStudio window. In my original version of the code, I relied on a fixed time delay before issuing a single call to AppActivate. But, I discovered that when my workspace saved from my last session was large then my VBScript would finish executing before RStudio was ready. With the above loop, it repeatedly tries to grab focus before timing out. Since I put in the loop, it has had a 100% success rate in opening RStudio with the proper focus. In some debugging code I removed, the variable i never once got above 3 in 10 consecutive attempts.
Obviously, you might have to change the path names, and tweak the timings.
In my comment above I said that certain things about how RStudio handled focus struck me as counterintuitive. Since then I discovered RStudio's option to automatically switch focus to console on sourcing. The lack of doing this by default is what I had in mind. The issue in this question was the only remaining focus issue for me. I'm somewhat used to it by now, so I'm not sure if I'll actually use this VBS script.

Can an already running program's window be hidden?

I have been using wsh to run vbscripts in conjunction with iMacros at my work for going on 3 years now. I know that the iMacros browser itself has an option to run hidden but the web-based db interface I have to work with does not function in the iMacros browser. For this reason I have had to use Internet Explorer. I have no problems with the script itself or how it runs. The thing I would like to improve is how the Internet Explorer window itself is handled.
Currently, when iMacros is initiated it creates the IE window in a non-maximized state cascaded from where the previous window was created. Because of iMacros's behavior, I then resize the window using an iMacros script so that enough of the page is rendered for the script to see everything it needs.
Set iim1 = CreateObject ("imacros")
iret = iim1.iimOpen ("-ie -iePrivate", TRUE, 300)
iret = iim1.iimPlayCode("SIZE X=" & scrWidth & " Y=" & scrHeight)
This all works fine and dandy. The scripts are scheduled to run at specific times, gather information out of the db and then imports that data into Excel spreadsheets and prints them out.
What I would like to do is make the IE window hidden while these scripts run. Because I do not create the IE window first (Set objIE = CreateObject("internetexplorer.Application")) I do not have access to the IE window object. I let iMacros create the window with the -iePrivate flag so that it will not disturb my own IE window if I should be logged in and working in the db while the script executes. However, it does try to take focus and become the active application while the script executes. Which can be very annoying at time. My goal is to be able to share these scripts with my co-workers but I don't want the IE windows popping up on them while they may be working on something else.
Is there a way to get to the IE object created by iMacros so that the window can be hidden?
This does Internet Explorer and Explorer windows (they used to be the same program).
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.locationname
Next

Invoke 'Open' Dialog from Windows Desktop

Is there some way I can programmatically (in VBS) OR by using CMD/RUN open the 'Open' dialog that contains the places bar and a browser but without opening say notepad or MSpaint?
http://i.technet.microsoft.com/dynimg/IC354177.jpg
I'd like to use this on the desktop itself, it would be really cool if there was a DLL I can just use instead of having a VBS file but if not i'm sure its possible in VBS.
I'm busy searching where the actual open dialog box comes from, it should come from some DLL file somewhere.
I might even consider stopping the windows shell from opening all together and just using this open window as the shell on some computers.
Regards, Rocklore
What version of Windows are you on?
"UserAccounts.CommonDialog" was the way to do this in XP. But it no longer exists in Windows 7. You may be able to use some of the flags available for the BrowseForFolder() method to make it look like a file open dialog. See this page for an example.
XP Edit:
Here's an XP example using UserAccounts.CommonDialog.
With CreateObject("UserAccounts.CommonDialog")
.InitialDir = CreateObject("WScript.Shell").SpecialFolders("Desktop")
.Filter = "All Files|*.*"
' Show the dialog. If [Open] is clicked, save the name of the selected file...
If .ShowOpen Then strFile = .FileName
End With

How do I launch an Excel document in Windows, edit it and get the application to ask to save it?

I can launch Excel with the document I want using VB.Net 2010:
Dim p As New System.Diagnostics.Process
p.StartInfo.FileName = "c:\temp\myfile.xlsx"
p.Start()
Excel starts OK with the file opened. I edit the file, but when I close it down, Excel doesn't ask if I want to save changes. So it closes and loses my edits.
If I do the same with a text file:
Dim p As New System.Diagnostics.Process
p.StartInfo.FileName = "c:\temp\myfile.txt"
p.Start()
It opens (in Notepad++), I edit it and close it, and Notepad++ asks if I want to save changes. Great!
I've now tried similar code with a Word document - and that's OK. Also with an OpenOffice Sheet document and that works as well.
This behaviour is happening in Windows 7 with Excel 2010. It works as expected in Windows XP with Excel 2007.
What am I doing wrong with Excel? Is it me?
I couldn't recreate your error. I ran the app with and without Excel already being open. I closed out of Excel and then by just closing the workbook only. I was prompted to save each time.
You may need to check your settings in Excel. Could be a security setting. Are you doing any other changes to the excel file programatically or are they just manual changes through the Excel UI only?
Maybe SuperUser.com can help.
OK, so it's not related to the way I was opening the document (using System.Diagnosis.Process). I initially thought it was. If I open files via Explorer, edit them and close Excel, it doesn't ask to save the changed file. So it's not a programming issue.
Thanks for the help anyway guys.
Dave

Resources