替换单词保持原始大小写
Replace words keeping original case
我正在制作一个程序来替换 MS Word 文档中的单词。我想保留单词的原始大小写。
比方说,有一个表达:“Broken Glass”。我想修改成“Cristal Roto”。
我有两个文本框,输入和输出。
输入是要替换的词所在的位置,输出是输入词将被替换的词。
对于这个例子,我会在(输入)文本框中输入“Broken Glass”,在(输出)文本框中输入“Cristal Roto”。
Word 文档中的结果将是“Cristal roto”。第二个单词的首字母小写。
Dim rng As Word.Range
rng = objWordS.ActiveDocument.Content
With rng.Find
.ClearFormatting()
.Execute(FindText:=Input.Text, _
ReplaceWith:=Output.Text, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End With
我只关心每个单词首字母的大小写
编辑
“Broken Glass”变成“Cristal roto”的原因是我错过了这个:
.Replacement.ClearFormatting()
我在 .ClearFormatting
和 .Execute
之间添加的。
现在 Word 文件中的文本被替换为我在输出文本框中使用的大小写。
不过,我想知道是否有一种方法可以检测 Word 文件中原始单词的大小写,从而在不使用 Proper Case 的情况下自动调整输出文本框的文本。所以:
替换:“碎玻璃” - 替换文本:“cristal roto” - 所需结果:“Cristal Roto”。
尝试在执行搜索时将 Output.Text
解析为 ProperCase
。这样的事情应该有效:
ReplaceWith:=StrConv(Output.Text, vbProperCase)
更短更清晰。但是还是得用ProperCase
,不知道合不合你的需求
我最终做了我想做的事:
Dim rng As Word.Range
rng = objWordS.ActiveDocument.Content
With rng.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Execute(FindText:=InputText)
If rng.Text = rng.Text.Substring(0, 1).ToLower() + rng.Text.Substring(1) Then
.Execute(ReplaceWith:=Output.Text.Substring(0, 1).ToLower() + Output.Text.Substring(1).ToLower, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End If
If rng.Text = rng.Text.Substring(0, 1).ToUpper() + rng.Text.Substring(1) Then
.Execute(ReplaceWith:=Output.Text, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End If
End With
这样,我就不必每次都手动更改大小写了,这将根据第一个字符是大写字母还是小写字母来替换单词。如果是大写,文本将替换为输出中使用的大小写(例如 BroKeN WindoW)。
如果是小写字母,那么它将替换小写输出内容的单词(例如 broken window)。有了这个方法,我以后或许可以实现新的条件,比如"If first word's first letter is uppercase, but second word's first letter is lowercase, then..."。我希望这对其他人也有用。
实际上,我什至可以使用 Proper Casing 实现一个条件,以实现我在主要问题的 EDIT 下提出的要求。
感谢您的宝贵时间和帮助!
我正在制作一个程序来替换 MS Word 文档中的单词。我想保留单词的原始大小写。
比方说,有一个表达:“Broken Glass”。我想修改成“Cristal Roto”。
我有两个文本框,输入和输出。
输入是要替换的词所在的位置,输出是输入词将被替换的词。
对于这个例子,我会在(输入)文本框中输入“Broken Glass”,在(输出)文本框中输入“Cristal Roto”。
Word 文档中的结果将是“Cristal roto”。第二个单词的首字母小写。
Dim rng As Word.Range
rng = objWordS.ActiveDocument.Content
With rng.Find
.ClearFormatting()
.Execute(FindText:=Input.Text, _
ReplaceWith:=Output.Text, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End With
我只关心每个单词首字母的大小写
编辑
“Broken Glass”变成“Cristal roto”的原因是我错过了这个:
.Replacement.ClearFormatting()
我在 .ClearFormatting
和 .Execute
之间添加的。
现在 Word 文件中的文本被替换为我在输出文本框中使用的大小写。
不过,我想知道是否有一种方法可以检测 Word 文件中原始单词的大小写,从而在不使用 Proper Case 的情况下自动调整输出文本框的文本。所以:
替换:“碎玻璃” - 替换文本:“cristal roto” - 所需结果:“Cristal Roto”。
尝试在执行搜索时将 Output.Text
解析为 ProperCase
。这样的事情应该有效:
ReplaceWith:=StrConv(Output.Text, vbProperCase)
更短更清晰。但是还是得用ProperCase
,不知道合不合你的需求
我最终做了我想做的事:
Dim rng As Word.Range
rng = objWordS.ActiveDocument.Content
With rng.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Execute(FindText:=InputText)
If rng.Text = rng.Text.Substring(0, 1).ToLower() + rng.Text.Substring(1) Then
.Execute(ReplaceWith:=Output.Text.Substring(0, 1).ToLower() + Output.Text.Substring(1).ToLower, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End If
If rng.Text = rng.Text.Substring(0, 1).ToUpper() + rng.Text.Substring(1) Then
.Execute(ReplaceWith:=Output.Text, _
MatchWholeWord:=True, _
Replace:=Word.WdReplace.wdReplaceAll)
End If
End With
这样,我就不必每次都手动更改大小写了,这将根据第一个字符是大写字母还是小写字母来替换单词。如果是大写,文本将替换为输出中使用的大小写(例如 BroKeN WindoW)。
如果是小写字母,那么它将替换小写输出内容的单词(例如 broken window)。有了这个方法,我以后或许可以实现新的条件,比如"If first word's first letter is uppercase, but second word's first letter is lowercase, then..."。我希望这对其他人也有用。
实际上,我什至可以使用 Proper Casing 实现一个条件,以实现我在主要问题的 EDIT 下提出的要求。
感谢您的宝贵时间和帮助!