2016-09-26 73 views
-1

打開單詞文檔時,我希望能夠檢查文檔中是否存在特定單詞。如果確實如此,我想打開一個用戶窗體。基本上,我有一個帶有「姓氏」的模板字母來代替收件人的姓氏。當我打開文檔時,我希望自動彈出一個用戶窗體,以便我可以在文本框中輸入該用戶的姓,並在單擊用戶窗體上的「完成」時運行查找和替換功能。如果我已經用姓氏的姓氏替換了「姓氏」,那麼我不希望用戶窗體彈出。我知道如何完成一切,除了檢查是否存在「lastname」。有沒有辦法做到這一點?VBA查找文檔中是否有特定單詞

+0

不要有 「姓」,有[書籤](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

回答

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

相關問題