MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Feb 26th, 2002, 01:32 PM   #1
Guest
 
Posts: n/a
Default

How would I modify this to work more like a [control-TAB]
If the app is already open and to open app if not already?

Sub ardis_open()
Range("A4:J93").Copy
Range("A2").Activate
MyAppID = Shell("C:ardiscowin.EXE", 1) ' Run ardis
AppActivate MyAppID ' Activate ardis
End Sub
  Reply With Quote
Old Feb 26th, 2002, 03:09 PM   #2
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Anon,

Here's one way to do it, copy the following into a module (I added the findwindow function so that some workable code is in one post):

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

sub test()
h = FindWindow("Windows Class Name for your Application", vbNullString)
If h > 0 Then
AppActivate "Windows Class Name for your Application"
Else: MyAppID = Shell("C:ardiscowin.EXE", 1)
end if
end sub

When a program is launched, like Lotus Notes or Winzip, Windows assigns an integer which it calls a "handle" by nickname. For Lotus Notes, it is "Notes." You'll have to find the Windows Class name for your program you're referring to. If the associated integer for the handle is greater than zero, the program is open. The best way I've found to test for a launched program.

Here's a small list of class names and their respective applications:

http://www.generation.net/~hleboeuf/handlevb.htm

And here's how to score other class names:

http://support.microsoft.com/default...;EN-US;q112649

Cheers, Nate

[ This Message was edited by: NateO on 2002-02-27 16:10 ]
NateO is offline   Reply With Quote
Old Feb 27th, 2002, 02:40 AM   #3
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

[quote]
On 2002-02-26 14:09, NateO wrote:
Anon,

Here's one way to do it:

sub test()
h = FindWindow("Windows Class Name for your Application", vbNullString)
If h > 0 Then
AppActivate "Windows Class Name for your Application"
Else: MyAppID = Shell("C:\ardis\cowin.EXE", 1)
end if
end sub


Nate
You forgot the windows API for this sub

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

The windows handle is usually a long

The good links that NateO gave are useful
if you are using VB but for VBA you will
need to change a few things for the Ms link..
Use the IEtimer as VBA has no Timer.
You will have to link the Prints to a textbox
If you can do these then the routine is very good.
Otherwise to get the class name have a look
@ NateO 2nd link or use this routine I use

Option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long


Sub GetClass()
Dim WinWnd As Long, sWndTitle As String, RetVal As Long, lpClassName As String

sWndTitle = InputBox("Enter the exact window title:" & Chr$(13) & Chr$(10) & _
"Note: must be an exact match")

WinWnd = FindWindow(vbNullString, sWndTitle)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub

lpClassName = Space(256)
RetVal = GetClassName(WinWnd, lpClassName, 256)
MsgBox "Classname for Window Caption" & vbLf & vbLf & _
"[" & sWndTitle & "] = " & Left$(lpClassName, RetVal)

End Sub





Ivan F Moala



Ivan F Moala is offline   Reply With Quote
Old Feb 27th, 2002, 01:49 PM   #4
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Ivan, good catch on the API, my fingers are moving faster than the brain. Another good one with the VB. I liked your code for calling the class name, very choice, hope you don't mind me playing with it below...While it seems relatively simple, I was having a little difficulty getting the exact string correct. So I played with a function (er 2) where you can enter part of a string to return the class name (i.e., Mr Excel or Internet returns IEFrame). It likes to default to the program manager when all else fails, but otherwise, I like this a lot. Thought you might be interested:

Option Explicit
Option Compare Text
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private psAppNameContains As String
Private pbFound As Boolean
Private sTitle As String
Public Function apptitlebystringpart(StringPart As String) As Boolean
Dim lRet As Long
psAppNameContains = StringPart
lRet = EnumWindows(AddressOf CheckForInstance, 0)
apptitlebystringpart = pbFound
pbFound = False
End Function
Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _
lParam As Long) As Long
Dim lRet As Long
Dim iNew As Integer
If Trim(psAppNameContains = "") Then
CheckForInstance = False
Exit Function
End If
sTitle = Space(255)
lRet = GetWindowText(lhWnd, sTitle, 255)
sTitle = StripNull(sTitle)
If InStr(sTitle, psAppNameContains) > 0 Then
CheckForInstance = False
pbFound = True
Else
CheckForInstance = True
End If
End Function
Private Function StripNull(ByVal InString As String) As String
Dim iNull As Integer
If Len(InString) > 0 Then
iNull = InStr(InString, vbNullChar)
Select Case iNull
Case 0
StripNull = InString
Case 1
StripNull = ""
Case Else
StripNull = Left$(InString, iNull - 1)
End Select
End If
End Function
Sub GetClass()
Dim WinWnd As Long, sWndTitle As String, RetVal As Long, lpClassName As String
sWndTitle = InputBox("Enter at least one word from the Window Title:")
If sWndTitle <> "" Then
apptitlebystringpart (sWndTitle)
sWndTitle = sTitle
WinWnd = FindWindow(vbNullString, sWndTitle)
lpClassName = Space(256)
RetVal = GetClassName(WinWnd, lpClassName, 256)
MsgBox "Classname for Window Caption" & vbLf & vbLf & _
"[" & sWndTitle & "] = " & Left$(lpClassName, RetVal)
Else: MsgBox ("You didn't enter a term, making the task challenging...")
End If
End Sub

This type of procedure is almost necessary if you're going to try to use the call names of programs like Adobe Acrobat or Paint Shop Pro, where the call name is "dynamic" (Windows changes the call name every time it's launched).


Cheers,

Nate

[ This Message was edited by: NateO on 2002-02-27 16:09 ]
NateO is offline   Reply With Quote
Old Feb 28th, 2002, 02:03 AM   #5
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Nate

Good one....was actually going to code
one to look at the windows name (partial)

No need to now....and yes this is much better
then typeing in the exact name of the window


cheers

Ivan
Ivan F Moala is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 07:56 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes