如何使用Visual Basic 2010

时间:2016-02-12 15:01:05

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

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

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

Imports Microsoft.Office.Interop

Public Class Form1

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

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

对不起,这是一个很长的问题,如果你不明白,我会尽可能回复。但如果有人可以提供帮助。非常感谢。

谢谢,

1 个答案:

答案 0 :(得分:0)

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

有两种基本方法可以解决这个问题:

  1. 插入域代码。这种情况不那么直观,但您实际上可以从文档字段代码中看到的内容构建它。
  2. 使用Document.TablesOfContents.Add方法。从表面上看,这更直观,但要求您准确了解字段代码及其开关(反斜杠+附加信息)的命令。
  3. 如果您想知道,可以在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 bracket}之间的内容并将其粘贴到代码中。 (注意:小心不要选择{方括号!)

    '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