(注意:请参阅下面的解决方案.)

我一直在尝试使用VBA从word文档中的各种标题页面中检索页码.我当前的代码返回2或3,而不是正确关联的页码,具体取决于我在主Sub中使用它的位置和方式.

astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading)

For Each hds In astrHeadings

docSource.Activate

With Selection.Find

.Text = Trim$(hds)

.Forward = True

MsgBox hds & ":" & Selection.Information(wdActiveEndPageNumber), vbOKOnly

End With

Selection.Find.Execute

Next

docSource是我设置的测试文档,有3个页面的10个标题.我有从后来在我的代码中使用的getCrossReferenceItems方法检索的标题.

我正在尝试循环getCrossReferenceItems方法的结果,并在docSource上的Find对象中使用它们,并从中确定结果在哪个页面上.然后,我的代码中的页码将在稍后的字符串中使用.这个字符串加上页码将被添加到另一个在我的主子开头创建的文档中,其他所有文件都可以处理,但这个代码段.

理想情况下,我需要这个段做的是用每个Find结果中的相关页码填充第二个数组.

解决问题

谢谢凯文,你在这里得到了很大的帮助,我现在从这个Sub的输出中得到了我所需要的.

docSource是我设置的测试文档,有3个页面的10个标题.

docOutline是一个新文档,它将作为目录文档.

我不得不使用这个Sub over Word的内置TOC功能,因为:

>我有多个文件要包含,我可以使用RD字段来包含这些文件

>我有另一个Sub,它在每个文件0.0.0(chapter.section.page代表)中生成自定义小数页编号,对于整个文档包来说有意义,需要作为页码包含在TOC中.可能有另一种方法可以做到这一点,但我发现了Word的内置功能.

这将成为我的页码编号Sub中包含的函数.我现在是完成这个小项目的3/4,最后一个季度应该是直截了当的.

修改并清理了最终代码

Public Sub CreateOutline()

' from https://stackoverflow.com/questions/274814/getting-the-headings-from-a-word-document

Dim docOutline As Word.Document

Dim docSource As Word.Document

Dim rng As Word.Range

Dim strFootNum() As Integer

Dim astrHeadings As Variant

Dim strText As String

Dim intLevel As Integer

Dim intItem As Integer

Dim minLevel As Integer

Dim tabStops As Variant

Set docSource = ActiveDocument

Set docOutline = Documents.Add

minLevel = 5 'levels above this value won't be copied.

' Content returns only the

' main body of the document, not

' the headers and footer.

Set rng = docOutline.Content

astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading)

docSource.Select

ReDim strFootNum(0 To UBound(astrHeadings))

For i = 1 To UBound(astrHeadings)

With Selection.Find

.Text = Trim(astrHeadings(i))

.Wrap = wdFindContinue

End With

If Selection.Find.Execute = True Then

strFootNum(i) = Selection.Information(wdActiveEndPageNumber)

Else

MsgBox "No selection found", vbOKOnly

End If

Selection.Move

Next

docOutline.Select

With Selection.Paragraphs.tabStops

'.Add Position:=InchesToPoints(2), Alignment:=wdAlignTabLeft

.Add Position:=InchesToPoints(6), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderDots

End With

For intItem = LBound(astrHeadings) To UBound(astrHeadings)

' Get the text and the level.

' strText = Trim$(astrHeadings(intItem))

intLevel = GetLevel(CStr(astrHeadings(intItem)))

' Test which heading is selected and indent accordingly

If intLevel <= minLevel Then

If intLevel = "1" Then

strText = " " & Trim$(astrHeadings(intItem)) & vbTab & "1" & "." & "2" & "." & strFootNum(intItem) & vbCr

End If

If intLevel = "2" Then

strText = " " & Trim$(astrHeadings(intItem)) & vbTab & "1" & "." & "2" & "." & strFootNum(intItem) & vbCr

End If

If intLevel = "3" Then

strText = " " & Trim$(astrHeadings(intItem)) & vbTab & "1" & "." & "2" & "." & strFootNum(intItem) & vbCr

End If

If intLevel = "4" Then

strText = " " & Trim$(astrHeadings(intItem)) & vbTab & "1" & "." & "2" & "." & strFootNum(intItem) & vbCr

End If

If intLevel = "5" Then

strText = " " & Trim$(astrHeadings(intItem)) & vbTab & "1" & "." & "2" & "." & strFootNum(intItem) & vbCr

End If

' Add the text to the document.

rng.InsertAfter strText & vbLf

docOutline.SelectAllEditableRanges

' tab stop to set at 15.24 cm

'With Selection.Paragraphs.tabStops

' .Add Position:=InchesToPoints(6), _

' Leader:=wdTabLeaderDots, Alignment:=wdAlignTabRight

' .Add Position:=InchesToPoints(2), Alignment:=wdAlignTabCenter

'End With

rng.Collapse wdCollapseEnd

End If

Next intItem

End Sub

Private Function GetLevel(strItem As String) As Integer

' from https://stackoverflow.com/questions/274814/getting-the-headings-from-a-word-document

' Return the heading level of a header from the

' array returned by Word.

' The number of leading spaces indicates the

' outline level (2 spaces per level: H1 has

' 0 spaces, H2 has 2 spaces, H3 has 4 spaces.

Dim strTemp As String

Dim strOriginal As String

Dim intDiff As Integer

' Get rid of all trailing spaces.

strOriginal = RTrim$(strItem)

' Trim leading spaces, and then compare with

' the original.

strTemp = LTrim$(strOriginal)

' Subtract to find the number of

' leading spaces in the original string.

intDiff = Len(strOriginal) - Len(strTemp)

GetLevel = (intDiff / 2) + 1

End Function

此代码现在正在生成(根据我在test-doc.docx中找到的标题规范应该是什么):

This is heading one 1.2.1

This is heading two 1.2.1

This is heading two.one 1.2.1

This is heading two.three 1.2.1

This is heading one.two 1.2.2

This is heading three 1.2.2

This is heading four 1.2.2

This is heading five 1.2.2

This is heading five.one 1.2.3

This is heading five.two 1.2.3

除此之外,我通过使用docSource.select和docOutline.Select语句而不是using.Active解决了ActiveDocument切换问题.

再次感谢凯文,非常感谢:-)

菲尔

oracle vba 数组_vba:使用array中的文本从selection.find返回页码相关推荐

  1. oracle vba 数组_vba 创建数据库

    带你读<数据库与数据处理:Access 2010 实现 第2版>之一:数据处理与数据库 计算机基础课程系列教材点击查看第二章点击查看第三章数据库与数据处理:Access 2010 实现 第 ...

  2. oracle vba 数组_vba 数据库目录

    利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档 对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你 ...

  3. 用VBA在Word文档中每页页眉插入返回文档目录中相应位置的超链接

    [说明]此文中在页眉插入跳转到目录项的超链接的代码几经改进,但改进后并未删除改进之前的代码,是为了有个对比利于学习.如果想节约时间,该步骤可直接查看该部分最后一个代码块. 对于Word长文档,标准做法 ...

  4. oracle vba 数组_Excel VBA 连接各种数据库(二) VBA连接Oracle数据库

    (2019.08.02更新)本文内容在Windows10 企业版64bit下测试通过 本文主要内容: Oracle环境配置 ODBC驱动设置.第三方驱动下载 VBA连接Oracle连接方法 Oracl ...

  5. vba 数组赋值_VBA数组与字典解决方案第18讲:VBA中静态数组的定义及创建

    大家好,我们今日继续讲解VBA数组与字典解决方案,今日讲解第18讲:VBA数组如何定义,又该如何创建呢? 从这一讲开始,我们开始进入VBA数组阶段,VBA数组和工作表数组有着不同的意义,在很大程度上, ...

  6. vba遍历数组_VBA 在行中循环遍历的例子

    在单元格区域对象中循环: For Each c In Range("$A$2:$A$" & Cells(Rows.count, "A").End(xlU ...

  7. vba遍历数组_VBA代码解决方案的第59讲内容:如何在代码运行时创建数组

    大家好,我们今日继续讲解VBA代码解决方案的第59讲内容:如何在代码运行时创建数组 .数组大家并不陌生,在之前我讲过很多了,估计详细阐述数组的只有我这个平台可以找到了,今日讲的是在VBA中代码运行的时 ...

  8. vba查找数据并返回单元格地址_VBA积木代码中实现反向多值查找、LIKE模糊查找...

    分享成果,随喜真能量.大家好,今日内容仍是和大家分享VBA编程中常用的简单"积木"过程代码,第NO.114-NO.115则,内容是:FindPrevious反向查找.利用LIKE查 ...

  9. Shell中创建序列和数组(list、array)的方法

    Shell中创建序列和数组(list.array)的方法 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2015-07-09 我要评论 这篇文章主要介绍了Shell中创建序列和数组(li ...

  10. oracle查数据存入数组,如何在SQL查询中使用Oracle关联数组

    ODP.Net公开了将关联数组作为参数从C#传递到Oracle存储过程的能力.除非您尝试在sql查询中使用该关联数组中包含的数据,否则它是一个很好的功能. 原因是它需要上下文切换 – SQL语句需要S ...

最新文章

  1. 【Linux】ps命令
  2. 2021-03-20 包含生成树的性质
  3. 《程序员的修炼——从优秀到卓越》一一1.6 勿以专家自居
  4. 服务器点播直播系统,服务器点播直播系统
  5. P2685 [TJOI2012]桥(最短路+线段树)
  6. Visual Studio 2013 (CV版)编译错误【error C4996: 'sprintf': This function or variable may be unsafe. 】的解决方案
  7. paypal如何支付欧元_国际在线支付巨头——PayPal
  8. arduino的esp32程序无法上传_【arduino】arudino开发ESP32 SPIFFS文件上传方法
  9. 阿里二面:我们为什么要做分库分表?
  10. 学习OpenCV研究报告指出系列(二)源代码被编译并配有实例project
  11. 福建厦门的Acrel-2000E/B配电室综合监控系统
  12. FAT12文件系统基本格式
  13. pyscripter与python的关系_我用过的最好的python编辑器PyScripter
  14. 《我的青春谁做主》剧中人物星座分析
  15. 带修莫队 的 小优化 (针对yxc版本)
  16. 【网络安全】练习与复习十二
  17. 解决docker启动错误 error creating overlay mount to /var/lib/docker/overlay2
  18. 运筹学基础(02375)-有道云笔记
  19. android oreo 老机型,Android Oreo 通知新特性,这坑老夫先踩了
  20. Windows System32目录下所有文件详解

热门文章

  1. linux代码折叠,Sublime代码折叠
  2. 万用表测线路断点位置_万用表测电流口诀,正确使用方法
  3. PHP获取产量地址,得到与PHP服务器RAM(get server ram with php)
  4. python二维数据读取对齐_[Python ] Python 多维数组转换的维度对齐问题
  5. 计算机函数语法,clickhouse 函数语法
  6. 解决idea使用jdbc连接数据库失败的方法(针对驱动导入失败)
  7. 丁腈橡胶自然老化时间_氙灯老化试验箱和紫外光老化试验箱的区别
  8. 02_创建 CA 根证书和秘钥
  9. FHJ学长的心愿 QDUOJ 数论
  10. vagrant box磁盘扩容 亲测有效