Vba 如何将文本样式设置为“格式”;标题1“;将所有Txt文件导入Word文档时

Vba 如何将文本样式设置为“格式”;标题1“;将所有Txt文件导入Word文档时,vba,ms-word,format,styles,Vba,Ms Word,Format,Styles,我使用此方法将所有文件导入Word文档 我在内容文本之前添加了文件名,如下所示 With wdDoc.Range .InsertAfter FileCnt & ". " .InsertAfter myFile & vbCr .InsertParagraphAfter .InsertAfter txtFiles.Range.Text & vbCr End With 它很好用。 是否可以将文件名文本的格式设置为“标题1”,将其余内容

我使用此方法将所有文件导入Word文档

我在内容文本之前添加了文件名,如下所示

With wdDoc.Range
     .InsertAfter FileCnt & ". "
     .InsertAfter myFile & vbCr
     .InsertParagraphAfter
     .InsertAfter txtFiles.Range.Text & vbCr
End With
它很好用。 是否可以将文件名文本的格式设置为“标题1”,将其余内容设置为普通文本。 完成后,我可以创建一个TOC并快速转到所需的文件

所以它需要看起来像

1。File1.Txt

这是File1文本

2。File2.Txt


这是File2文本

这是可能的,但是(轻松地)这样做需要一种稍微不同的方法来处理目标
范围
。类似于此(未经测试):

想象一下使用一个专用的
范围
对象,就像使用一个选择一样-“折叠”就像按箭头键一样。因此,请输入内容、格式,然后转到结尾(或开始)。然后重复下一个内容

Dim rng as Word.Range
Set rng = wdDoc.Content 'a property that returns a Range; Doc.Range is a method
rng.Collapse wdCollapseEnd
With rng
     .Text = FileCnt & ". " &  myFile & vbCr
     .Style = wdStyleHeading1
     .Collapse wdCollapseEnd
     .Text = vbCr & txtFiles.Range.Text & vbCr
     .Style = wdStyleNormal
End With