Close Notepad-VBForums
Results 1 to 4 of 4

Thread: Close Notepad

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    35

    Close Notepad

    Hi

    I am using a Shell Command in Excel Macro to open the notepad.
    Shell "Notepad.exe " & "C:\test1.txt"

    I want to check if this file is open, if its open then I do not want to open the notepad again.

    How can I check if the notepad is already open.

    Or atleast can anyone tell me how do I close the notepad from Excel Macro.

  2. #2
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Close Notepad

    I'll answer both questions... take your pick

    I want to check if this file is open, if its open then I do not want to open the notepad again.
    vb Code:
    1. Sub CheckIfFileisOpen()
    2.     Dim curr_line As String
    3.    
    4.     On Error GoTo FILE_OPEN_ERR
    5.    
    6.     Open "C:\test1.txt" For Input As #1
    7.     Open "C:\test1.txt" For Input As #1
    8.      
    9.     Close #1
    10.  
    11.    Exit Sub
    12.  
    13. FILE_OPEN_ERR:
    14.    If Err = 55 Then
    15.       MsgBox "FILE IS OPEN"
    16.    End If
    17. End Sub


    How can I check if the notepad is already open.
    vb Code:
    1. Sub CheckIfNotepadisOpen()
    2.     On Error Resume Next
    3.     strComputer = "."
    4.     Set objWMIService = GetObject("winmgmts:" _
    5.     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    6.    
    7.     Set colItems = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'notepad.exe'")
    8.  
    9.     If colItems.Count > 0 Then
    10.         MsgBox "Notepad is open."
    11.     Else
    12.         MsgBox "Notepad is not open."
    13.     End If
    14. End Sub
    Last edited by Siddharth Rout; Nov 19th, 2008 at 07:15 AM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    35

    Re: Close Notepad

    Thanks for the reply.

    The code below "CheckIfNotepadisOpen" would help me find if there are any notepads open.
    But I want find if C:\test1.txt is open or not.

    How can I do that?

  4. #4
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Close Notepad

    But I want find if C:\test1.txt is open or not.
    see the code above that...

    Also to answer your third question in post 1
    Or atleast can anyone tell me how do I close the notepad from Excel Macro.
    vb Code:
    1. Private Const WM_CLOSE = &H10
    2. Private Const INFINITE = &HFFFFFFFF
    3.  
    4. Private Declare Function apiPostMessage _
    5.     Lib "user32" Alias "PostMessageA" _
    6.     (ByVal hWnd As Long, _
    7.     ByVal wMsg As Long, _
    8.     ByVal wParam As Long, _
    9.     lParam As Any) _
    10.     As Long
    11.  
    12. Private Declare Function apiFindWindow _
    13.     Lib "user32" Alias "FindWindowA" _
    14.     (ByVal lpClassName As String, _
    15.     ByVal lpWindowName As String) _
    16.     As Long
    17.    
    18. Private Declare Function apiWaitForSingleObject _
    19.     Lib "kernel32" Alias "WaitForSingleObject" _
    20.     (ByVal hHandle As Long, _
    21.     ByVal dwMilliseconds As Long) _
    22.     As Long
    23.    
    24. Private Declare Function apiIsWindow _
    25.     Lib "user32" Alias "IsWindow" _
    26.     (ByVal hWnd As Long) _
    27.     As Long
    28.        
    29. Private Declare Function apiGetWindowThreadProcessId _
    30.     Lib "user32" Alias "GetWindowThreadProcessId" _
    31.     (ByVal hWnd As Long, _
    32.     lpdwProcessID As Long) _
    33.     As Long
    34.        
    35. Function fCloseApp(lpClassName As String) As Boolean
    36.  
    37. Dim lngRet As Long, hWnd As Long, pID As Long
    38.  
    39.     hWnd = apiFindWindow(lpClassName, vbNullString)
    40.     If (hWnd) Then
    41.         lngRet = apiPostMessage(hWnd, WM_CLOSE, 0, ByVal 0&)
    42.         Call apiGetWindowThreadProcessId(hWnd, pID)
    43.         Call apiWaitForSingleObject(pID, INFINITE)
    44.         fCloseApp = Not (apiIsWindow(hWnd) = 0)
    45.     End If
    46. End Function
    47.  
    48. Sub CloseNotepad()
    49.     a = fCloseApp("Notepad")
    50. End Sub
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.