【发布时间】: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