vba改动excel参数

将标题改为“新标题-工作薄1”

Sub new_title()
Application.Caption = "新标题"
End Sub

 

让excel的窗口始终显示在最前端

Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Public Sub Window_Top()         '调用API函数,实现窗口最前
    Dim WINWND As Long
    WINWND = FindWindow(vbNullString, Application.Caption)
    SetWindowPos WINWND, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

 

--------

The following is the codes from the Microsoft examples for using API.

Excel 设置为“总在前面”

下面的示例代码说明如何使 Microsoft Excel“总在前面”。这可以防止其他应用程序
显示在 Microsoft Excel 前面。

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
                            ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _
                            ByVal hWndInsertAfter As Long, _
                            ByVal x As Long, _
                            ByVal y As Long, _
                            ByVal cx As Long, _
                            ByVal cy As Long, _
                            ByVal wFlags As Long) As Long
Global Const HWND_TOPMOST = -1
Global Const HWND_NOTOPMOST = -2

Sub SetOnTop()
    Dim WinHnd As Long, SUCCESS As Long
    WinHnd = FindWindow("xlmain", Application.Caption)
    SUCCESS = SetWindowPos(WinHnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
    '下面一行只是为了 20 秒之后将 Excel 切换回正常操作状态
    Application.OnTime Now + TimeValue("00:00:20"), "NotOnTop"
End Sub

Sub NotOnTop()
    Dim WinHnd As Long, SUCCESS As Long
    WinHnd = FindWindow("xlmain", Application.Caption)
    SUCCESS = SetWindowPos(WinHnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
End Sub


------------

The following is the codes from the Microsoft examples for using API.

Excel 设置为“总在前面”

下面的示例代码说明如何使 Microsoft Excel“总在前面”。这可以防止其他应用程序
显示在 Microsoft Excel 前面。

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
                            ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _
                            ByVal hWndInsertAfter As Long, _
                            ByVal x As Long, _
                            ByVal y As Long, _
                            ByVal cx As Long, _
                            ByVal cy As Long, _
                            ByVal wFlags As Long) As Long
Global Const HWND_TOPMOST = -1
Global Const HWND_NOTOPMOST = -2

Sub SetOnTop()
    Dim WinHnd As Long, SUCCESS As Long
    WinHnd = FindWindow("xlmain", Application.Caption)
    SUCCESS = SetWindowPos(WinHnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
    '下面一行只是为了 20 秒之后将 Excel 切换回正常操作状态
    Application.OnTime Now + TimeValue("00:00:20"), "NotOnTop"
End Sub

Sub NotOnTop()
    Dim WinHnd As Long, SUCCESS As Long
    WinHnd = FindWindow("xlmain", Application.Caption)
    SUCCESS = SetWindowPos(WinHnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值