VBA字词:我想找到一个词组,选择它之前的词,并用斜体显示文本 [英] 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
的属性,由数字表示,从第一个字节,我的代码创建了一个新的Range对象,称为Rng.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
对象现在仅包含单词"Michael".
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
,因为命令非常清楚:"Move Start"(移动开始),这意味着End保留在原处.
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"之前的两个单词开始,以"Michael"单词结束.您可以复制或删除此范围,或根据需要对其进行修改.请记住,您的屏幕仍显示原始的Selection.Range
. MS Word不允许您分配Set Selection.Range = Rng
,但是有一种甚至更简单的方法可以将显示与代码所做的操作重新对齐.通过在修改Font之后(在外部End With
之前)添加.Select
行,修改后的Rng
将成为Selection.
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字词:我想找到一个词组,选择它之前的词,并用斜体显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!