Word 2007 VBA - 制作一些文字BOLD&其他ITALIC [英] Word 2007 VBA - Making some text BOLD & other ITALIC

查看:167
本文介绍了Word 2007 VBA - 制作一些文字BOLD&其他ITALIC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码从Excel Cell&替换我的Word文档中的一段特定文本(就这个问题而言,Excel单元格已被替换为纯文本字符串)。

数据:去到是恒定的,那么数据aaa bbb可以是任何东西,直到我们到达也是恒定的。那么在之后,ccc ddd eee之后的数据可以是任何东西,直到它碰到 - ,这也是常数。

是否有可能使aaa bbbdata BOLD & UPPER CASE,同时将ccc ddd eee数据转换为ITALICS?


$ b :转到 AAA BBB ccc ddd eee -

  Selection.HomeKey单位:= wdStory 
选择。 Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text =MOTMDIV1
.Replacement.Text =:去ccc的aaa bb ddd eee -
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute替换:= wdReplaceAll


解决方案

如果您将查找和替换更改为单个查找和全部替换,文本允许您更改替换范围的属性。我已经修改了你的代码来突出显示这个:

$ Sub $ Replace $ $ $ b $ Dim sConst1 As String,sConst2 As String ,sReplaceMent As String
Dim rRange As Range,rFormat As Range

'设置你的常量。这是你可以从Excel或
读取的位置sConst1 =aaa bbb
sConst2 =ccc ddd eee

构建替换字符串
sReplaceMent =:转到& sConst1& 的和 sConst2& -

您的替换码
Selection.HomeKey单位:= 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替换:= wdReplaceOne

'如果我们一个一个地替换Word将在找到它之后选择范围
如果.Found然后
'在完成替换之后,将其设置为一个范围,可以格式
设置rRange = Selection.Range

'我们知道所有字符串的长度,所以我们可以设置aaa bbb等的范围等等
Set rFo rmat = ActiveDocument.Range(rRange.Start + 10,rRange.Start + 10 + VBA.Len(sConst1))
'设置第一部分的格式
rFormat.Font.Bold = True
rFormat.Font.AllCaps = True

'为第二部分重复
设置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

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


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).

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.

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

": goes to AAA BBB of ccc 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

解决方案

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:

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

这篇关于Word 2007 VBA - 制作一些文字BOLD&其他ITALIC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆