Results 1 to 4 of 4

Thread: Switching between programs

  1. #1
    Joe_B
    Guest

    Switching between programs

    I am having trouble switching between 2 programs. I created a program that opens up another program using ShellExecute and then sends it keystrokes to open files and save them in a different format.

    The problem that I'm having is that when I check to see if the file already exists before saving it, I switch back to my program and throw a message up asking the user if they want to override the current file.

    This is where I'm having problems. I can't seem to switch back to the other program. I've tried a few API's SetForegroundWindow but can't seem to get it to work.

    Here's my code:

    Private 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
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long


    Public Sub ChangeFileFormat(sLocation As String)
    Dim sFile As String
    Dim sNewFile As String
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As Integer
    Dim lClaris As Long
    Dim fso As FileSystemObject
    Dim bDupFile As Boolean

    'Initialize object
    Set fso = New FileSystemObject
    bDupFile = False

    'Set file variable to first CWK file in directory
    sFile = Dir(sLocation & "*.cwk")
    'Open up first file
    If sFile <> "" Then
    lClaris = ShellExecute(frmRegImport.hwnd, vbNullString, "clworks.exe", vbNullString, "C:\claris\", 1)
    'Set the focus to Claris
    'AppActivate ("ClarisWorks 1.0")
    Sleep (5000)
    SetForegroundWindow lClaris
    Sleep (2000)
    'Hit the Escape key to remove the startup window
    SendKeys "{ESC}", True
    Sleep (2000)
    End If


    '********************************************************************
    'Run loop until all of the CWK files in the working directory have been processed
    While sFile <> ""
    sNewFile = Mid(sFile, 1, Len(sFile) - 3) & "xls"

    'Check to see if the file is a regular file
    If UCase(Mid(sFile, 1, 2)) = "RG" Then
    'Open up first file into ClarisWorks
    SendKeys "^o", True
    Sleep (2000)
    'Enter the filename of the file
    SendKeys sLocation & sFile, True
    Sleep (2000)

    SendKeys "{ENTER}", True
    Sleep (2000)

    'Check to see if the file already exists
    If fso.FileExists(sNewFile) Then
    'Set the focus back to the vb form
    BringWindowToTop (frmRegImport.hwnd)
    SetForegroundWindow frmRegImport.hwnd
    iAnswer = MsgBox("The file " & sNewFile & " already exists." & vbCrLf & _
    "Overwrite?", vbExclamation + vbYesNo, "File Already Exists")
    bDupFile = True
    'Set focus back to Claris
    BringWindowToTop (lClaris)
    SetForegroundWindow lClaris
    Else
    bDupFile = False
    End If

    'Send response if duplicate file
    If bDupFile Then
    'Send keys based on users response to msgbox
    If iAnswer = vbYes Then
    'Open the "Save As" window
    SendKeys "+^s", True
    Sleep (2000)
    'Replace the current filename with an Excel filename
    SendKeys sNewFile, True
    Sleep (2000)
    'Submit the new file and save it
    SendKeys "{ENTER}", True
    Sleep (2000)
    'Select yes to save file
    SendKeys "%y", True
    End If
    Sleep (2000)
    Else
    'Open the "Save As" window
    SendKeys "+^s", True
    Sleep (2000)
    'Replace the current filename with an Excel filename
    SendKeys sNewFile, True
    Sleep (2000)
    'Submit the new file and save it
    SendKeys "{ENTER}", True
    Sleep (2000)
    End If

    'Close the ClarisWorks file
    SendKeys "^w", True
    sFile = Dir
    Sleep (2000)

    End If
    Wend

    'Close ClarisWorks
    SendKeys "^q", True

    AppActivate ("Timesheet_Import")

    'Remove object from memory
    Set fso = Nothing
    End Sub

  2. #2
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    Hi,

    i guess you are not able to set the window back to the foreground...
    try this piece of code to put the desired window to focus..


    PHP Code:
    Public Sub ShowPrevInstance(OldTitle as String)
        
    On Error Resume Next
        Dim ll_WindowHandle 
    As Long
        
        ll_WindowHandle 
    FindWindow("ThunderRT5Main"OldTitle)
        
        If 
    ll_WindowHandle 0 Then Exit Sub
        
        ll_WindowHandle 
    GetWindow(ll_WindowHandleGW_HWNDPREV)
        
        
    Call OpenIcon(ll_WindowHandle)
        
        
    Call SetForegroundWindow(ll_WindowHandle)
            
    End Sub 
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  3. #3
    Joe_B
    Guest
    Can you give me a little more detail about the API calls. If it's possible, I would like to know how the code you wrote brings the window to the foreground.

  4. #4
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    Hi,

    Sorry buddy...

    There is one thing to be considered while getting a window to foreground..

    1) is the window minimised???

    For this case Use OpenIcon api

    The OpenIcon function restores a minimized (iconic) window to its previous size and position; it then activates the window.

    Now if you use SetForegroundWindow() api.. will make your job..
    There are other api's like BringWindowToTop() SetWindowPos().. etc etc.. i couldnt make this functionality.. .. but this was the final code .. which helped me..

    sorry for the previous code. As ThunderRT5Main works for VB 5.0 and you may have to use ThunderRT6Main for VB 6.0.

    Using GetWindow()api.. would help you keep only single instance of app to be running.. ) sorry.. the previous code was to keep a single instance.. Some times i give different answer to different question..)

    please try the following code

    PHP Code:
    Public Sub getWindotoTop(OldTitle as String)
        
    On Error Resume Next
        Dim ll_WindowHandle 
    As Long
        
        ll_WindowHandle 
    FindWindow(vbNullStringOldTitle)
        
        If 
    ll_WindowHandle 0 Then Exit Sub
        
        Call OpenIcon
    (ll_WindowHandle)
        
        
    Call SetForegroundWindow(ll_WindowHandle)
            
    End Sub 
    Last edited by pradeepkrao; Mar 5th, 2002 at 11:33 PM.
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

Posting Permissions

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



Click Here to Expand Forum to Full Width