Word 2007 VBA - 制作一些文本粗体和其他斜体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14773971/
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-11 19:32:49  来源:igfitidea点击:

Word 2007 VBA - Making some text BOLD & other ITALIC

vbaformattingms-wordword-vba

提问by Gary Heath

I have the following code that selects data from an Excel Cell & replaces a specific piece of text in my Word document (for purposes of this question, the Excel Cell has been replaced by a plain text string).

我有以下代码从 Excel 单元格中选择数据并替换我的 Word 文档中的特定文本段(出于此问题的目的,Excel 单元格已替换为纯文本字符串)。

The data ": goes to " is constant, then the data "aaa bbb" can be anything until we reach the " of " which is also constant. Then the data after the " of ", "ccc ddd eee" can be anything until it hits the " - " which is also constant.

数据“:gos to”是常量,那么数据“aaa bbb”可以是任何东西,直到我们到达“of”,它也是常量。然后“ of”,“ccc ddd eee”之后的数据可以是任何东西,直到它碰到“-”,这也是常数。

Is it possible to make the "aaa bbb" data BOLD& UPPER CASE, whilst making the "ccc ddd eee" data into ITALICS?

是否有可能使“AAA BBB”数据BOLD&大写,而使得“CCC DDD EEE”数据转换成斜体字

": goes to AAA BBBof ccc ddd eee- "

“:去AAA BBBCCC DDD EEE- ”

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "MOTMDIV1"
    .Replacement.Text = ": goes to aaa bbb of ccc ddd eee - "
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Selection.Find.Execute Replace:=wdReplaceAll

采纳答案by CuberChase

If you change your Find and Replace to a single Find and Replace from All, Word will highlight the replaced text allowing you to alter the properties of the replaced range. I've modified your code to highlight this:

如果您将“查找和替换”更改为单个“从所有查找和替换”,Word 将突出显示替换文本,允许您更改替换范围的属性。我已经修改了您的代码以突出显示这一点:

Sub ReplaceAndFormat()
   Dim sConst1 As String, sConst2 As String, sReplaceMent As String
   Dim rRange As Range, rFormat As Range

    'Set your constants.  This is where you can read in from Excel or whereever
   sConst1 = "aaa bbb"
    sConst2 = "ccc ddd eee"

    'Build the replacement string
    sReplaceMent = ": goes to " & sConst1 & " of " & sConst2 & " - "

    'Your replacement code
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
       .Text = "MOTMDIV1"
        .Replacement.Text = sReplaceMent
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceOne

       'If we replace one by one Word will select the range after it finds it
        If .Found Then
            'After you've done the replacement, set it to a range so you can format
            Set rRange = Selection.Range

           'We know the length of all the strings so we can set the range of the "aaa bbb" etc etc 
           Set rFormat = ActiveDocument.Range(rRange.Start + 10, rRange.Start + 10 + VBA.Len(sConst1))
           'Set the formats for the first part
           rFormat.Font.Bold = True
           rFormat.Font.AllCaps = True

           'Repeat for the second part
           Set rFormat = ActiveDocument.Range(rRange.Start + 14 + VBA.Len(sConst1), rRange.Start + 14 + VBA.Len(sConst1) + VBA.Len(sConst2))
           rFormat.Font.Italic = True
       End If
   End With
End Sub

Note: If you want to Find and Replace all instantences of your search text then you'll have to loop through the document like this: Repeating Microsoft Word VBA until no search results found

注意:如果要查找和替换搜索文本的所有实例,则必须像这样循环遍历文档:重复 Microsoft Word VBA 直到找不到搜索结果