VBA查找文档中是否存在特定单词

VBA查找文档中是否存在特定单词,vba,Vba,打开word文档时,我希望能够检查文档中是否存在特定的word。如果是,那么我想打开一个userform。基本上,我有一个模板信,在问候语中用“姓氏”代替收件人的姓氏。当我打开文档时,我希望自动弹出一个用户表单,以便在文本框中输入此人的姓氏,并在单击用户表单上的“完成”时运行查找和替换功能。如果我已经将“lastname”替换为此人的姓氏,那么我不希望弹出userform。我知道如何完成一切,除了检查是否存在“姓氏”。有什么方法可以做到这一点吗?在“ThisDocument”代码窗格中放置此(注

打开word文档时,我希望能够检查文档中是否存在特定的word。如果是,那么我想打开一个userform。基本上,我有一个模板信,在问候语中用“姓氏”代替收件人的姓氏。当我打开文档时,我希望自动弹出一个用户表单,以便在文本框中输入此人的姓氏,并在单击用户表单上的“完成”时运行查找和替换功能。如果我已经将“lastname”替换为此人的姓氏,那么我不希望弹出userform。我知道如何完成一切,除了检查是否存在“姓氏”。有什么方法可以做到这一点吗?

在“ThisDocument”代码窗格中放置此(注释)代码:

选项显式
私有子文档_Open()
Dim lastNameRng As范围

设置lastNameRng=GetLastname(ActiveDocument,“lastname”)'没有“lastname”,有一个,如果是空的,则显示一个表单。非常感谢,工作非常完美!
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