【问题标题】:How to create an automatic table of contents in word using Visual Basic 2010如何使用 Visual Basic 2010 在 Word 中创建自动目录
【发布时间】:2016-05-23 18:53:10
【问题描述】:

我正在尝试创建一个“作业模板创建者”。它基本上是为了大学,但它不是一项任务,所以没有人会在这里遇到麻烦。但我设法找到了使用 Visual Basic 2010 或 Visual Basic 语言创建 Word 文档的惊人代码示例。我对其进行了一些修改,以便它可以从文本框中获取信息并将它们放入文本框中。一切正常,创建新页面也正常。

但是如何使用下面列出的相同代码创建目录 (TOC):

Imports Microsoft.Office.Interop

公开课表1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
    Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
    Dim oRng As Word.Range
    Dim oShape As Word.InlineShape
    Dim oChart As Object
    Dim Pos As Double
    Dim what As Object = Word.WdGoToItem.wdGoToLine
    Dim which As Object = Word.WdGoToDirection.wdGoToLast
    Const wdPageBreak = 1


    'Start Word and open the document template.'
    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add

    'Insert a paragraph at the beginning of the document.'
    oDoc.Range.Font.Size = "16"
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = stuName.Text
    oPara1.Range.InsertParagraphAfter()

    'Insert a paragraph at the beginning of the document.'
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = centNo.Text
    oPara1.Range.InsertParagraphAfter()

    'Insert a paragraph at the beginning of the document.'
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = units.Text
    oPara1.Range.InsertParagraphAfter()

    'Insert a paragraph at the beginning of the document.'
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = tutor.Text
    oPara1.Range.InsertParagraphAfter()

    'Insert a paragraph at the beginning of the document.'
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = dueDate.Text
    oPara1.Range.InsertParagraphAfter()

    'Insert a new page'
    oWord.Selection.GoTo(what, which, Nothing, Nothing)
    Dim objSelection = oWord.Selection
    objSelection.InsertBreak(wdPageBreak)

    'Insert a table of contents'

    With oDoc
        .TablesOfContents.Add(Range:=oWord.Selection.Range, _
                   RightAlignPageNumbers:=True, _
                   UseHeadingStyles:=True, _
                   IncludePageNumbers:=True, _
                   AddedStyles:="Automatic Table 1", _
                   UseHyperlinks:=False, _
                   HidePageNumbersInWeb:=True, _
                   UseOutlineLevels:=True)
        .TablesOfContents(1).Range.Font.Name = "Arial Narrow"
        .TablesOfContents(1).Range.Font.Size = 11
        .TablesOfContents(1).TabLeader = Word.WdTabLeader.wdTabLeaderDots
        .TablesOfContents.Format = Word.WdTocFormat.wdTOCTemplate
    End With

    'Insert another blank page'



    'All done. Close this form.

    Me.Close()

End Sub

结束类

您可以看到那里已经有一个目录代码,但这不是我想要的。我希望它使用单词模板之一。自动的!

你可以看到它有点基本,我从这里得到它:How to automate Word from Visual Basic .NET to create a new document

所有这些都有效,但我想在 word 中创建“自动目录 1”模板。我不需要它一直更新,我只希望它添加一个然后程序会关闭。

抱歉,这个问题太长了,如果您不明白,我会尽快回复。但是,如果有人可以提供帮助。将不胜感激。

谢谢,

【问题讨论】:

  • 这类事情的一个小窍门:在宏中记录创建TOC的步骤。这将告诉您插入所需 TOC 类型的语法。以此为基础来调整您必须创建这种 TOC 的代码。由于您使用的是 VB.NET,因此它应该相当简单。
  • 我使用了宏,但它并没有真正帮助。不过还是谢谢
  • 怎么没有帮助?
  • Application.Templates( _ "C:\Users\Daniel\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx" _ ).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range _ , RichText:=True 这对我没有帮助
  • 感谢您提供更多信息。

标签: vb.net visual-studio-2010 visual-studio-2012 ms-word


【解决方案1】:

在 Word 中使用 TOC 域代码管理目录。按 Alt+F9 打开域代码显示。您需要重新创建此域代码。

有两种基本方法:

  1. 插入域代码。这不太“直观”,但您基本上可以根据您在文档字段代码中看到的内容来构建它。
  2. 使用 Document.TablesOfContents.Add 方法。从表面上看,这更直观,但需要您准确了解域代码及其开关(反斜杠 + 附加信息)的作用。

如果您想知道,可以查找有关 TOC 字段及其开关 (https://support.office.com/en-us/article/Field-codes-TOC-Table-of-Contents-field-1f538bc4-60e6-4854-9f64-67754d78d05c?ui=en-US&rs=en-US&ad=US) 的信息。但是对您来说更快更容易的是复制 { field 括号} 之间的内容并将其粘贴到您的代码中。 (注意:注意不要选择{括号}!)

'Insert TOC field at beginning of document
Dim rngDocStart as Word.Range
Set rngDocStart = ActiveDocument.Content
rngDocStart.Collapse
rngDocStart.Fields.Add Range:=rngDocStart, _
  Type:=wdFieldEmpty, _
  Text:="the text you copy from the field code", _
  PreserveFormatting:=False

【讨论】:

  • 感谢您的帮助。我在这里真的没有帮助我们两个。但是我复制了代码,我认为我做错了什么,因为这是我输入的:Dim rngDocStart As Word.Range rngDocStart = oDoc.Content rngDocStart.Collapse() rngDocStart.Fields.Add(Range:=rngDocStart, _ Type:="Automatic Table 1", _ Text:="TOC \o 1-3 \h \z \u", _ PreserveFormatting:=False) 当我运行它时,我得到一个错误代码 Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
  • 使用与我的代码示例中相同的类型。您唯一更改的是分配给 Text:= 的内容,并且在评论中显示的内容看起来完全正确。
  • 我还没有尝试过,因为由于丢失了硬盘空间,我现在没有安装 Office。当我最终把它拿回来时,我会告诉你的。并感谢您的提问:)
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
相关资源
最近更新 更多