我正嘗試創建一個'Assignment Template creator'。這基本上是爲了大學,但這不是一項任務,所以沒有人會在這裏遇到麻煩。但我設法找到了令人驚歎的代碼示例,使用Visual Basic 2010或Visual Basic語言創建Word文檔。我已經修改了一下,以便它可以從文本框中獲取信息並將它們放在文本框中。所有的工作和創建一個新的頁面的作品。如何使用Visual Basic 2010在Word中創建自動目錄表
但我怎麼可以創建下面列出的使用相同的代碼目錄(TOC)的表:
Imports Microsoft.Office.Interop
公共類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模板。我不需要它一直更新,我只是希望它添加一個,然後程序將關閉。
對不起,這是一個很長的問題,如果你不明白,我會盡我所能回覆。但如果有人可以幫忙。這將非常感激。
謝謝,
丹
這種事情的提示:記錄在宏中創建目錄的步驟。這會告訴你插入你想要的那種TOC的語法。以此爲基礎調整您必須創建這種TOC的代碼。既然你使用VB.NET,它應該是相當直接的。 –
我使用了宏,但它並沒有真正的幫助。但是,無論如何謝謝 –
它沒有幫助嗎? –