首页 > 解决方案 > 将所有 Txt 文件导入 Word 文档时如何将文本样式格式化为“标题 1”

问题描述

我使用这种方法将所有文件导入 Word 文档。

https://stackoverflow.com/a/30494740/908080

我在这样的内容文本之前添加了包含文件名

With wdDoc.Range
     .InsertAfter FileCnt & ". "
     .InsertAfter myFile & vbCr
     .InsertParagraphAfter
     .InsertAfter txtFiles.Range.Text & vbCr
End With

它工作正常。是否可以将文件名文本的格式设置为“标题 1”,其余内容为普通文本。完成后,我可以创建一个目录并快速转到所需的文件。

所以它需要看起来像

1.文件1.txt

这是 File1 文本

2.文件2.txt

这是 File2 文本

标签: vbams-wordformatstyles

解决方案


这是可能的,但这样做(很容易)需要稍微不同的方法来处理 target Range。更像这样的东西(未经测试):

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

考虑使用专用Range对象,例如使用选择 - “折叠”就像按箭头键。所以输入内容、格式,然后转到结尾(或开头)。然后重复下一个内容。


推荐阅读