打开单词文档时,我希望能够检查文档中是否存在特定单词。如果确实如此,我想打开一个用户窗体。基本上,我有一个带有“姓氏”的模板字母来代替收件人的姓氏。当我打开文档时,我希望自动弹出一个用户窗体,以便我可以在文本框中输入该用户的姓,并在单击用户窗体上的“完成”时运行查找和替换功能。如果我已经用姓氏的姓氏替换了“姓氏”,那么我不希望用户窗体弹出。我知道如何完成一切,除了检查是否存在“lastname”。有没有办法做到这一点?VBA查找文档中是否有特定单词
-1
A
回答
0
在 “的ThisDocument” 代码窗格地方
这个(注释)代码:
Option Explicit
Private Sub Document_Open()
Dim lastNameRng As Range
Set lastNameRng = GetLastname(ActiveDocument, "lastname") '<--| set 'lastNameRng' range to the one in the active document containing "lastname"
If lastNameRng Is Nothing Then Exit Sub '<--| exit if active document doesn't contain "lastname"
With UserForm2 '<--| change "UserForm2" to your actual userform name
With .TextBox1 '<--| change "TextBox1" to your actual TextBox name
.Value = "lastname" '<--| default value
.SetFocus '<--| make textbox the active control
.SelStart = 0 '<--| set the textbox selected text start from the beginning of the textbox text
.SelLength = Len(.Text) '<--| set the textbox selected text length as the textbox text one
End With
.Show '<--| show the userform and let the user input its text
lastNameRng.Text = .TextBox1.Value '<--| change "lastname" to the validated user input in TextBox1 (change "TextBox1" to your actual TextBox name)
End With
Unload UserForm2
End Sub
Private Function GetLastname(doc As Document, strng As String) As Range
Dim myRange As Range
Set myRange = ActiveDocument.Content '<--| set 'myRange' to passd dcoument entire content
myRange.Find.Execute FindText:=strng, MatchCase:=True, MatchWholeWord:=True, Forward:=True '<--| set 'myRange' to the one containing passed string in the passed document
If myRange.Find.Found = True Then Set GetLastname = myRange '<--| if 'myRange' has been actually set the return it
End Function
在用户窗体代码窗格将下面的代码:
Option Explicit
Private Sub CommandButton1_Click() '<--| change "CommandButton1" to your actual "Done" button name
If Not ValidateInput(Me.TextBox1) Then Exit Sub '<--| exit if invalid input in TextBox1 (change "TextBox1" to your actual textbox name)
Me.Hide
End Sub
Function ValidateInput(tb As MSForms.TextBox) As Boolean
With tb '<--| reference passed textbox
If Trim(.Value) = "" Then '<--| if its content is empty...
MsgBox "You must enter a last name !", vbExclamation + vbInformation '<--| inform the user
.SetFocus '<--| make textbox the active control
.Value = "lastname" '<--| set the "default" textbox text
.SelStart = 0 '<--| set the textbox selected text start from the beginning of the textbox text
.SelLength = Len(.Text) '<--| set the textbox selected text length as the textbox text one
Else
ValidateInput = True
End If
End With
End Function
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then '<--| don't let the user close the userform by clicking the white cross at its top left
MsgBox "Click the 'Done' button to close the form"
Cancel = True
End If
End Sub
+0
非常感谢你,完美的作品! – Jim
+0
不客气。然后请将答案标记为已接受。谢谢。 – user3598756
相关问题
- 1. 检查Word文档中的特定单词是否为粗体?
- 2. 查找单词的英文定义VBA
- 3. 在xhtml文档中查找特定单词的最快方法
- 4. 如何在Python中搜索文档以查找特定单词?
- 5. C#查找单词是否在文档中
- 6. 如何在word文档中查找特定单词并粘贴使用vba代码的单词?
- 7. 使用sklearn查找文档中特定单词的tf-idf得分
- 8. BeautifulSoup查找包含特定单词
- 9. 如何查找文档是否以solr中的给定词语开头?
- 10. 查找单词的文件,检查它们是否回文
- 11. 查找一个文件中开头的行特定的单词
- 12. 在文本块中查找特定单词的最大簇
- 13. 用于查找文件名中特定单词的Linux脚本
- 14. 确定单词是否在文件中的单词列表中
- 15. JAVA:如何检查网站文档是否包含单词?
- 16. 将特定单词移动到特定单元格 - VBA EXCEL
- 17. 在excel文档中寻找特定行VBA
- 18. 查找所有包含某个特定单词的n-gram
- 19. Lucene:查找以特定前缀开头的所有单词
- 20. 在vba词中查找单词后插入字符串
- 21. 如何检查句子是否包含特定单词?
- 22. C#检查DataGridView是否包含特定单词
- 23. 如何检查输出是否包含特定单词?
- 24. wordpress:如何检查slu contains是否包含特定的单词?
- 25. 如何在文件的特定部分查找单词?
- 26. 查找包含特定单词的文件 - SublimeText2
- 27. 如何查找具有特定字段的所有文档
- 28. 查找具有特定字段的所有Lucene文档
- 29. 查找单词开始于正则表达式中的特定单词
- 30. 在VBA中的列中查找特定单元格条目
不要有 “姓”,有[书签](https://support.office.com/en-us/article/Add-or-delete-bookmarks-f68d781f-0150-4583-a90e-a4009d99c2a0),并显示一个表单,如果[书签](https ://msdn.microsoft.com/en-us/library/office/ff834559.aspx)为空。 – GSerg