rjwebgraphix
Well-known Member
- Joined
- May 25, 2010
- Messages
- 590
I'll try to make a long story as short as possible. I have a script I'm working on that keeps loading windows to the background. Otherwise, it functions as it should with what is currently complete.
I created a video to really showcase what it's doing. https://www.youtube.com/watch?v=QYhmihrzSjs
The basis of the script is that it will restore a backup from a list of files that were previously backed up, although I do still have to write that portion of the code as well as dynamically create the list, but right now it's about getting functional what currently exists. I can handle the rest on my own.
If I start the script from an Explorer window, select the file to restore from the listbox and press restore, it works as it should. It runs the code to restore the file (not written yet, just a msgbox marker representing what will happen). That portion is working. Although, if I hit cancel the msgbox appears in the background rather than foreground.
To take this one step further, if I run the code from a shortcut on the desktop then all windows open in the background rather than the foreground. That's even more mind boggling than the first problem. Why does it matter that it was ran from a shortcut? It's like it's not even registering the AppActivate
Any idea how to fix it so that 1. the script retains focus and actually brings the window needed to the foreground and 2. operate the same using a desktop shortcut.
As a side note: A moderator told me to use this particular forum since it's not technically VBA, although they should be similar or at least pretty close to the same.
I created a video to really showcase what it's doing. https://www.youtube.com/watch?v=QYhmihrzSjs
The basis of the script is that it will restore a backup from a list of files that were previously backed up, although I do still have to write that portion of the code as well as dynamically create the list, but right now it's about getting functional what currently exists. I can handle the rest on my own.
If I start the script from an Explorer window, select the file to restore from the listbox and press restore, it works as it should. It runs the code to restore the file (not written yet, just a msgbox marker representing what will happen). That portion is working. Although, if I hit cancel the msgbox appears in the background rather than foreground.
To take this one step further, if I run the code from a shortcut on the desktop then all windows open in the background rather than the foreground. That's even more mind boggling than the first problem. Why does it matter that it was ran from a shortcut? It's like it's not even registering the AppActivate
Any idea how to fix it so that 1. the script retains focus and actually brings the window needed to the foreground and 2. operate the same using a desktop shortcut.
As a side note: A moderator told me to use this particular forum since it's not technically VBA, although they should be similar or at least pretty close to the same.
Code:
'will be needed to work with file system
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
' file system object
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'Call function and return filename as string
Dim BackupFile
BackupFile = IEButtons
If BackupFile <> "0" then
'Restore Backupfile
Msgbox "Restoring File:" & BackupFile
Else
Msgbox "Restore Canceled"
End if
Function IEButtons( )
' Function was originally created by http://www.robvanderwoude.com/vbstech_ui_buttons.php
' Function modified for listbox input
' Function now returns string rather than integer
' This function uses Internet Explorer to create a dialog.
Dim objIE, sTitle, iErrorNum
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
' Create an IE object
Set objIE = WScript.CreateObject( "InternetExplorer.Application" )
'Make visible and brings window in focus WHY DO YOU HAVE TO ACTIVIATE THE WINDOW????? MSGBOX's still go to background
objIE.Visible = True
wshShell.AppActivate objIE
' specify some of the IE window's settings
objIE.Navigate "about:blank"
sTitle="Select File to Restore" & String( 80, "." ) 'Note: the String( 80,".") is to push "Internet Explorer" string off the window
objIE.Document.title = sTitle
objIE.MenuBar = False
objIE.ToolBar = False
objIE.AddressBar = false
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 340
objIE.Height = 365
' Center the dialog window on the screen
With objIE.Document.parentWindow.screen
objIE.Left = (.availWidth - objIE.Width ) \ 2
objIE.Top = (.availHeight - objIE.Height) \ 2
End With
' Wait till IE is ready
Do While objIE.Busy
WScript.Sleep 200
Loop
' Insert the HTML code to prompt for user input
objIE.Document.body.innerHTML = "Select MGSV: TPP Save File to Restore." _
& "<div align=""center"">" & vbcrlf _
& "<select id=""FileName"" name=""FileName"" size=""15"">" _
& "<option value=""TPP_GAME_DATA1.B01"">TPP_GAME_DATA1.B01 - 2/12/2017 12:06 AM</option>" _
& "<option value=""TPP_GAME_DATA1.B02"">TPP_GAME_DATA1.B02 - 2/13/2017 12:06 AM</option>" _
& "<option value=""TPP_GAME_DATA1.B03"">TPP_GAME_DATA1.B03 - 2/14/2017 12:06 AM</option>" _
& "<option value=""TPP_GAME_DATA1.B04"">TPP_GAME_DATA1.B04 - 2/15/2017 12:06 AM</option>" _
& "<option value=""TPP_GAME_DATA1.B04"">TPP_GAME_DATA1.B05 - 2/16/2017 12:06 AM</option>" _
& "</select>" _
& "<p><input type=""hidden"" id=""OK"" name=""OK"" value=""0"">" _
& "<input type=""submit"" value=""Restore"" *******=""VBScript:OK.value=FileName.value"">" _
& "<input type=""hidden"" id=""Cancel"" name=""Cancel"" value=""0"">  " _
& "<input type=""submit"" id=""CancelButton"" value=""Cancel"" *******=""VBScript:Cancel.value=-1""></p></div>"
'Get element
' Hide the scrollbars
objIE.Document.body.style.overflow = "auto"
' Set focus on Cancel button
objIE.Document.all.CancelButton.focus
wshShell.AppActivate(objIE.document.title)
'CAVEAT: If user click red X to close IE window instead of click cancel, an error will occur.
' Error trapping Is Not doable For some reason.
'Note: Error Trapping done through code then looking for "" rather than an error code
Dim RestoreFile
On Error Resume Next
Do While objIE.Document.all.OK.value = 0 and objIE.Document.all.Cancel.value = 0
WScript.Sleep 200
RestoreFile = objIE.Document.all.OK.value
If RestoreFile <> 0 and RestoreFile <> "" Then 'user did not clicked cancel or X
'User Clicked Restore
IEButtons = RestoreFile
objIE.Quit
Set objIE = Nothing
Exit Function
End if
IEButtons = 0
Loop
On Error Goto 0
' Close and release the object
objIE.Visible = False
objIE.Quit
Set objIE = Nothing
End Function