2016-02-12 71 views
0

我正嘗試創建一個'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模板。我不需要它一直更新,我只是希望它添加一個,然後程序將關閉。

對不起,這是一個很長的問題,如果你不明白,我會盡我所能回覆。但如果有人可以幫忙。這將非常感激。

謝謝,

+0

這種事情的提示:記錄在宏中創建目錄的步驟。這會告訴你插入你想要的那種TOC的語法。以此爲基礎調整您必須創建這種TOC的代碼。既然你使用VB.NET,它應該是相當直接的。 –

+0

我使用了宏,但它並沒有真正的幫助。但是,無論如何謝謝 –

+0

它沒有幫助嗎? –

回答

0

內容表在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)上的信息。但更快,更容易的是複製{字段括號}之間的內容並將其粘貼到代碼中。 (注意:不要選擇{括號}!)

'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 
+0

感謝您的幫助。我真的不幫助我們兩個人。但我複製了代碼,我假設我做錯了什麼,因爲這是我輸入的內容:'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)**當我運行它時, **類型不匹配的錯誤代碼。 (來自HRESULT的異常:0x80020005(DISP_E_TYPEMISMATCH))' –

+0

使用與我的代碼示例中相同的類型。你改變的唯一的東西就是分配給Text:=的東西,而且在評論中顯示的內容看起來完全正確。 –

+0

@DanielMorris是否有建議爲你工作? –

相關問題