|
楼主 |
发表于 2022-9-13 18:30
|
显示全部楼层
本帖最后由 pxt2001 于 2022-9-13 18:43 编辑
感谢楼上回复,我通过保存数据到注册表,近似解决了此问题。
1、虽然读取不到当前导航窗格宽度,但可以保存数据到注册表,打开Word时读取注册表数据,来设置导航窗格宽度。
2、打开Word后,编写一个带窗体的宏,接收用户输入数据,点击确认按钮,保存数据到注册表。
效果如下:
代码如下(VBA新手,高手见笑了。但代码的功能有点意思,搜索全网没找到类似功能):
'两个函数:读写用户窗体位置到注册表
Public Function Getwz_form()
Dim wz, arr
wz = GetSetting("MyApp", "Startup", "wz_窗体位置", "100-100")
If (wz Like "*-*") = False Then
wz = "100-100"
End If
arr = Split(wz, "-")
Getwz_form = arr
End Function
Public Function Setwz_form(s)
SaveSetting "MyApp", "Startup", "wz_窗体位置", s
End Function
'启动Word时,读取注册表,设置导航栏宽度
Sub AutoOpen()
'Word导航窗格的宽度调整后怎么固定住,下次就不用再调了?
wid = GetSetting("MyApp", "Startup", "wid_导航栏宽度", "260-1")
If (wid Like "*-*") = False Then
wid = "260-1"
End If
arr = Split(wid, "-")
wid = arr(0) 'change to suit
With Application.CommandBars("Navigation")
.Visible = arr(1)
.Width = wid
End With
End Sub
'主程序
Sub 导航栏宽度()
UserForm3_导航栏宽度.Show 0
End Sub
'用户窗体代码
Private Sub UserForm_Initialize()
arr = Getwz_form()
UserForm3_导航栏宽度.left = Val(arr(0))
UserForm3_导航栏宽度.top = Val(arr(1))
'读取注册表内容
wid = GetSetting("MyApp", "Startup", "wid_导航栏宽度", "260-1")
If (wid Like "*-*") = False Then
wid = "260-1"
End If
arr = Split(wid, "-")
TextBox1.Value = Val(arr(0))
CheckBox1.Value = arr(1)
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Sub CommandButton1_Click() '点击确认按钮'
Dim s As String
If CheckBox1.Value = True Then
vis = "1"
Else
vis = "0"
End If
s = TextBox1.Text & "-" & vis
SaveSetting "MyApp", "Startup", "wid_导航栏宽度", s
s = CStr(Me.left) & "-" & CStr(Me.top)
Call Setwz_form(s)
'MsgBox "文字框输入的内容是:" + TextBox1.Value + TextBox2.Value
'保存变量值到注册表
'UserForm_导航栏宽度.hide
With Application.CommandBars("Navigation")
.Visible = vis
.Width = Val(TextBox1.Text)
End With
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
'TextBox1_Enter无效
' MsgBox ("进入了文本框1")
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Sub CommandButton2_Click() '点击取消按钮
s = CStr(Me.left) & "-" & CStr(Me.top)
Call Setwz_form(s)
Unload UserForm3_导航栏宽度
End Sub
Private Sub UserForm_Click()
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
|
|