使用VBA将粗体文字插入到Word中 [英] Insert bold text into Word using VBA

查看:472
本文介绍了使用VBA将粗体文字插入到Word中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,将某些Excel单元格值导出到Word中。但是,某些插入需要是粗体。而且似乎没有一个简单的方法来执行此操作。



此代码循环遍历记录并将其添加到Word文档中

  
Do While intRow< intTotalRows + 1

strTemp =; b; &Range(G&intRow).FormulaR1C1&&Range(I&intRow).FormulaR1C1&; e;

如果strTemp& strCur然后
strCur = strTemp
.Content.Font.Bold = True
.Content.InsertAfter strCur
End If

.Content.Font.Bold = False
.Content.InsertAfter Range(A&intRow).FormulaR1C1& - &Range(C&intRow).FormulaR1C1&&Range(E&intRow).FormulaR1C1& *

intRow = intRow + 1
循环

在插入文本之前先打开粗体,然后再次关闭它似乎是最合乎逻辑的解决方案,所以它不起作用。



然后我尝试找到并替换文本,但是这也没有起作用:



  
.Content.Find.ClearFormatting
With。 Content.Find
.Text =; b; '寻找
.Replacement.Text =; bbb; '替换为
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
结束与

  .Content。 Find.Execute替换:= wdReplaceAll 

解决方案

.InsertAfter 替换为 .TypeText 。插入像粘贴的作品,而 TypeText 的工作原理就好比您键入键盘上的文本。


I wrote a little script that exports certain Excel cell values into Word. However, certain inserts need to be bold. And there doesn't seem to be an easy way to do this.

This code loops through the records and adds them to the Word document


Do While intRow < intTotalRows + 1

                strTemp = " ;b;" & Range("G" & intRow).FormulaR1C1 & " " & Range("I" & intRow).FormulaR1C1 & ";e; "

                If strTemp <> strCur Then
                    strCur = strTemp
                    .Content.Font.Bold = True
                    .Content.InsertAfter strCur
                End If

                .Content.Font.Bold = False
                .Content.InsertAfter Range("A" & intRow).FormulaR1C1 & " - " & Range("C" & intRow).FormulaR1C1 & " " & Range("E" & intRow).FormulaR1C1 & " * "

            intRow = intRow + 1
        Loop

Turning on bold before inserting text and turning it off again afterwards seems like the most logical solution, so it does not work.

I then tried to find and replace the text, but that also did not work:


        .Content.Find.ClearFormatting
        With .Content.Find
            .Text = ";b;" 'Look for
            .Replacement.Text = ";bbb;" 'Replace with
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False 
            .MatchWholeWord = False
            .MatchWildcards = False 
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

    .Content.Find.Execute Replace:=wdReplaceAll

解决方案

Replace .InsertAfter with .TypeText. Inserting works like pasting whereas TypeText works like if you would actually type the text on the keyboard.

这篇关于使用VBA将粗体文字插入到Word中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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