通过宏将粗体和常规文本插入单词 - 交替问题

时间:2018-02-01 08:28:43

标签: vba ms-word doc

我有一个宏,其中一个参数是一个数组。对于数组中的每个元素,我向表中添加一行,添加标准文本“NOTE:”,然后添加数组元素文本。这是我正在使用的宏的相关部分:

Sub fill(..., notes)

    Dim tblNew As Table
    Dim rowNew As Row
    Dim celTable As Cell
    Dim intCount As Integer

    For Each element In notes
        Set tblNew = ActiveDocument.Tables(2)
        Set rowNew = tblNew.Rows.Add
        tblNew.Cell(tblNew.Rows.Count, 1).Range.Select
        Selection.Font.Bold = wdToggle
        If Selection.Font.Underline = wdUnderlineNone Then
            Selection.Font.Underline = wdUnderlineSingle
        Else
            Selection.Font.Underline = wdUnderlineNone
        End If
        Selection.TypeParagraph
        Selection.TypeText Text:="NOTE:"
        Selection.Font.Bold = wdToggle
        If Selection.Font.Underline = wdUnderlineNone Then
            Selection.Font.Underline = wdUnderlineSingle
        Else
            Selection.Font.Underline = wdUnderlineNone
        End If
        Selection.TypeText Text:=element
    Next element

End Sub

现在这确实添加了所有行,它确实添加了“NOTE:”和数组元素文本部分ok,问题在于粗体和下划线。我想要“注意:”是粗体和下划线,元素文本只是简单。使用上面的宏,对于第一行,“NOTE:”是粗体/下划线,文本是正常的,但对于下一行,“NOTE:”是正常的,元素文本是粗体/下划线。图案交替排列。所以这是模式(减去下划线):

注意:

元素文字

注意:

元素文字

注意:

元素文字

......等等。我一直在玩,但不能让它在每一行都是一样的,为什么它会这样做呢?

1 个答案:

答案 0 :(得分:1)

我了解NOTE:必须加粗+加下划线,后面的文字必须为正常。如果这是正确的,请尝试以下代码:

Dim tblNew As Table
Dim rowNew As Row
Dim celTable As Cell
Dim intCount As Integer
Dim Notes As Variant

For Each element In Notes
    Set tblNew = ActiveDocument.Tables(2)
    Set rowNew = tblNew.Rows.Add
    tblNew.Cell(tblNew.Rows.Count, 1).Range.Select
    Selection.TypeParagraph
    With Selection.Range
        .Text = "NOTE:"
        .Font.Bold = True
        .Font.Underline = wdUnderlineSingle
    End With
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
    With Selection.Range
        .Text = element
        .Font.Bold = False
        .Font.Underline = wdUnderlineNone
    End With
Next element