General discussion

Locked

VBScript: current active window

By mitch_haber ·
I would like to capture the current active Window title in a string.

Ideally I would like to grab the current openend file and path, however since the file name appears inthe title bar I thought I could parse out the name. Ultimatrly I will locate the file and change the file to read only.

I realize this wouldn't make sense normally because the file is currently open but I am using this technique as a sort of 'check out/in' system for a shared file workspace folder. The program (not a MSOffice program) does not have this capability. However if I can write the script then users can manually execute the code. Any ideas even partial is helpful? I have tried to use GetActiveWindowTitle but I must have the syntax incorrect because wscript reports a compilation error.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by amanabala In reply to VBScript: current active ...

Hello,
There is no direct API call "GetActiveWindowTitle". You can get the window text in 2 steps.
1. Use GetActiveWindow() to get the window handle
2. Use GetWindowText or GetWindowTextA to get the window text.
Here is a code snippet that might help:
dim hwnd,retVal
dim s
hwnd=GetActiveWindow()
s=space(256)
retVal = GetWindowText(hwnd,s,256)

Now, "s" contains your window title. This code assumes that the program in question is indeed the active window..

Hope this helps,
Ravi

Collapse -

by mitch_haber In reply to VBScript: current active ...

I am trying to do this using Windows Host Scripting. I think your solution uses VB not VBS. Or basically I am just confused, but I do not think this canbe done in VBS (at least easily).

When I ran the suggested code I got the following:
C>cscript getwin.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\getwin.vbs(3, 1) Microsoft VBScript runtime error: Type mismatch: 'GetActiveW
indow'

Collapse -

by mitch_haber In reply to VBScript: current active ...

okay dumb me...compiles okay but...
I modified the code a bit and it comiles okay. Needed spaces between "var = values"
I wanted to display the string just to be sure, but I am stumbling a bit.

dim hwnd,retVal
dim s, WSH

on error resume next
set WSH = Wscript.CreateObject ("WScript.Shell")

hwnd = GetActiveWindow()
s = space(256)
retVal = GetWindowText(hwnd,s,256)

msg = (" & retval & ")

WScript.Echo msg

Collapse -

by gl1200_87 In reply to VBScript: current active ...

Hi All
I have tried the above code and all I get is & retval & in the message box.
I tried changing the wscript.echo msg to
WScript.Echo hwnd + s + retval
and all I get is a blank message box.
I must be doing something wrong.
Thank

Collapse -

by mitch_haber In reply to VBScript: current active ...

I don't thinl you did anything wrong. I beleive my note prior indicate the same problem?

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums