【wordマクロ・word VBA】選択範囲内の指定したキーワードを太字ゴシック下線付きにする
選択された範囲内の「指定したキーワード」を「太字&MSゴシック&下線付き」にするマクロです。
①反映したい範囲を選択します
②マクロを実行する
③入力Boxに対象のキーワードを入力する
※正規表現が使用できます
④OKを押下
![](https://assets.st-note.com/img/1728365269-WyIz06M35qEJc2sLvwSrtPne.png)
Sub 指定したキーワードを太字ゴシック下線付きにする()
Dim myKW As String
Dim lStart_1 As Long
Dim lStart As Long
Dim lEnd As Long
myKW = InputBox("キーワードを入力してください。")
If myKW = "" Or Selection.Start = Selection.End Then
Exit Sub
End If
lStart_1 = Selection.Range.Start
lStart = Selection.Range.Start
lEnd = Selection.Range.End
With Selection.Find
.Format = False '書式の指定
.MatchAllWordForms = False 'Textプロパティの使用
.MatchSoundsLike = False '類似した単語
.MatchFuzzy = False 'あいまい検索
.MatchWildcards = True '正規表現
.MatchByte = True '全角半角の区別
.Wrap = wdFindStop '検索範囲の末尾に到達で終了
End With
Do While Selection.Find.Execute(myKW)
With Selection
.Font.Bold = True '太字
.Font.Name = "MS ゴシック" 'フォントをMSゴシック
.Font.Underline = wdUnderlineSingle '下線
End With
Selection.Collapse Direction:=wdCollapseEnd
lStart = Selection.Range.Start
ActiveDocument.Range(lStart, lEnd).Select
Loop
ActiveDocument.Range(lStart_1, lEnd).Select
MsgBox ("処理が完了しました。")
End Sub