我正在尝试将 Excel 窗口置于所有正在运行的应用程序的前面。
当前代码,
Private Declare Function SetForegroundWindow _
Lib "user32" _
(ByVal hWnd As Long) As Long
Public Sub Bring_to_front()
SetForegroundWindow wb.Application.hWnd
End Sub
Sub Test()
Set wb = Workbooks("MyWorkBook.xlxs")
call Bring_to_front
End Sub
目前什么也没有发生。
最佳答案
您不需要为此使用 API,您可以使用以下内容:
Sub BringXLToFront()
AppActivate Application.Caption
End Sub
VBA 中的 AppActivate()
方法采用字符串参数,它将激活(即将其置于前面)包含该<的任何窗口em>精确字符串。
更具体地说,您需要更多地了解 API 在 VBA 中的工作原理 - 如果您使用的是 x64 系统,那么您需要使用条件编译并使用以下命令将 API 函数声明为指针安全的: PtrSafe
关键字和 LongPtr
数据类型:
#If Win64 Then
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As LongPtr) As LongPtr
#Else
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As Long
#End If
关于excel - VBA API 声明。无论应用程序如何,将窗口置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481674/