vba - 将所有 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 文本
解决方案
这是可能的,但这样做(很容易)需要稍微不同的方法来处理 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
对象,例如使用选择 - “折叠”就像按箭头键。所以输入内容、格式,然后转到结尾(或开头)。然后重复下一个内容。
推荐阅读
- sql-server - SQL Server [本地慢 - 远程快]
- javascript - 导航后视频不再播放
- asp.net - 是否可以允许用户从已发布的 Web 应用程序连接到他们自己的数据库?
- java - 如何在 EDT 中运行摆动输入对话框并获取返回值?
- unity3d - 如何将记事本文件转换为 C#?
- ios - iOS 上 WebGL 着色器中的莫名行为
- c++ - C++ & SDL 为什么直接处理我的对象而不是使用指针有时会导致视觉问题?
- ios - 在多个 UICollectionViews 中显示电影列表中的详细视图
- redis - redis中如何表示product_id和产品属性数据
- javascript - 将下拉框选择附加到文本框