単語文書を開くときに、文書内に特定の単語が存在するかどうかを確認できます。もしそうなら、私はユーザーフォームを開きたいと思います。基本的には、挨拶の受取人の姓の代わりに「姓」のテンプレートレターがあります。ドキュメントを開くと、自動的にユーザーフォームがポップアップされるので、テキストボックスにユーザーの姓を入力し、ユーザーフォームで「完了」をクリックすると検索と置換機能が実行されます。 「lastname」をその人の姓に置き換えた場合、私はuserformをポップアップしたくありません。私は "姓"が存在するかどうかをチェックすることを除いてすべてを達成する方法を知っています。これを行う方法はありますか? "ThisDocumentの" コード・ペインの代わりに特定の単語が文書内にあるかどうかを調べるためのVBA
-1
A
答えて
0
この(コメント)コード:
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. imputfieldの文字が単語内にあるかどうかを調べる
- 2. 単語がArrayList内の特定の単語と等しいかどうかを調べる
- 3. elasticsearch内の特定の文書の特定の単語を強調する
- 4. 単語が複数であるかどうかを調べる
- 5. 入力された単語がPython 3のループ内の大文字であるかどうかを調べる
- 6. Word文書内の特定の単語が太字であるかどうかを確認しますか?
- 7. コンポーネントが特定のコンテナ内にあるかどうかを調べる
- 8. 日付が特定の範囲内にあるかどうかを調べるためのNSPredicateの作成
- 9. c#文字列に特定の単語があるかどうかをチェック
- 10. VBA文字列内の特定の単語を検索する
- 11. 与えられた単語が回文かどうかを調べる関数
- 12. Python:リスト内の要素に特定の文字列があるかどうか調べる
- 13. 単語が検索文字で始まるかどうかを調べるRegEx
- 14. 特定の文字が単語に含まれているかどうかを調べるためにstr.find()を使用しています
- 15. C 2つの単語がアナグラム(内部のコード)であるかどうかを調べるプログラム
- 16. 文書がsolrで指定された用語で始まるかどうかを調べる方法は?
- 17. セル内の値が2つのセルの値の範囲内にあるかどうかを調べるVBAマクロ
- 18. ディレクトリ内の特定の単語を持つxmlファイルの数を調べる
- 19. 文字列が英語かどうかを調べる
- 20. クラスが特定の型であるかどうかを調べる
- 21. リストに特定の値を持つ辞書があるかどうかを調べる
- 22. オブジェクトがヒープ内部にあるかどうかを調べる
- 23. javaを使って単語が別の単語から綴られているかどうかを調べる
- 24. c#特定の単語がtxtファイル内で何時に発生するかを調べる
- 25. Ocaml StringMapに単語が存在するかどうかを調べる
- 26. Javaで特定のフォントが特定の文字をレンダリングできるかどうかを調べる
- 27. [C code]の文章で特定の単語が何回出現するかを調べる方法は?
- 28. 文字列に特定の構造があるかどうかを調べるpreg_match
- 29. Postgresの文字列に単語全体が存在するかどうかを調べるには
- 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