Closing Notepad from VBA

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
20,601
Office Version
  1. 365
Platform
  1. Windows
I have some VBA that is generating two .TXT files and opening them up in Notepad.
Is there a way in VBA to close these specific instances of Notepad from VBA, which I want to do on the Workbook BeforeClose event.
Thanks.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello there

With an API function, you could do:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub CheckNotepad()
    Dim hwnd As Long
    hwnd = FindWindow("Notepad", vbNullString)
    If hwnd Then
        MsgBox "Notepad is running, the window handle is " & hwnd
    Else
        MsgBox "Notepad is not running"
    End If
End Sub

to check if Notepad is running. You can then use the API to close the window with that handle.
Or, store the window handle at the moment of opening the TXT file in Notepad (for example, in a hidden cell).
 
Upvote 0
How would I close Notepad? Can you give an example? Also, how would I close only the Notepad instances that were created from my program? In other words, if 3 Notepad instances were open and only 2 were created by my program, I only want to close those 2.
 
Upvote 0
How is the code opening Notepad? Is it using the Shell function - Shell("Notepad")? If so, convert the process id returned by Shell to a windows handle using Windows API functions and once you have the handle (hWnd) send a Close message to the window, again using the Windows API.
 
Upvote 0
Yes, follow the ideas posted by John_w. Like I said above, trace the Shell ID's and/or window handles of the opened Notepad instances.
That way you could close them with API coding.
 
Upvote 0
This is one more option which doesn't require API.
Code:
Public Sub CloseNotepad()
Dim oWdApp As Word.Application
Dim oTask As Task
Dim bCreate As Boolean

On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
On Error GoTo 0
If oWdApp Is Nothing Then
    Set oWdApp = CreateObject("Word.Application")
    oWdApp.Visible = False
    bCreate = True
End If

For Each oTask In oWdApp.Tasks
    'Provide a condition here which will match the notepad instances created by your code!
    If InStr(oTask.Name, "Notepad") > o Then
        oTask.Close
    End If
Next

If bCreate Then oWdApp.Quit

End Sub
 
Upvote 0
It is being opened with Shell:
Code:
Shell "notepad.exe " & fname, vbMaximizedFocus

Other people besides me are going to be using this so I need to close only those Notepad instances with the full path in the variable fname
 
Upvote 0
OK, the problem here is that if I knew how to do this, I wouldn't be asking.

You said this:

to check if Notepad is running. You can then use the API to close the window with that handle.
Or, store the window handle at the moment of opening the TXT file in Notepad (for example, in a hidden cell).

How do I use the API to close the exact instance of Notepad. Does each instance have a different handle? Is this how you keep track of which instance are open, not by filepath?

Please can I get a working example of how to close text files open in Notepad?

Thanks.
 
Upvote 0
If you have not tried the code I have posted then please try.

Full working example will be like this:
Code:
Public Sub Example()
Dim fname As String, strWindowName As String
fname = "C:\Documents and Settings\Administrator\Desktop\Hathway Login.txt"
Shell "notepad.exe " & fname, vbMaximizedFocus
strWindowName = Mid(fname, InStrRev(fname, Application.PathSeparator) + 1, 99)
CloseNotepad strWindowName
End Sub


Public Sub CloseNotepad(strWindowName As String)
Dim oWdApp As Object
Dim oTask As Object
Dim bCreate As Boolean

On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
On Error GoTo 0
If oWdApp Is Nothing Then
    Set oWdApp = CreateObject("Word.Application")
    oWdApp.Visible = False
    bCreate = True
End If

For Each oTask In oWdApp.Tasks
    'Provide a condition here which will match the notepad instances created by your code!
    If InStr(oTask.Name, strWindowName) > o Then
        oTask.Close
    End If
Next

If bCreate Then oWdApp.Quit

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,366
Messages
6,171,659
Members
452,416
Latest member
johnog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top