VBA API声明。 无论应用程序如何,都可以将窗口置于前面
我正试图把Excel窗口带到不pipe运行的所有应用程序的前面。
目前的代码,
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()
方法需要一个string参数,并且它将激活(即将它带到前面)包含该确切string的任何窗口。
更具体到你的问题,但你需要了解如何在VBA中的API工作多一点 – 如果你使用的是64位系统,那么你需要使用条件编译,并使用PtrSafe
关键字声明API函数为指针安全LongPtr
数据types:
#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
经过多一点研究,find了我想要做的答案。
这会将您指定的工作表放在前面。
Public Declare Function SetForegroundWindow _ Lib "user32" (ByVal hwnd As Long) As Long Public Sub Bring_to_front() Dim setFocus As Long ThisWorkbook.Worksheets("Sheet1").Activate setfocus = SetForegroundWindow(Application.hwnd) End Sub