用于关闭特定记事本窗口的 Excel 2010 VBA 宏。(findwindow 给出了不匹配类型错误??)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21239394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:28:03  来源:igfitidea点击:

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

vbaexcel-2010findwindow

提问by user1759942

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

所以我正在尝试在我正在编写的 vba 宏中关闭窗口/应用程序。网上的许多帖子都建议使用FindWindow来自 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 vbNullStringas the second, some of which use a string for the caption of a particular window. But none of it works for me.

我发现的所有示例都使用了 2 个参数,我会说其中一半vbNullString用作第二个参数,其中一些使用字符串作为特定窗口的标题。但它对我来说都不起作用。

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.

唯一有效的方法是为第二个参数提供 0(我假设它代替了vbNullString),但是如果我尝试使用vbnullstring或实际字符串,则会出现类型不匹配错误。如果我尝试使用任何其他数字,那么 excel 将停止工作。(出现可爱的“程序已停止工作”消息。

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)

那么,可以给我一些关于为什么会发生这种情况的建议吗?我可以只使用 0,但我猜我的用户会打开其他记事本窗口,所以我希望能够提供一个参数来找到特定的记事本窗口。(它将是一个临时窗口,在宏期间弹出并随后关闭,因为我不希望它保持打开状态让他们看到)

Thanks in advance

提前致谢

回答by Pankaj Jaju

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

我想你忘了声明 API。尝试以下操作(在模块中)

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.

这将为您提供在记事本中打开的 HOUSE.txt 文件的句柄。

回答by user1759942

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:

当我输入声明时,(我一生都无法弄清楚它是如何发生的,因为我找到了代码的位置并且它没有错误)我声明了 api:

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

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 Longnext to ByVal lpWindowNamewhen it obviously should be string. UGH!

有人看到这个问题吗?当它显然应该是字符串时,我把它放在As Long旁边ByVal lpWindowName。啊!

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:

顺便说一句,我发现了一篇关于 windows 7 32 和 64 位/VBA7 兼容性的有趣文章,我想发布它。显然,Windows 7 64 位中的大多数 api 函数都应该使用ptrSafe. 因此,例如,如果我使用 64 位,我应该将该 api 声明为:

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

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)

这是我发现的链接: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

那个提到了 VBA7,这个提到了 32 位和 64 位窗口:http://www.jkp-ads.com/articles/apideclarations.asp

Thanks much for any views and considerations!

非常感谢您的任何意见和考虑!