我有以下宏。它将x.x格式的数字更改为x,x。它被记录下来,我添加了IF语句,以确保选择一个文本以防止用户在整个文档中执行该操作。
Sub fixComma()
'
' fixComma Macro
'
'
If (Selection.Start <> Selection.End) Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.LanguageID = wdEnglishUS
With Selection.Find
.Text = "([0-9]).([0-9])"
.Replacement.Text = "\1,\2"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Else
MsgBox "Nothing is selected, Macro terminated"
End If
End Sub
问题是它正在改变整个文档而不仅仅是选择。
答案 0 :(得分:0)
我想为 Excel 中的文本添加格式,并最终使用搜索和替换将 Markdown **Stuff in Bold** 和 *Stuff in Italic* 转换为格式化文本。
我创建了一个文本框 (ActiveDocument.Shapes.Range(1)) 并在该文本框中插入了我想要格式化的记录。
文本框中的记录远远超过 255 个字符并且显示正常。
Sub SearchAndReplace()
'
' Search and Replace Macro
'
'
Set myshape = ActiveDocument.Shapes.Range(1)
Set myrange = myshape.TextFrame.TextRange
myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
myrange.Find.Replacement.Font.Bold = True
With myrange.Find
.text = "(\*\*)(*)(\*\*)"
.Replacement.text = "\2"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
myrange.Find.Execute Replace:=wdReplaceAll
Set myshape = ActiveDocument.Shapes.Range(1)
Rem myshape.Fill.ForeColor.RGB = RGB(0, 255, 0)
Set myrange = myshape.TextFrame.TextRange
myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
myrange.Find.Replacement.Font.Italic = True
With myrange.Find
.text = "(\*)(*)(\*)"
.Replacement.text = "\2"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
myrange.Find.Execute Replace:=wdReplaceAll
End Sub