Microsoft Developer Network >
Forums Home
>
Microsoft ISV Community Center Forums
>
Visual Basic for Applications (VBA)
>
Macro to insert numbers into a table
Macro to insert numbers into a table
- Hi
I have a word document with 2-column layout .
The word document has a table with a header row repeating on all pages (I also have the "Allow row to break across pages" turned OFF) .
See http://img689.imageshack.us/i/sample.jpg/ for a screenshot of what I mean.
I would like to create a macro that goes through the rows in the table (there is only one table in the document) and inserts the numbers 1, 2, 3 ... as shown in the screenshot.
And I want the numbering to restart from 1 on every page. i.e, in page 2 the first row of the table should start from number 1.
How would I go about doing this? Any ideas?
Answers
- No problem.
I wrote a macro that loops through every page, selects all the text in the current page and then picks the first field in the selection ie., Selection.Fields[1]
and changes it's code
Selection.Fields[1].Code.Text = Selection.Fields[1].Code.Text & "\r1"
That did the trick.
- Marked As Answer byTim LiMSFT, ModeratorThursday, November 12, 2009 2:56 AM
All Replies
- i don't have exactly what you need off hand but this will help you get closer to figure it out
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.Tables(1).Rows(2).Cells(2).Range.Text = "mylastname
Dim lastname As String: lastname = Selection.Tables(1).Rows(2).Cells(2).Range.Text
the above:
gets the first table in the document (count:=1)
sets the second second row value in the second column with the text "mylastname"
gets the value into a variable
as i said, it is not exact for your need but you can work it out to get your tables in order or by name and work with the cells. there should be some methods to add rows to it also. i don't know the code off hand. the above was the first i have ever written code for word.
hope it helps
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial Jeff - thanks, but that did not help my situtation.
Just to make things clear, The document is produced using OpenXML SDK (the data comes from a database). The document doesn't have any numbers when it is produced. I would like to run a word macro that inserts the numbers (and restarts the numbering on each page).- I figured that I could use a SEQ to get the numbering going - {SEQ myNum}
But I need to reset the numbering on each page using {SEQ myNum \r1} = How do I do this programmatically? - i wish i had the answer for you. i would suggest a google/bing seacrch for it. word is something that i have not done much with and it is still kind of awkward for me. just have not spent the time to figure it out.
i hope you find the answer. if you don't get any response on this, i might suggest starting a new thread specifically about your last post question.
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial - No problem.
I wrote a macro that loops through every page, selects all the text in the current page and then picks the first field in the selection ie., Selection.Fields[1]
and changes it's code
Selection.Fields[1].Code.Text = Selection.Fields[1].Code.Text & "\r1"
That did the trick.
- Marked As Answer byTim LiMSFT, ModeratorThursday, November 12, 2009 2:56 AM

