VBScript: Loading to background issue.

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.

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""> &nbsp" _
                                & "<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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Made some progress on this. I found out why clicking Cancel went to the background.... Well, not the reason why, more what was causing it. I still don't understand the why. Although by removing....

Code:
objIE.Visible = False

....from the last few lines the msgboxes come up as they should. That was what the difference was between Restore and Cancel, but now if it's ran from a desktop icon that's a different story. It runs fine if navigating to the file and double clicking the file, but if you run it from a desktop icon it still goes to the background. Figure that out and I'll have this thing licked yet.
 
Last edited:
Upvote 0
....and I figured it out. Whew!!!! *wipes brow*

The problem was not in the code at all, but was lack of code in previous attempts testing the script and in previous attempts the IE Object was not closed out properly, so it was still technically running as a windows process, though it was doing nothing and as my previous message stated, it was not visible, but still an open process. As a result it wouldn't allow anything to gain focus / activate. I only figured that out because I loaded Taskman to check something with performance and noticed there were still instances of IE running and other than this purpose, I don't use IE at all. I also haven't rebooted my computer in weeks, so it never had a chance at a fresh start. :)

Now back to getting the rest of it work now that all windows are operating properly.

Peace,

RJ
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,657
Members
452,575
Latest member
Fstick546

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