VBA Word:我想找一个词组,选择它前面的词,把文字斜体 [英] VBA Word: I would like to find a phrase, select the words before it, and italicise the text
问题描述
我在使用 VBA 命令查找某个短语时遇到问题,然后选择它前面的 1 或 2 个单词,然后将整个内容斜体.
I'm having trouble with VBA commands to find a certain phrase, then select the 1 or 2 words before it, and then italicize the entire thing.
我可以独立使用 Selection.Find
、Font.Italicise
和 wdExtend
命令,但是当我将它们结合起来执行这个任务,宏就会崩溃.有什么帮助吗?
I'm able to use the Selection.Find
, Font.Italicise
, and wdExtend
commands independently of each other, but when I combine them to perform this task, the macro just crashes. Any help?
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Michael"
.Replacement.Text = "Michael"
.Forward = True
.Wrap = wdFindStop
Do While .Execute() = True
Selection.TypeParagraph
Selection.MoveLeft Unit:=wdWord, Count:=2, Extend:=wdExtend
Selection.Find.Replacement.Font.Italic = True
Selection.Font.Bold = True
Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
推荐答案
以下代码将满足您的需求.但是,我以我认为最能让您理解的方式编写它.
The following code will do what you want. However, I wrote it in such a way as I think will enable you best to understand it.
Private Sub SelFind()
' 08 Apr 2017
Dim Rng As Range
Dim Fnd As Boolean
Set Rng = Selection.Range
With Rng.Find
.ClearFormatting
.Execute FindText:="Michael", Forward:=True, _
Format:=False, Wrap:=wdFindStop
Fnd = .Found
End With
If Fnd = True Then
With Rng
.MoveStart wdWord, -2
With .Font
.Italic = True
.Bold = True
End With
End With
End If
End Sub
首先将文档中的所有字符想象成一行,中间散布着同样被视为字符的格式代码.这个长字符串称为范围,在代码中,ActiveDocument.Range
.
Start with imagining all the characters in your document strung into a single line, interspersed with formatting codes which are also treated like characters. This long string of characters is called a range, in code, ActiveDocument.Range
.
您可以选择文档整个范围的任何部分.那将是 Selection.Range
,与所有范围一样,它有一个开始(第一个字节)和一个结束(最后一个字节.Start
和 End
是由数字表示的 Range
的属性,从第一个字节开始计数.我的代码创建了一个名为 Rng 的新 Range 对象.Selection.Range 被分配给该新对象.Rng
和 Selection.Range
在这一点上是相同的,但是当您操作 Rng
对象时,Selection.Range
将不改变.
You can select any part of the document's entire range. That would be the Selection.Range
which, like all ranges, has a Start (the first byte) and an End (the last byte. Start
and End
are properties of the Range
represented by numbers, counting from the first byte. My code creates a new Range object which is called Rng. The Selection.Range is assigned to that new object. Rng
and Selection.Range
are identical at this point, but as you manipulate the Rng
object, the Selection.Range
will not change.
代码现在在 Rng
对象中查找Michael".您设置搜索的语法非常完美.我使用了不同的语法,因为我发现它更容易掌握.如果搜索成功,.Found
属性返回 True.在这种情况下,搜索范围将更改为仅包括找到的子范围.如果在 Selection.Range
中进行搜索,您会在屏幕上看到Michael"突出显示.但是由于搜索是在内存中进行的(在 Rng
对象上),Selection.Range
保持不变,而 Rng
对象现在只包含单词迈克尔".
The code now looks for "Michael" in the Rng
object. Your syntax for setting up the search is perfect. I used different syntax because I find it easier to grasp. The .Found
property returns True if the search was successful. In that case the search range is changed to include only the found sub-range. Had the search been conducted in the Selection.Range
you would see "Michael" highlighted on the screen. But since the search was conducted in memory (on the Rng
object) the Selection.Range
remains unchanged while the Rng
object now contains only the word "Michael".
所以,回到 ActiveDocument.Range
,其中 Rng
是其中的一部分,我们现在将 Start
属性两个字移到左边.正数会将其向右移动 2 个字.不需要Extend
,因为命令非常清楚:移动起点",意思是终点保持原处.
So, going back to the ActiveDocument.Range
, of which Rng
is a part, we now move the Start
property two words to the left. A positive number would move it 2 words to the right. There is no need for Extend
because the command is perfectly clear: "Move Start", meaning the End remains where it is.
现在 Rng 对象在 "Michael" 之前的 2 个单词开始,并以 "Michael" 结束.您可以复制或删除此范围,或根据需要对其进行修改.请记住,您的屏幕仍会显示原始的 Selection.Range
.MS Word 不允许您分配 Set Selection.Range = Rng
,但是有一种更简单的方法可以将显示与代码所做的内容重新对齐.通过在修改 Font 之后(在外部 End With
之前)添加行 .Select
,修改后的 Rng
将成为选择.
Now the Rng object starts 2 word before "Michael" and ends with the word "Michael". You can copy this range or delete it, or modify it as you wish. Bear in mind that your screen still shows the original Selection.Range
. MS Word will not allow you to assign Set Selection.Range = Rng
, but there is an even easier way to realign the display with what the code has done. By adding the line .Select
after modifying the Font (before the outer End With
), the modified Rng
would become the Selection.
这篇关于VBA Word:我想找一个词组,选择它前面的词,把文字斜体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!