Operating a dos like window(screen) with the help of excel

Is this possible , to operate a dos like window(screen like dos) already opened at the desktop with the help of Excel.
The task is to write a predefined command on this window(screen) and then press enter automatically by excel.
Can we make an excel macro button which will automatically switch over to that dos like window and will automatically write predefined command on that screen and will press button enter.
Someone told me we can use SENDKEYS???dunno wats that.Wats is the code for excel-VB by which i can switch over to that already opened window(screen like DOS)
Can anyone guide, plz.

operating cmd.exe

The following code activates an already opened CMD.exe Hopefully you can work from here. Look in the last sub change the name to your app and see if it works.
Option Explicit
 
' Module Name: ModFindWindowLike
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 02/06/2005
                    
Private Declare Function EnumWindows Lib "user32" _
   (ByVal lpEnumFunc As Long, _
    ByVal lParam As Long) As Long
 
Private Declare Function GetWindowText Lib "user32" _
    Alias "GetWindowTextA" _
   (ByVal hwnd As Long, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long
 
Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As Long) As Long
 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long
 
Private Declare Function GetWindowThreadProcessId Lib "user32" _
    (ByVal hwnd As Long, _
     lpdwProcessId As Long) As Long
 
Private Declare Function IsIconic 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 AttachThreadInput Lib "user32" _
    (ByVal idAttach As Long, _
     ByVal idAttachTo As Long, _
     ByVal fAttach As Long) As Long
 
Private Declare Function GetForegroundWindow Lib "user32" _
    () As Long
 
Private Const SW_RESTORE = 9
Private Const SW_SHOW = 5
 
 
'Custom structure for passing in the parameters in/out of the hook enumeration function
'Could use global variables instead, but this is nicer.
Private Type FindWindowParameters
 
    strTitle As String  'INPUT
    hwnd As Long        'OUTPUT

End Type
 
Public Function FnFindWindowLike(strWindowTitle As String) As Long
 
    'We'll pass a custom structure in as the parameter to store our result...
    Dim Parameters As FindWindowParameters
    Parameters.strTitle = strWindowTitle ' Input parameter

    Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
 
    FnFindWindowLike = Parameters.hwnd
 
End Function
 
Private Function EnumWindowProc(ByVal hwnd As Long, _
                               lParam As FindWindowParameters) As Long
 
   Dim strWindowTitle As String
 
   strWindowTitle = Space(260)
   Call GetWindowText(hwnd, strWindowTitle, 260)
   strWindowTitle = TrimNull(strWindowTitle) ' Remove extra null terminator
                                          
   If strWindowTitle Like lParam.strTitle Then
 
        lParam.hwnd = hwnd 'Store the result for later.
        EnumWindowProc = 0 'This will stop enumerating more windows
   
   Else
 
        EnumWindowProc = 1
 
   End If
 
End Function
 
Private Function TrimNull(strNullTerminatedString As String)
 
    Dim lngPos As Long
 
    'Remove unnecessary null terminator
    lngPos = InStr(strNullTerminatedString, Chr$(0))
 
    If lngPos Then
        TrimNull = Left$(strNullTerminatedString, lngPos - 1)
    Else
        TrimNull = strNullTerminatedString
    End If
 
End Function
' Module Name: ModSetForegroundWindow
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 02/06/2005

 
Public Function FnSetForegroundWindow(strWindowTitle As String) As Boolean
 
    Dim MyAppHWnd As Long
    Dim CurrentForegroundThreadID As Long
    Dim NewForegroundThreadID As Long
    Dim lngRetVal As Long
 
    Dim blnSuccessful As Boolean
 
    MyAppHWnd = FnFindWindowLike(strWindowTitle)
 
    If MyAppHWnd <> 0 Then
 
        'We've found the application window by the caption
            CurrentForegroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), ByVal 0&)
            NewForegroundThreadID = GetWindowThreadProcessId(MyAppHWnd, ByVal 0&)
 
        'AttachThreadInput is used to ensure SetForegroundWindow will work
        'even if our application isn't currently the foreground window
        '(e.g. an automated app running in the background)
            Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, True)
            lngRetVal = SetForegroundWindow(MyAppHWnd)
            Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, False)
 
        If lngRetVal <> 0 Then
 
            'Now that the window is active, let's restore it from the taskbar
            If IsIconic(MyAppHWnd) Then
                Call ShowWindow(MyAppHWnd, SW_RESTORE)
            Else
                Call ShowWindow(MyAppHWnd, SW_SHOW)
            End If
 
            blnSuccessful = True
 
        Else
 
            MsgBox "Found the window, but failed to bring it to the foreground!"
 
        End If
 
    Else
 
        'Failed to find the window caption
        'Therefore the app is probably closed.
        MsgBox "Application Window '" + strWindowTitle + "' not found!"
 
    End If
 
     FnSetForegroundWindow = blnSuccessful
 
End Function
 
Sub work()
    FnSetForegroundWindow ("C:\WINDOWS\system32\cmd.exe")
End Sub

Thank you very much-ur code very effective,,some concern.plz see

Hi sir, how are u..

Thank u very much for the code you provided.

Its working well.

I have some more questions to ask u .Now the code u gave it possible to switch over to that DOS like screen by running excel macro..

My next task is to retrieve some text from that screen.Like at some specific cursor position in that DOS like screen i wana check what is written.Like check at (2,3) cursor if written =1.

 

Can u plz suggest some code ...

Thank you.

JPH's picture

You could try to redirect the

You could try to redirect the output of your App (dos?) to a txt file and manipulate that.

If it is dos you're using:

The DOS command to redirect output is >. Use it like this:
Dir *.xls > c:\myfile\excel\MyXLSFiles.txt

 

Don't forget to build in a delay function because it takes time to create the file.

 

thank u very much JPH,but

thank u JPH,,,but i wana write a command in excel that automatically copy some text from that DOS like screen,,Its not actually dos.Its not an exe.

JPH's picture

Ok, first step is taken. You

Ok, first step is taken. You activate the screen.

Because I don't know which program you use it is hard to give an answer as to how you can "manipulate" content.

Did you try Google to find out what you can do to "manipulate" content via VBa?

Grtnx,

JPH

JPH's picture

not logged in

sry i was not logged in while placing the comment above.

Why doesn't my computer remember this.