簡體   English   中英

如何在循環的每次迭代中添加一個表?

[英]How can I add a table in each iteration of my loop?

我正在使用VBA從excel生成word文檔。 我有一個for循環,我想添加一個[1行,1列,有邊界]表。 這是用戶可以將他們的評論放在word文檔中的區域。 當我嘗試添加.table.add時,我遇到了不同的錯誤,包括對象錯誤。 這是我到目前為止:

Sub GenDocumentables()
    Worksheets("checklist").Activate
    Dim wdApp As Word.Application
    Set wdApp = New Word.Application
    Dim saveName As String
    Dim NumberOfCells As Integer
    With wdApp
        .Visible = True
        .Activate
        'Debug.Print .Version
        .Documents.Add
        With .Selection
            .InsertBreak Type:=wdPageBreak
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .BoldRun
            .Font.Size = 13
            .TypeText "Documentable Items for "
            .TypeText Range("d4").Value
            .BoldRun
            .TypeParagraph
        End With
        NumberOfCells = Range("a4", Range("a3").End(xlDown)).Count
        For loopcounter = 1 To 2 ' NumberOfCells
            With .Selection
                .Font.Bold = False
                .Style = wdStyleHeading3
                .TypeText Range("a3").Offset(loopcounter, 0).Value & " - "
                .TypeText Range("a3").Offset(loopcounter, 4).Value
                .TypeParagraph
                .Font.Size = 10
                .TypeText Range("a3").Offset(loopcounter, 5).Value
                .TypeParagraph
                .Font.Italic = True
                .TypeText "<<Please enter your commentary here. Ensure all aspects of the check content are met>>"
                .TypeParagraph
                '-------------------ADD TABLE HERE-------------------
            End With
        Next
        Set myRange = ActiveDocument.Range(0, 0)
        ActiveDocument.TablesOfContents.Add Range:=myRange, UseFields:=False, UseHeadingStyles:=True, LowerHeadingLevel:=3, UpperHeadingLevel:=1
        With .Selection
            .GoTo What:=wdGoToSection, Which:=wdGoToFirst
            .InsertBreak Type:=wdPageBreak
        End With
        saveName = Environ("UserProfile") & "\Desktop\My Word Doc_" & Format(Now, "yyyy-mm-dd hh-mm-ss") & ".docx"
        .ActiveDocument.SaveAs2 saveName
        '.ActiveDocument.Close
        '.Quit
    End With
    MsgBox "done!"
End Sub

由於Selection沒有方法,你得到對象錯誤.Table為了解決這個問題,你需要使用這行:

Set newTable = wdApp.ActiveDocument.Tables.Add(SomeRange,1,1)

Tables是Document的成員,您可以使用wdApp.ActiveDocument部分檢索它。 在這里,需要定義SomeRange才能使其正常工作。

要嘗試運行此代碼,請嘗試添加一些變量以使其更容易。 返回聲明其他變量的位置添加以下內容:

Dim myRange As Word.Range
Dim wdDoc As Word.Document
Dim newTable As Word.Table

在進入循環之前,在創建文檔后添加:

Set wdDoc = wdApp.ActiveDocument

接下來,在循環內部,但在End With(.Selection)之后,您可以添加:

Set myRange = wdDoc.Range(wdDoc.Content.End - 1, wdDoc.Content.End)
Set newTable = wdDoc.Tables.Add(myRange, 1, 1)
newTable.Cell(1, 1).Range.Text = "Hello"
Set myRange = wdDoc.Range(wdDoc.Content.End - 1, wdDoc.Content.End)
myRange.Select

讓我們來看看它的作用。

  • 首先,它將自定義變量myRange設置為文檔中的最后一個字符。 這允許我們將表放在已經創建的所有內容之下,
  • 接下來,它在此位置創建一個表,大小為1x1。
  • 此表中第一個單元格的值設置為“Hello”
  • 然后下一行AGAIN將范圍設置為文件的底部,然后選擇它。 這是必要的,因為創建表會將選擇更改為新表的內部。 跳過此行將使您運行表的循環INSIDE的下一次迭代。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM