• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

[Solved] VBScript help

Tweak155

Lifer
So basically my goal is to disable Immediate Window access in.... Access.

I "compile" the MDB into an MDE but a savvy user can use the MDE Immediate Window to do things I don't want them to do.

After researching and seeing that there is no multi-threading in VBA (so I can monitor the windows the user opens), I found an idea about kicking off a VBS that can infinite loop and check for the VBA editor and whether or not it is open.

I want my "infinite" loop to end when it detects that Access is no longer open.

So basically it will work like this:

User opens the MDE
MS Access will write a VBS to the user's system and execute it.
This VBS will scan every second for the VBA editor
If the VBA editor is open, it will force close it (through SendKeys)
Once MS Access is closed, the script self terminates.

The "tricky" part to this is I want to look at the application title to determine when to close - since I will always know the title and it should be unique. Maybe I can pass the application handle to the VBS, but even then I'm not sure how to check if that handle is simply open.

I've simulated the loop with the following:

Code:
Set oShell = CreateObject("WScript.Shell") 

While oShell.AppActivate("App Title")
If oShell.AppActivate("Microsoft Visual Basic for Applications") Then
   WScript.Sleep 500
   oShell.SendKeys "%{F4}"
End If

wscript.Sleep 1000
Wend

Problem is this activates the window as my check - so if they go to use another application, it will keep bringing the Access app to the forefront. Looking through the WIN32 classes I can't find one that will suit my needs.

Any ideas?

Thanks.
 
Last edited:
Usually (if I remember right) when you create a process, you get a HANDLE to the process.

When the process ends, windows will set an event on that handle.

So if you have a WaitForSingleObjects with a 0 timeout, you can check to see if that signal has occured. If it has, the app has closed, if not, it's still open.

Also.

http://stackoverflow.com/questions/506195/win32-api-for-shutting-down-another-process-elegantly

Looking up this function - I can see how it could be used.

I can set a wait time of 0 or 1 and check for the WAIT_TIMEOUT reply. If I get a WAIT_FAILED it isn't open... I'll see if I can get this going. Thanks.
 
Well I can't get it going. VBScript doesn't recognize the WaitForSingleObject function and I can't get it to include Kernel32 functions.

I'm not familiar with VBScript (this will be my first). Probably easy to someone who has done it at least a little...any ideas?
 
I figured out a solution. And since I google all over the place, this might help someone some day.

You can queue the running tasks inside a Word application then search through their titles that way. The following will identify a running window and close it, and kill itself when the specified app closes:

Code:
Dim Word
Dim apprunning
Dim keepGoing 
Dim oShell

Set Word = CreateObject("Word.Application")

Set oShell = CreateObject("WScript.Shell")

keepGoing = 1

While keepGoing = 1

Set Tasks = Word.Tasks

apprunning = 0

For Each Task in Tasks
  If Task.Visible Then
    If InStr(Task.name, "App that is open that will keep this script running") > 0 Then
      apprunning = 1

      If oShell.AppActivate("Window you want to close") Then
        WScript.Sleep 50 'Delay to allow activation of window
        oShell.SendKeys "%{F4}" 'ALT F4
      End If
  End If
Next

wscript.Sleep 150  'Set how often to check here (this is in milliseconds)

if (apprunning = 0) then
   keepGoing = 0
end if

Wend

It's pretty crude but it works.
 
Last edited:
Back
Top