Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Insert SEQ field thru VBA

    I suppose it's time to throw in the towel. I've been searching for this syntax all day, but to no avail.

    I have multiple tables in my doc which I need to add a SEQ field to the first cell of every table in row 1.

    I managed to cobble together the below, but I can't seem to find the right syntax right before the .Fields?

    Code:
    Sub Macro1()
        Dim tb As Table
        For Each tb In ActiveDocument.Tables
            With tb.Rows(1).Cells(1)
                .Fields.Add Range:=Selection.Range, _
                            Type:=wdFieldEmpty, _
                            text:="SEQ Table1 \n", _
                            PreserveFormatting:=True
            End With
        Next tb
    End Sub
    Two other questions.

    1) How can I add the text Table to the beginning of the SEQ field? Like in Excel, I tried to add extra quotes, but maybe Word and Excel are not the same in that respect.

    So in the end, Table 1, Table 2, and so on.

    2) I know I can add /r1 to the SEQ field which will make the numbering start over at 1, but in my case, the Tables and actually, Table 1.1., Table 1.2., Table 1.3., and so on

    The next section will be Table 2.1., Table 2.2., Table 2.3., and so on

    Any thoughts?

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table, Rng As Range
    With ActiveDocument
    For Each Tbl In .Tables
      Set Rng = Tbl.Cell(1, 1).Range
      Rng.End = Rng.End - 1
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ Table \n", PreserveFormatting:=False
    Next
    End With
    Application.ScreenUpdating = True
    End Sub
    For something more elaborate, you could use:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table, Rng As Range
    With ActiveDocument
    For Each Tbl In .Tables
      Set Rng = Tbl.Cell(1, 1).Range
      Rng.End = Rng.End - 1
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ SubTable \n", PreserveFormatting:=False
      Rng.InsertBefore "."
      Rng.Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ Table \n", PreserveFormatting:=False
      Rng.InsertBefore "Table: "
    Next
    End With
    Application.ScreenUpdating = True
    End Sub
    After which you might change the field switch for the first pair of field in each sequence (i.e. 1.1, 2.1, etc.) from "\n" to "\r 1", "\r 2", etc. as appropriate.

    However, it seems to me your needs might better be met via the use of a multi-level list numbering setup, perhaps linked to a heading Style. Without further details of what you're trying to achieve, though it's impossible to give specific advice.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Thanks Paul

    I'm trying to make this as user friendly as possible. Running one macro seems like the best option. Simply building a template would be the best method I suppose, but since most people would not know how to use the template, I'm trying to craft the best/easiest solution.

    I agree about the multi-level list numbering setup, but most people in my office don't even use the numbering. This macro to add the SEQ field is more or less use at the very end of create our documents to ensure the formatting is the same.

    Again, since this is for multiple users, I'm just not 100% sure the right way to go, but this is a good start which I think I can work with.

    I like the option two macro, but it produces 1.1, 2.2, 3.3 etc, so I must not understand.

    I changed the macro to Table {SEQ Table \r1}.{SEQ Table \n} and it produces 1.1, 1.2, 1.3 etc

    This is the part I don't understand.

    After which you might change the field switch for the first pair of field in each sequence (i.e. 1.1, 2.1, etc.) from "\n" to "\r 1", "\r 2", etc. as appropriate.
    Also, where do I insert another "." to go on the end? 1.1.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by jrb View Post
    I'm trying to make this as user friendly as possible. Running one macro seems like the best option. Simply building a template would be the best method I suppose, but since most people would not know how to use the template, I'm trying to craft the best/easiest solution.
    Using a template could hardly be as complicated as this process will be. What could be simpler than using File>New and choosing the template?
    Quote Originally Posted by jrb View Post
    I agree about the multi-level list numbering setup, but most people in my office don't even use the numbering. This macro to add the SEQ field is more or less use at the very end of create our documents to ensure the formatting is the same.
    That's hardly a reason to not use it. Besides which, if you needed it to do so, a macro could be used to apply that numbering scheme instead of messing around with SEQ fields. better still would be to teach the users to use Word properly. If they're not using numbering & styles properly, all they're doing is creating maintence problems for subsequent users and increasing the likelihood of document corruption.
    Quote Originally Posted by jrb View Post
    I like the option two macro, but it produces 1.1, 2.2, 3.3 etc, so I must not understand.
    As I said:
    you might change the field switch for the first pair of field in each sequence (i.e. 1.1, 2.1, etc.) from "\n" to "\r 1", "\r 2", etc. as appropriate.
    So, where you want a series to start at 1.1, change "\n" to "\r 1" for both fields. Where you want it to re-start as 2.1 change "\n" to "\r 2" for the first field and to "\r 1" for the second field. This is hardly something you could expect your end-users to do but, unless, there is an obvious logic about the document structure that dictates when to re-start the numbering sequence, that's what will be required. Hence my reference to multi-level list numbering.
    Quote Originally Posted by jrb View Post
    Also, where do I insert another "." to go on the end? 1.1.
    You could add:
    Rng.InsertAfter "."
    after or before:
    Rng.InsertBefore "."
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    I tried adding Rng.InsertAfter "." before and even after Rng.InsertBefore "." and the result is

    1..1, 1..2 etc.

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table, Rng As Range
    With ActiveDocument
    For Each Tbl In .Tables
      Set Rng = Tbl.Cell(1, 1).Range
      Rng.End = Rng.End - 1
      Rng.InsertAfter "."
      Rng.End = Rng.End - 1
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ SubTable \n", PreserveFormatting:=False
      Rng.InsertBefore "."
      Rng.Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ Table \n", PreserveFormatting:=False
      Rng.InsertBefore "Table: "
    Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. The Following User Says Thank You to macropod For This Useful Post:

    jrb (2016-04-02)

  8. #7
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Thank you Paul, this is very nice. I will take all of your comments and see what will work best.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •