INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Compliments on a fantastic web site. I have learned so much from my threads and even if sometimes I cannot solve the problem, it gives me the reassurance that I am not the only one putting up with it!..."

Geography

Where in the world do Tek-Tips members come from?
mkefeli (Programmer)
6 Sep 03 19:07
I want to start calc.exe in Visual Basic with shell command. But I want to check there is a calc.exe already running or not. If there is, then setfocus to it; if not, start a calc.exe program. How can I do this?
Check Out Our Whitepaper Library. Click Here.
vb5prgrmr (Programmer)
6 Sep 03 19:12

You can accomplish what you want with FindWindow and SetWindowPos, or you can EnumWindows.

VB Forum222 or VB Win32API Forum711 as suggestions for the future.

Good Luck

Helpful Member!Hypetia (Programmer)
7 Sep 03 10:49
Hi mkefeli,
From your profile it seems that you have just joined Tek-Tips and this is your first post here.

Welcome to Tek-Tips.
Hope you will enjoy your time here with the Tek-Tips community.

Being a new member, I will advise you to see FAQ222-2244, "How to get the best answers". It will provide you very useful guidelines for getting the best out of this forum.

And here is the solution to your problem.

You need a command button (Command1) on your form to test this code.
___

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_NORMAL = 1

Private Sub Command1_Click()
    Dim hwndCalc As Long
    hwndCalc = FindWindow("SciCalc", "Calculator")
    If hwndCalc = 0 Then
        Shell "calc.exe", vbNormalFocus
    Else
        ShowWindow hwndCalc, SW_NORMAL
        SetForegroundWindow hwndCalc
    End If
End Sub

SBendBuckeye (Programmer)
2 Oct 03 11:47
Hello Hypetia,

Thanks for the example code. The FindWindow is asking for Class Name and Window Name. I tried that with Excel using "Excel" as the class name and "Microsoft Excel" as the window name and it didn't find it.

I am very new to API calls so please forgive the elementary level questions. I infer that class name is the Library name that shows up in the object browser. Is that correct? Then I just used the title bar hoping that would work.

Thanks for any help anyone can give me!

Have a great day!

j2consulting@yahoo.com

Helpful Member!Hypetia (Programmer)
3 Oct 03 7:35
Class name is not the library name. This Class name is the name of the registered window class from which the subject window is created. For Excel windows, this class name is "XLMAIN" (without quotes). So when you call the FindWindow function, pass this string as the lpClassName argument.

The other parameter, is in this case optional. You should pass this parameter only if you exactly know the title of the target window. The title of the Excel window is normally "Microsoft Excel" if no workbook or other document is currently opened.

However, this caption is not fixed and may vary if you open a document in Excel, for example, it may change to "Microsoft Excel - SER.dbf", where SER.dbf is the filename of the currently opened document.

In this case, you should ignore the lpWindowName parameter and pass vbNullString in its place. FindWindow function then looks only for the correct class name when finding windows.

The title of the Calculator application does not change, that is why we specified title as well when finding the Calculator window.

So the code for your requirement becomes this.
___

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_NORMAL = 1

Private Sub Command1_Click()
    Dim hWndXL As Long, PathXL As String
    PathXL = "D:\Program Files\Microsoft Office\Office10\Excel.exe"
    hWndXL = FindWindow("XLMAIN", vbNullString)
    If hWndXL = 0 Then
        Shell PathXL, vbNormalFocus
    Else
        ShowWindow hWndXL, SW_NORMAL
        SetForegroundWindow hWndXL
    End If
End Sub

___

Note that you need to substitute the correct path of Excel.exe on your computer or better retrieve it from the registry.
SBendBuckeye (Programmer)
3 Oct 03 13:51
Hello Hypetia,

Sorry it took me so long to come back and say thank you but I was under the gun. Your code was exactly what I needed. After you got me pointed in the right direction, here is some code I found and modified slightly to return the class name based on the Window title. It came from the AllAPI site at http://www.mentalis.org/index2.shtml.

Based on your answer above you probably already know this but just in case I thought I would post it.

I also found a similar thread on the VB API forum:
Thread711-663482.

Private Declare Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long

Private Declare Function GetClassName Lib "user32" Alias _
  "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName _
  As String, ByVal nMaxCount As Long) As Long

Private Declare Function ShowWindow Lib "user32" _
  (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_SHOWNORMAL = 1
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameNotePad = "Notepad"

Public Sub GetClassNameFromTitle()
Dim sInput As String, lpClassName As String
Dim nMaxCount As Long, lresult As Long, hwnd As Long
  nMaxCount = 256
  lpClassName = Space(nMaxCount)
  sInput = InputBox("Enter the exact window title:" + _   
    Chr$(13) + Chr$(10) + "Note: must be an exact match")
  hwnd = FindWindow(vbNullString, sInput)
  If hwnd = 0 Then
    MsgBox "Couldn't find the window."
  Else
    lresult = GetClassName(hwnd, lpClassName, nMaxCount)
    MsgBox "Window: " + sInput + Chr$(13) + Chr$(10) + _
      "Classname: " + Left$(lpClassName, lresult)
    Debug.Print sInput & ": " & Left$(lpClassName, lresult)
  End If
End Sub

Thanks again for your helpfulness!

Have a great day!

j2consulting@yahoo.com

Start A New Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: Win API (Microsoft) Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=713
DESCRIPTION: Win API (Microsoft) technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.

 
Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close