|
-
Mar 4th, 2002, 11:55 AM
#1
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|