从Word文档中获取标题
如何使用VBA获取Word文档中所有标题的列表?
How do I get a list of all the headings in a word document by using VBA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何使用VBA获取Word文档中所有标题的列表?
How do I get a list of all the headings in a word document by using VBA?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
您的意思是这样 createOutline 函数(实际上复制了将源Word文档转换为新的Word文档):(
我相信
astrHeadings = docSource。GetCrossReferenceItems(wdRefTypeHeading)
函数是此程序的关键,应该允许您检索您所要求的内容)@kol 于 2018 年 3 月 6 日更新
虽然
astrHeadings
是一个数组(IsArray
返回True
,并且TypeName< /code> 返回
String()
) 当我尝试在 VBScript 中访问其元素(Windows 10 Pro 1709 16299.248 上的 v5.8.16384)时,出现类型不匹配
错误。 这肯定是 VBScript 特定的问题,因为如果我在 Word 的 VBA 编辑器中运行相同的代码,我就可以访问这些元素。 我最终迭代了目录中的各行,因为它甚至可以在 VBScript 中工作:You mean like this createOutline function (which actually copy all headings from a source word document into a new word document):
(I believe the
astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading)
function is the key in this program, and should allow you to retrieve what you are asking for)UPDATE by @kol on March 6, 2018
Although
astrHeadings
is an array (IsArray
returnsTrue
, andTypeName
returnsString()
) I get atype mismatch
error when I try to access its elements in VBScript (v5.8.16384 on Windows 10 Pro 1709 16299.248). This must be a VBScript-specific problem, because I can access the elements if I run the same code in Word's VBA editor. I ended up iterating the lines of the TOC, because it works even from VBScript:获取标题列表的最简单方法是循环浏览文档中的段落,例如:
顺便说一句,我发现删除段落范围的最后一个字符是个好主意。 否则,如果将字符串发送到消息框或文档,Word 将显示额外的控制字符。 例如:
The easiest way to get a list of headings, is to loop through the paragraphs in the document, for example:
By the way, I find it is a good idea to remove the final character of the paragraph range. Otherwise, if you send the string to a message box or a document, Word displays an extra control character. For example:
这个宏对我来说非常有效(Word 2010)。 我稍微扩展了该功能:现在它提示用户输入最低级别,并禁止该级别以下的副标题。
This macro worked beautifully for me (Word 2010). I've extended the functionality slightly: now it prompts the user to enter a minimum level, and supresses subheadings below that level.
提取所有标题的最快方法(至 LEVEL5)。
Fastest method for extracting of all headings (to LEVEL5).
在 Wiki 对 VonC 答案的评论之后,这是对我有用的代码。 它使该功能更快。
Following Wikis comment on VonC answer, here is the code that worked for me. It makes the function faster.
为什么要重复发明轮子这么多次?!?
“所有标题列表”就是标准的Word文档索引!
这是我在向文档添加索引时录制宏所得到的结果:
Why reinventing the wheel so many times?!?
A "list of all headings" is just the standard Word index of document!
This is what I got by recording a macro while adding index to the document:
您还可以在文档中创建目录并复制它。 这将参考文献与标题分开,如果您需要在另一个上下文中呈现它,这会很方便。
如果您不想在文档中包含目录,只需在复制粘贴后将其删除即可。 JK。
You can also create a Table of Contents in the doc and copy that. This separates out the para ref from the title, which is handy if you need to present that in another context.
If you do not want the ToC in your doc, just delete that after the Copy n Paste. JK.