vba应用样式

vba应用样式,vba,ms-word,word-style,Vba,Ms Word,Word Style,我有一段代码将excel单元格值插入word文档: Set wrdRange = wrdDoc.Range With wrdRange .Collapse Direction:=wdCollapseEnd .InsertParagraphAfter xText = Rng.Cells(i + 1, 1).Value .InsertAfter xText .Collapse Direction:=

我有一段代码将excel单元格值插入word文档:

    Set wrdRange = wrdDoc.Range
    With wrdRange
        .Collapse Direction:=wdCollapseEnd
         .InsertParagraphAfter
         xText = Rng.Cells(i + 1, 1).Value
         .InsertAfter xText
        .Collapse Direction:=wdCollapseEnd
    End With
我需要用H1设置xText的样式,但不明白。
xText可以包含许多单词

提前谢谢。

试试这个

Set wrdRange = wrdDoc.Range
With wrdRange
    .Collapse Direction:=wdCollapseEnd
    .InsertParagraphAfter
    xText = Rng.Cells(i + 1, 1).Value
    .InsertAfter xText

    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=Len(xText), Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles("Heading 1")

    .Collapse Direction:=wdCollapseEnd
End With

我尝试了您的建议,但如果失败,则会出现运行时错误(选择不是支持者)。经过一些研究,我找到了这样做的方法:使用wrdRange.collapseend.InsertParagraphAfter xText=Rng.Cells(I+1,1).Value.InsertAfter xtextext.parages(2).Style=“Heading 1”.InsertParagraphAfter.Collapse方向:=wdCollapseEnd结束于