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.
 
In your code, you have this:

'Provide a condition here which will match the notepad instances created by your code!

What do I put there to identify each instance?
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Shell returns the processID you need, so store that and then when you want to terminate it, pass it to this sub:
Code:
Sub FindAndTerminateProcID(ByVal lProc As Long)
   Dim objWMIService, objProcess, colProcess
   Dim strComputer
   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _
                                 & "{impersonationLevel=impersonate}!\\" _
                                 & strComputer & "\root\cimv2")
   Set colProcess = objWMIService.ExecQuery _
                    ("Select * from Win32_Process Where ProcessID = " & lProc)
   If colProcess.Count > 0 Then
      For Each objProcess In colProcess
         objProcess.Terminate
      Next objProcess
   End If
End Sub
 
Upvote 0
In your code, you have this:

'Provide a condition here which will match the notepad instances created by your code!

What do I put there to identify each instance?
You have a working solution from Rory.

I have given complete example with code usage in post #10. In that example, I have shown the way to use fname [variable from your code] and its usage in the code I've posted.
 
Upvote 0
Very nice code, was looking exactly for somehting like this. Do you know how can use Save before closing the file ? I tried

oTask.Save
oTask.SaveAs Filename

But it is not working and getting error

THanks for anyone who can help :)

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
Try this to close already certain Notepad
collected and modified

Code:
Sub test()

Dim StrPath As String
StrPath = "C:\New folder\Test.txt" ' Please type your [U][B]File FullName[/B][/U]
Call Close_Notepad_ByName(StrPath)
End Sub
Public Sub Close_Notepad_ByName(NtpPath As String)

Dim oServ As Object
Dim cProc As Object
Dim oProc As Object
StrProcessName = "Notepad.exe"
Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("select * from win32_process")
For Each oProc In cProc
    If InStr(1, oProc.Name, StrProcessName, vbTextCompare) <> 0 Then' check if Notepad
        If InStr(1, oProc.CommandLine, NtpPath, vbTextCompare) <> 0 Then ' check Path
        oProc.Terminate
        End If
    End If
Next
Set oServ = Nothing
Set cProc = Nothing
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