Vb.net 如何使用Visual Basic 2010在word中创建自动目录

Vb.net 如何使用Visual Basic 2010在word中创建自动目录,vb.net,visual-studio-2010,visual-studio-2012,ms-word,Vb.net,Visual Studio 2010,Visual Studio 2012,Ms Word,我正在尝试创建“作业模板创建者”。这基本上是为了上大学,但这不是一项任务,所以这里没有人会遇到麻烦。但是我设法找到了一个使用VisualBasic2010或VisualBasic语言创建word文档的惊人代码示例。我对它做了一点修改,这样它就可以从文本框中获取信息并将其放入文本框中。所有这些都有效,创建新页面也有效 但是如何使用下面列出的相同代码创建目录(TOC): Imports Microsoft.Office.Interop 公开课表格1 Private Sub Button1_Clic

我正在尝试创建“作业模板创建者”。这基本上是为了上大学,但这不是一项任务,所以这里没有人会遇到麻烦。但是我设法找到了一个使用VisualBasic2010或VisualBasic语言创建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
末级

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

你可以看到它有点基本,我从这里得到:

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

对不起,这是一个很长的问题,如果你不明白,我会在可能的时候回复你。但如果有人能帮忙的话。非常感谢

谢谢


Dan

目录在Word中使用TOC字段代码进行管理。按Alt+F9切换字段代码显示。您需要重新创建此字段代码

有两种基本的方法:

  • 插入字段代码。这不太“直观”,但实际上可以根据在文档字段代码中看到的内容构建它
  • 使用Document.TablesOfContents.Add方法。从表面上看,这更直观,但要求您确切了解字段代码及其开关(反斜杠+附加信息)的命令作用
  • 如果您想知道,可以在TOC字段及其开关()上查找信息。但是对您来说,复制{field方括号}之间的内容并将其粘贴到代码中会更快更容易。(注意:小心不要选择{括号}!)


    这类事情的提示:在宏中记录创建TOC的步骤。这将告诉您插入所需TOC类型的语法。将此作为调整代码的基础,您必须创建此类TOC。由于您使用的是VB.NET,所以应该是相当直接的。我使用了宏,但它没有真正的帮助。但无论如何还是要谢谢你。它怎么没有帮助呢?
    Application.Templates(“C:\Users\Daniel\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\build-In Building Blocks.dotx”)。buildingblockents(“自动表1”)。插入其中:=Selection.Range,RichText:=True
    这并没有真正帮助我向您提供更多信息。谢谢您的帮助。我真的没有帮我们两个。但是我复制了代码,我假设我做错了什么,因为我键入的是:
    Dim rngDocStart As Word.Range rngDocStart=oDoc.Content rngDocStart.Collapse()rngDocStart.Fields.Add(Range:=rngDocStart,Type:=“自动表1”,“文本:=”TOC\o 1-3\h\z\u“,uuPreserveFormatting:=False)
    当我运行它时,我得到一个类型不匹配的错误代码。(HRESULT的异常:0x80020005(DISP_E_TYPEMISMATCH))使用与我的代码示例中相同的类型。您唯一要更改的是分配给Text:=的内容,这在您在注释中显示的内容中看起来完全正确。我还没有尝试过它,因为由于丢失了硬盘空间,我现在还没有安装Office。当我最终把它拿回来时,我会让你知道的。谢谢你的提问:)
    '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