VBA Word:我想找一个词组,选择它前面的词,把文字斜体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43284752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:25:45  来源:igfitidea点击:

VBA Word: I would like to find a phrase, select the words before it, and italicise the text

vbams-wordword-vba

提问by Kevin

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.

我在使用 VBA 命令查找某个短语时遇到问题,然后选择它前面的 1 或 2 个单词,然后将整个内容斜体。

I'm able to use the Selection.Find, Font.Italicise, and wdExtendcommands independently of each other, but when I combine them to perform this task, the macro just crashes. Any help?

我能使用Selection.FindFont.ItalicisewdExtend命令相互独立的,但是当我将它们合并执行此任务,宏只是崩溃。有什么帮助吗?

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

回答by Variatus

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

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.

首先想象文档中的所有字符都串成一行,并散布着格式代码,这些代码也被视为字符。这个长字符串称为范围,在代码中,ActiveDocument.Range

You can select any part of the document's entire range. That would be the Selection.Rangewhich, like all ranges, has a Start (the first byte) and an End (the last byte. Startand Endare properties of the Rangerepresented 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. Rngand Selection.Rangeare identical at this point, but as you manipulate the Rngobject, the Selection.Rangewill not change.

您可以选择文档整个范围的任何部分。这将是Selection.Range,像所有范围一样,有一个开始(第一个字节)和一个结束(最后一个字节。Start并且EndRange由数字表示的属性,从第一个字节开始计数。我的代码创建了一个新的 Range 对象,它是所谓的RNG该Selection.Range被分配给新的对象。RngSelection.Range是相同的这一点,但是当你操纵Rng对象时,Selection.Range不会改变。

The code now looks for "Michael" in the Rngobject. Your syntax for setting up the search is perfect. I used different syntax because I find it easier to grasp. The .Foundproperty 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.Rangeyou would see "Michael" highlighted on the screen. But since the search was conducted in memory (on the Rngobject) the Selection.Rangeremains unchanged while the Rngobject now contains only the word "Michael".

代码现在在Rng对象中查找“Michael” 。您设置搜索的语法非常完美。我使用了不同的语法,因为我发现它更容易掌握。.Found如果搜索成功,则该属性返回 True。在这种情况下,搜索范围将更改为仅包括找到的子范围。如果在 中进行搜索,Selection.Range您会在屏幕上看到“Michael”突出显示。但是由于搜索是在内存中(在Rng对象上)进行的,因此对象Selection.Range保持不变,而Rng对象现在只包含单词“Michael”。

So, going back to the ActiveDocument.Range, of which Rngis a part, we now move the Startproperty two words to the left. A positive number would move it 2 words to the right. There is no need for Extendbecause the command is perfectly clear: "Move Start", meaning the End remains where it is.

所以,回到ActiveDocument.Range,它Rng是其中的一部分,我们现在将Start属性向左移动两个字。正数会将其向右移动 2 个字。没有必要,Extend因为命令非常清楚:“移动起点”,意思是终点保持在原处。

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 .Selectafter modifying the Font (before the outer End With), the modified Rngwould become the Selection.

现在 Rng 对象在“Michael”之前的 2 个词开始,以“Michael”这个词结束。您可以复制或删除此范围,或根据需要对其进行修改。请记住,您的屏幕仍显示原始Selection.Range. MS Word 不允许您分配Set Selection.Range = Rng,但有一种更简单的方法可以将显示与代码所做的内容重新对齐。通过.Select在修改 Font 之后(在外层之前End With)添加行,修改后的Rng将成为选择。