vba改动excel参数

来源:互联网 发布:目前比较火的 js 框架 编辑:程序博客网 时间:2024/05/16 10:23

将标题改为“新标题-工作薄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