Consulting

Results 1 to 5 of 5

Thread: Switch the active Window ...

  1. #1
    VBAX Regular
    Joined
    Mar 2009
    Location
    Stowmarket
    Posts
    62
    Location

    Switch the active Window ...

    I am running Excel 2003 under Windows Vista.

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

    [vba]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[/vba]
    As highlighted in red, how do I make the new window the Active window, so that I can edit the text file ?!?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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.
    [vba]dbRetValue = Shell(stNoteExe & " " & stFileName, vbNormalFocus)[/vba]

    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)
    [vba]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[/vba]

    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

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This in a standard module seems to open the textfile on top and ready to edit.

    [VBA]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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Regular
    Joined
    Mar 2009
    Location
    Stowmarket
    Posts
    62
    Location

    Thank You both ...

    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 !!!

    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 !!!

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •