0

我有一个 XLS 到 Word 文档自动化项目,其中 2 个字符串String1需要String2分配给 Word 表格单元格。挑战在于让 String1 为红色斜体字体样式,而 String2 为黑色正常字体样式。我如何做到这一点?

我目前的代码使所有文本都变成红色斜体,这是默认的表格字体样式。

With wdoc.Tables(pos)
     .Rows(1).Cells(1).Range.Text = String1
     wapp.Selection.Font.Italic = False
     wapp.Selection.Font.Color = wdColorAutomatic
     .Rows(1).Cells(1).Range.Text = .Rows(1).Cells(1).Range.Text & String2
End with
4

1 回答 1

1

我能想到的第一个想法是根据文本在整个文档中的位置使用对范围的引用。下面的代码查找两个属性的值:.Start以及.End您的单元格文本,检查两者的长度String1String2文本。全部用作Document.Range(Start,End)参数。因此,代码将如下所示(将其添加到您的代码之后:

With wdoc.Tables(pos).Rows(1).Cells(1).Range
        With wdoc.Range(.Start, .Start + Len(String1)).Font
            .Italic = True
            .Color = vbRed
        End With
        With wdoc.Range(.End - Len(String2), .End).Font
            'do nothing assuming text is black/standard
            'or change to black if necessary
        End With
End With
于 2013-07-31T14:31:40.553 回答