Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Notepad

Status
Not open for further replies.

simoncpage

Programmer
Apr 4, 2002
256
GB
How can I open a textfile from a textbox entry (using notpad) and on exit delete this file?

Any help would be appreciated, Thanks

Simon
 
Simply use the Shell to open the file, wait for it to end, and the kill to delete it using the path in your textbox.
For the waiting part, search the site and you'll find an example.
Hope this helps.
 
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&
Dim sPrgm As String
Dim lProcID As Long
Dim hProc As Long

Private Sub Command1_Click()

RunUntilFinished sPrgm

Kill (Text1.Text)
MsgBox (Text1.Text & " has been deleted!"), vbInformation

End Sub

Public Sub RunUntilFinished(ByVal sApp As String)

' Start the App
On Error GoTo ErrHndlr
lProcID = Shell("C:\WINNT\NOTEPAD.EXE " & UCase(Text1.Text), vbNormalFocus)
On Error GoTo 0

DoEvents

' Wait for the App
hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
If hProc <> 0 Then
WaitForSingleObject hProc, INFINITE
CloseHandle hProc
End If
Exit Sub

ErrHndlr:
MsgBox &quot;Error starting App:&quot; & vbCrLf & _
&quot;App: &quot; & sApp & vbCrLf & _
&quot;Err Desc: &quot; & Err.Description
Err.Clear
End Sub Swi
 
Right the shell thing is pretty straightforward now I have a more trick problem....how to open up excel.

I am using the following code
-----------------------------------------------
Sub GetExcel()
Dim MyXL As Object
Dim ExcelWasNotRunning As Boolean

Set MyXL = GetObject(, &quot;Excel.Application&quot;)
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

DetectExcel

Set MyXL = GetObject(&quot;&quot; & frmMain.Text1(2).Text & &quot;&quot;)

MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True

If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing

End Sub

Sub DetectExcel()

Const WM_USER = 1024
Dim hWnd As Long

hWnd = FindWindow(&quot;XLMAIN&quot;, 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else

SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub

--------------------------------------------------
The problem is that it only opens up excel and shows you the sheet it wont let me edit any cells - how can I change this so I can???? (If I replace excel with word and open a word document its fine?)

Any help would be great, thanks

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top