简单地做了一下,请楼主看一下是否合适: '* +++++++++++++++++++++++++++++++++++++++ '* Created By 守柔(ShouRou)@ExcelHome 2005-1-17 6:34:52 '仅测试于System: Windows NT Word: 10.0 Language: 2052 '^The Code CopyIn [用户窗体-UserForm1]^' '* -------------------------------------------------------------------------- Private Sub CommandButton1_Click()
Dim Doc As Document, Bk As String
'以隐藏窗口方式打开指定文档
Set Doc = Documents.Open(FileName:=ActiveDocument.AttachedTemplate.Path & "\样本文档.doc", _
Visible:=False)
Bk = Me.TextBox1 '定义一个BK string值
If Doc.Bookmarks.Exists(Bk) = True Then '如果找到该书签
Me.Label3 = Doc.Bookmarks(Bk).Range.Text '则显示其书签对应文本内容
Me.TextBox1.SetFocus
Else
'提示并清空文本框\标签框
MsgBox Bk & "不是有效书签名,请重新输入!", vbOKOnly + vbInformation
Me.TextBox1 = ""
Me.Label3 = ""
Me.TextBox1.SetFocus '设置焦点
Me.CommandButton1 = False
End If
End Sub
'----------------------
Private Sub CommandButton2_Click()
On Error Resume Next
Unload Me '卸载窗体
'关闭指定文档
Documents(ActiveDocument.AttachedTemplate.Path & "\样本文档.doc").Close False
End Sub
'----------------------
Private Sub TextBox1_Change()
If Len(Me.TextBox1) > 4 Then '设置一个小动作,如果书签名长度不满5位
Me.CommandButton1.Enabled = True
Me.CommandButton1.Default = True
Else
Me.CommandButton1.Enabled = False
End If
End Sub
'----------------------
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Me.TextBox1) > 4 Then Me.CommandButton1.Value = True
End Sub
'----------------------
Private Sub UserForm_Initialize()
Me.CommandButton1.Enabled = False '确定按钮无效
Me.TextBox1.SetFocus '文本框设置焦点
End Sub
'---------------------- |