利用VBA给word文档添加一个悬浮按钮,只需鼠标单击该按钮就能跳转回文档的目录。
工具/原料
word2003
vba
方法/步骤
1、ALT+F11打开VBE编辑器,在工程窗口右键-插入-用户窗体,插入一个新窗体UserForm1。
2、在窗体属性对话框将:ShowModel属性设为False即将窗体设为无模态窗体,B艺皱麾酪orderStyle属性设为0-fmBorde鸡堕樱陨rStyleNone即将窗体设为无边框,StartUpPosition属性设为0-手动即将窗体的初始显示位置设置成手动更改,其余属性请按需更改。
3、利用标签控件在窗体上拖拉出一个标签Label1。
4、将标签Label1的Caption属性设为返回目录,BorderStyle属性设为0-fmBorderStyleNone,其余属性按需设置即可。
5、在窗体UserForm1代码窗口粘贴入下代码:PrivateDeclareFu艘绒庳焰nctionGetWindowLongL足毂忍珩ib"user32"Alias"GetWindowLongA"(ByValhWndAsLong,ByValnIndexAsLong)AsLongPrivateDeclareFunctionSetWindowLongLib"user32"Alias"SetWindowLongA"(ByValhWndAsLong,ByValnIndexAsLong,ByValdwNewLongAsLong)AsLongPrivateDeclareFunctionFindWindowLib"user32"Alias"FindWindowA"(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLongPrivateDeclareFunctionDrawMenuBarLib"user32"(ByValhWndAsLong)AsLongPrivateDeclareSubReleaseCaptureLib"user32"()PrivateDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"(ByValhWndAsLong,ByValwMsgAsLong,ByValwParamAsLong,lParamAsAny)AsLongPrivateConstGWL_STYLEAsLong=(-16)PrivateConstWS_CAPTIONAsLong=&HC00000PrivateConstWM_NCLBUTTONDOWN=&HA1PrivateConstHTCAPTION=2PrivateSubLabel1_Click()Selection.HomeKeyunit:=wdStory'返回文档开头Selection.MoveDownunit:=wdLine,Count:=9'笔者此处演示文档,目录位置是位于文档开头往下数9行,具体可以根据需要自行更改EndSubPrivateSubUserForm_Initialize()DimlngStyleAsLongDimhWndAsLonghWnd=FindWindow(vbNullString,Me.Caption)lngStyle=GetWindowLong(hWnd,GWL_STYLE)SetWindowLonghWnd,GWL_STYLE,lngStyleAndNotWS_CAPTIONDrawMenuBarhWndMe.Height=31.5Me.Left=Selection.Information(wdHorizontalPositionRelativeToPage)+545Me.Top=Selection.Information(wdVerticalPositionRelativeToPage)+50EndSubPrivateSubLabel1_MouseMove(ByValButtonAsInteger,ByValShiftAsInteger,ByValXAsSingle,ByValYAsSingle)DimhWndAsLonghWnd=FindWindow(vbNullString,Me.Caption)ReleaseCaptureSendMessagehWnd,WM_NCLBUTTONDOWN,HTCAPTION,0&EndSub
6、在ThisDocument代码窗口粘贴如下代码:PrivateSubDocument_Open()UserForm1.ShowEndSub