Search code examples
vb.netloopsdynamicms-wordoffice-interop

How to Create mutiple Tables on word with VB.NET


 Dim Linha3 = WordDoc.Paragraphs.Add
            Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
            Tabela.Range.Font.Name = "Calibri"
            Tabela.Range.Font.Size = 8
            Tabela.Columns(2).Width = 50
            Tabela.Columns(3).Width = 150
            Tabela.Columns(4).Width = 80
            Tabela.Columns(5).Width = 80
            Tabela.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter
            Tabela.Cell(1, 4).Merge(Tabela.Cell(1, 5))

My question is. I wanna do this on a loop, cuz I gotta create mutiple tables. The problem is, when the loop occours it gives me a error saying that like Columns(2) was alredy resized, and kind double it... So I was thinking I gotta create like "Tabela2" but how can I create a dynamic variable ?


Solution

  • The thing on add more tables was. On my code, here:

    Dim Tabela = WordDoc.Tables.Add(Linha3.Range, 7, 5)
    

    The "WordDoc.Paragraphs.Add" wasnt the same I was using on the WordDoc. I was using "Linha" for the rest of the document instead of Linha3. Idk why it make diference, but that is what was giving me error. So I just change it to "Linha", and it worked fine, with some custom editing to the Text of some cells.

    Dim WordDoc = WordApp.Documents.Add
    Dim Linha = WordDoc.Paragraphs.Add
    

    The answer is just dont add another WordDoc.Paragraphs, And u should be fine.