Ask a questionAsk a question
 

AnswerMacro to insert numbers into a table

  • Thursday, November 05, 2009 5:35 AMPGAEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Sunday, November 08, 2009 10:53 AMPGAEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.


All Replies

  • Thursday, November 05, 2009 1:58 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Thursday, November 05, 2009 10:49 PMPGAEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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).

  • Friday, November 06, 2009 12:39 AMPGAEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Friday, November 06, 2009 1:25 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 08, 2009 10:53 AMPGAEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.