Excel 2010 VBA Macro to close specific notepad window. (findwindow gives mismatch type error??)

StackOverflow https://stackoverflow.com/questions/21239394

  •  30-09-2022
  •  | 
  •  

سؤال

So I'm experimenting with closing windows/apps in a vba macro I'm writing. Numerous posts online suggest the use of FindWindow from the windows api.

I'm starting out slow, I'm trying to close a notepad window from my macro. (its one of a few things I want but it seems the easiest) and I found a solution, I forget where it was posted, it was short and sweet.

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

seems great, its the start of how to get the handle for the notepad window, and from there I could find code to close the window using that handle. But it doesn't work for me!

All of the examples I have found use the 2 arguments, I would say half of which use vbNullString as the second, some of which use a string for the caption of a particular window. But none of it works for me.

The only thing that works is supplying a 0 for the second argument (I assume this is in place of vbNullString) but if I try to use vbnullstring, or an actual string, I get a type mismatch error. and if I try to use any other number then excel stops working. (the lovely "Program has stopped working" message appears.

So, can give me some advise as to why this happens? I could just use the 0, but I'm guessing my user will have other notepad windows open so I'd like to be able to supply an argument to find the specific notepad window. (its going to be a temp window, popped up during the macro and subsequently closed as I do not want it left open for them to see)

Thanks in advance

هل كانت مفيدة؟

المحلول 2

I just figured out the solution, and it'd so dumb I want to cry. Today is most certainly monday.

When I put in the declaration, (I can't for the life of me figure out how it happened as I found where i got the code and it did not have the error) I declared the api with:

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

anyone see the issue? I put As Long next to ByVal lpWindowName when it obviously should be string. UGH!

As an aside, I found an interesting articule about windows 7 32 and 64-bit / VBA7 compatibility which I'd like to post. Apparently most api functions in windows 7 64-bit should be declared using ptrSafe. so for example, if I was usign 64-bit I should declare that api as:

Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

heres a link to where I found that: http://msdn.microsoft.com/en-us/library/ff700513(v=office.11)

that one mentions VBA7, this one mentions 32 vs 64-bit windows: http://www.jkp-ads.com/articles/apideclarations.asp

Thanks much for any views and considerations!

نصائح أخرى

I think you forgot to declare the API. Try the following (in a Module)

Public 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", "HOUSE.txt - Notepad")
If hwnd Then
    MsgBox "Notepad is running, the window handle is " & hwnd
Else
    MsgBox "Notepad is not running"
End If
End Sub

This will give you the handle for HOUSE.txt file opened in notepad.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top