
テキスト内に [単語]- [単語] が複数回使用されています。たとえば、正しい形式である self-interest の代わりに self-interest が使用されています。MS Word のワイルドカードを使用して、ストロークの後の余分なスペースを削除しようとしています。検索と置換ボックスに次のように入力します。
検索: *- *
置換: \1-\2
しかし、うまくいきません。これを実現するための正しい形式は何ですか?
答え1
VBA:
WrapReplace
Word 文書で実行します。
Sub WrapReplace()
Call RegExpReplace("(\w+)\-\s(\w+)", "$1-$2")
End Sub
Private Sub RegExpReplace(pattern As String, Backreference As String)
Dim strReplacement As String
Set oRegExp = CreateObject("VBScript.RegExp")
With oRegExp
.Global = True
.IgnoreCase = False
.pattern = pattern
End With
Set matches = oRegExp.Execute(ActiveDocument.Content)
For Each match In matches
Set matchRange = ActiveDocument.Content
strReplacement = oRegExp.Replace(match.Value, Backreference)
With matchRange.Find
.Text = match.Value
.Replacement.Text = strReplacement
.Wrap = wdFindAsk
.Execute Replace:=wdReplaceOne
End With
Next
End Sub