Insert bold text into Word using VBA

早过忘川 提交于 2020-01-02 04:43:05

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/3966984/insert-bold-text-into-word-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!