#1
|
|||
|
|||
Design macro insert number table rows
Hi there,
In a lot of documents I need to number each cell of the first column of each row of a table. Does someone have a macro or can someone creat a macro that does the following: For each table with specific table.style: "xxxx" Insert number based on the list of the heading and row of the cell. For example: |
#2
|
|||
|
|||
Something like this might get you close:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oTbl As Table Dim oRng As Range Dim strList As String Dim oRow As Row Dim lngRow As Long For Each oTbl In ActiveDocument.Tables If oTbl.Style = "Table Grid" Then Set oRng = oTbl.Range oRng.Collapse wdCollapseStart oRng.Move wdParagraph, -1 If Not oRng.Paragraphs(1).OutlineLevel < 10 Then Do oRng.Move wdParagraph, -1 Loop Until oRng.Paragraphs(1).OutlineLevel < 10 End If End If strList = oRng.Paragraphs(1).Range.ListFormat.ListString lngRow = 0 For Each oRow In oTbl.Rows lngRow = lngRow + 1 oRow.Cells(1).Range.Text = strList & "." & lngRow Next oRow Next oTbl lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Hi, Thanks for your effort
It gives an error at Code:
strList = oRng.Paragraphs(1).Range.ListFormat.ListString Object variable or With block variable not set |
#4
|
|||
|
|||
Would have to see a sample document with a couple of tables and the preceding heading text.
|
#5
|
|||
|
|||
Please find attached document
|
#6
|
|||
|
|||
That error is from the style line.
Change If oTbl.Style = "Table Grid" Then to If oTbl.Style = "Protix Table" Then I can't figure out how to get it to skip the first row. Right now it overwrites the "ID" in row one. |
#7
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oTbl As Table Dim oRng As Range Dim strList As String Dim oRow As Row Dim lngRow As Long For Each oTbl In ActiveDocument.Tables If oTbl.Style = "Protix Table" Then Set oRng = oTbl.Range oRng.Collapse wdCollapseStart oRng.Move wdParagraph, -1 If Not oRng.Paragraphs(1).OutlineLevel < 10 Then Do oRng.Move wdParagraph, -1 Loop Until oRng.Paragraphs(1).OutlineLevel < 10 End If End If strList = oRng.Paragraphs(1).Range.ListFormat.ListString lngRow = 0 For Each oRow In oTbl.Rows If oRow.Index > 1 Then lngRow = lngRow + 1 oRow.Cells(1).Range.Text = strList & "." & lngRow End If Next oRow Next oTbl lbl_Exit: Exit Sub End Sub |
#8
|
|||
|
|||
It works up until I have other tables in the document aswell
If there is another table style used before the 'protix table' then it doesn't work If there is another table style used after a 'protix table' then it also gives that table an ID number. |
Tags |
insert number |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save | aaghd72 | Word VBA | 8 | 02-18-2019 11:22 AM |
How to find number of coma and then add that number of rows in word using macro? | PRA007 | Word VBA | 7 | 05-27-2015 10:45 PM |
Creating a table for a variable number of rows | OllieOnline | Mail Merge | 1 | 03-27-2013 02:48 PM |
Using macro to add variable number of rows to a protected word table | Julia | Word Tables | 1 | 01-09-2013 06:04 AM |
Fixing number or rows in a table | burnsie | Word | 2 | 07-12-2011 02:59 AM |