Reputation: 826
I need to add a row after first row in MSWord Document
. Document have already one row and two columns containing Country
in first cell and Partner
in second cell as values of both cells. Problem with the following code is it only work with at-least two rows.
Macro should be able to add row and write predefined strings in both columns. This can be done in two ways
By adding a row at the end of the table and reaching to it and writing the strings. If i do this then my question is how can I reach to the last row of the table?
By adding second row Everytime as mentioned in the following code.
Set doc = Documents.Open("C:\Users\dell\Desktop\LATAM.DOCX", , , , , , , , , , , True)
Set rng = doc.Content
rng.Tables(1).Rows.Add (rng.Tables(1).Rows(2)) 'Here I am getting error 'required memeber of collect not exist.
Set Cell = rng.Tables(1).Cell(2, 1)
Set Cell2 = rng.Tables(1).Cell(2, 2)
Cell.Range.Text = UT
If UT = "CROATIA" Then Cell2.Range.Text = "ERSTE SECURITIES ZAGREB"
If UT = "CZECH REPUBLIC" Then Cell2.Range.Text = "ERSTE GROUP"
Latam.docx look like this:
Requirement: After Macro it should be like this with values in added cells.
UPDATED ANSWER:
Set doc = Documents.Open("C:\Users\ibnea\Desktop\List of Countries & Companies CEEMEA & LATAM.DOCX", , , , , , , , , , , True)
Set rng = doc.Content.Tables(1).Rows.Add
rng.Range.Font.Bold = False
rng.Cells(1).Range = UT
rng.Cells(2).Range = UT2
Upvotes: 0
Views: 957
Reputation: 25663
Use Rows.Add
without a parameter and the new row will be inserted at the end of the table. The parameter (as stated in the Help topic for Rows.Add
enables code to insert new rows before a specific row.
In order to work with the new row, declare an object variable and assign the row to it when it's created. It's also a good idea to do this with the Table
object. That way it's possible to address the objects directly, rather than needing to always use something like rng.Tables(index).Rows(index)
not only is this simpler to read and write, it also executes more quickly.
So, based on the code in the question my recommendation:
Dim tbl as Word.Table, rw as Word.Row
Set tbl = rng.Tables(1)
Set rw = tbl.Rows.Add
rw.Cells(1).Range.Text = "cell content"
rw.Cells(2).Range.Text = "other cell content"
Upvotes: 1
Reputation: 366
Maybe I don't really understand your question but when I open a Word File and create a table with 1 row (as header) and 2 columns and enter "Country" and "Partner" then I can create a new row in this table with this code:
Option Explicit
Sub addRow()
Dim tbl As Table
'set tble variable to Table with Index 1 in your document
Set tbl = ActiveDocument.Tables(1)
'add a row to this table at the end
tbl.Rows.Add
'get last row in this table
Dim lastRow As Variant
lastRow = ActiveDocument.Tables(1).Rows.Count
'write to the last row
ActiveDocument.Tables(1).Cell(lastRow, 1).Range = "your value in last row / column 1"
ActiveDocument.Tables(1).Cell(lastRow, 2).Range = "your value in last row / column 2"
End Sub
You said your list has only one row. This code now adds a row and fills in column 1 and 2 the value whatever is on the right side of the equality sign. It's still not clear what UT is and where it comes from. But anyway you can now access the last cells with this code.
Upvotes: 1