PDA

View Full Version : Switch the active Window ...



vodkasoda
03-30-2009, 04:22 PM
I am running Excel 2003 under Windows Vista.

The following code successfully opens an existing .TXT file ...

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Sub Open_Notepad_StartUp_File()
Dim dbRetValue As Double
Dim stNoteExe As String, stFileName As String

stNoteExe = "Notepad.exe"
stFileName = MyTeamTo
dbRetValue = Shell(stNoteExe & " " & stFileName) <-- This line opens a new window & I want this window to become Active ...
End Sub
As highlighted in red, how do I make the new window the Active window, so that I can edit the text file ?!?

GTO
03-30-2009, 06:38 PM
Greetings,

You'll probably get better advice on Shell than this, but til then, I think you can use Shell's second arg to set the focus.
dbRetValue = Shell(stNoteExe & " " & stFileName, vbNormalFocus)

I saw that you looked at ShellExecute, which seems to be at least as good in setting the focus if not better. (Shell seemed to throw noetpad in front of Excel, but ShellExecute would even toss notepad in front of VBIDE if stepping thru)
Option Explicit

Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long

Declare Function apiFindWindow Lib "User32" Alias "FindWindowA" _
(ByVal lpclassname As Any, ByVal lpCaption As Any) As Long

Global Const SW_SHOWNORMAL = 1
'
Sub A_NotePad()
Dim lTxtFile As Long
Dim strPath As String
Dim strName As String

'//change to suit//
strPath = ThisWorkbook.Path & _
Application.PathSeparator
'//SAA//
strName = "MyText.txt"

lTxtFile = ShellExecute(0, "open", strPath & strName, "", _
strPath, SW_SHOWNORMAL)
End Sub

Of course I don't know what you are editing/adding/deleting once the txt file is open, but I think I would look at either FSO or using WORD.

Mark

lucas
03-30-2009, 07:34 PM
This in a standard module seems to open the textfile on top and ready to edit.

Option Explicit

Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Function OpenAnyFile(FileToOpen As String)

Call ShellExecute(0, "Open", FileToOpen & vbNullString, _
vbNullString, vbNullString, 1)

End Function
Sub Example()
Call OpenAnyFile(ThisWorkbook.Path & "\Example Text File.txt")
End Sub

vodkasoda
03-31-2009, 09:34 AM
GTO : The reason I am opening and editing a .TXT file is simply because that is what is sent to me by E:Mail and that is what the sender wants back ... he's even more of a dinosaur than I am, unfortunately !!! As for "I saw that you looked at ShellExecute" ... to be honest, I just cut & pasted it from some code on a website, I haven't got a clue what ShellExecute is or how it works :whistle: !!!

lucas : That works perfectly, thank you, all I had to do was add my filename to where I called the module & bang, exactly what I wanted ... though I still have no idea how it works :o: !!!

lucas
03-31-2009, 03:25 PM
where the call to Shellexecute is located you can delete the parameters and as you add them intellisense will inform you of what is next.

glad it worked.