word文档添加返回目录悬浮按钮的方法

2025-10-26 20:30:10

1、ALT+F11打开VBE编辑器,在工程窗口右键-插入-用户窗体,插入一个新窗体UserForm1。

word文档添加返回目录悬浮按钮的方法

2、在窗体属性对话框将:ShowModel属性设为False即将窗体设为无模态窗体,BorderStyle属性设为0-fmBorderStyleNone即将窗体设为无边框,StartUpPosition属性设为0-手动即将窗体的初始显示位置设置成手动更改,其余属性请按需更改。

word文档添加返回目录悬浮按钮的方法

3、利用标签控件在窗体上拖拉出一个标签Label1。

word文档添加返回目录悬浮按钮的方法

4、将标签Label1的Caption属性设为返回目录,BorderStyle属性设为0-fmBorderStyleNone,其余属性按需设置即可。

word文档添加返回目录悬浮按钮的方法

5、在窗体UserForm1代码窗口粘贴入下代码:

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

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

Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Sub ReleaseCapture Lib "user32" ()

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const GWL_STYLE As Long = (-16)

Private Const WS_CAPTION As Long = &HC00000

Private Const WM_NCLBUTTONDOWN = &HA1

Private Const HTCAPTION = 2

Private Sub Label1_Click()

Selection.HomeKey unit:=wdStory '返回文档开头

Selection.MoveDown unit:=wdLine, Count:=9 '笔者此处演示文档,目录位置是位于文档开头往下数9行,具体可以根据需要自行更改

End Sub

Private Sub UserForm_Initialize()

    Dim lngStyle As Long

    Dim hWnd As Long

    hWnd = FindWindow(vbNullString, Me.Caption)

    lngStyle = GetWindowLong(hWnd, GWL_STYLE)

    SetWindowLong hWnd, GWL_STYLE, lngStyle And Not WS_CAPTION

    DrawMenuBar hWnd

    Me.Height = 31.5

    Me.Left = Selection.Information(wdHorizontalPositionRelativeToPage) + 545

    Me.Top = Selection.Information(wdVerticalPositionRelativeToPage) + 50

End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Dim hWnd As Long

    hWnd = FindWindow(vbNullString, Me.Caption)

    ReleaseCapture

    SendMessage hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&

End Sub

word文档添加返回目录悬浮按钮的方法

6、在ThisDocument代码窗口粘贴如下代码:

Private Sub Document_Open()

UserForm1.Show

End Sub

word文档添加返回目录悬浮按钮的方法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢