Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Cell / Range (Word 2003)

    I want to add a merge field to each cell in a table, excluding gutter columns. With the exception of the first cell, the merge field is to be preceded by a {NextRecord} field. I have been successful doing this by going to each cell in each row and adding the field. I thought that it would be more efficient to use a For Each construct for each cell in the table.

    The code below is fine if I add text to each cell, but adding a field is giving me trouble. I don't know what to put as the Range.

    w = ActiveDocument.Tables(1).Cell(1, 1).Width
    Dim cl As Cell
    For Each cl In ActiveDocument.Tables(1).Range.Cells
    If cl.Width > w * 0.9 Then ' skip gutter columns
    cl.Range.Fields.Add RANGE???, Type:=wdFieldNext
    cl.Range.Fields.Add RANGE???, Type:=wdFieldMergeField, Text:="""MailingAddress"""
    End If
    Next
    ActiveDocument.Fields(1).Delete ' remove {Next} from 1st cell

    Have I approached this incorrectly? Was I better off with my original code (below)?

    With ActiveDocument.Tables(1)
    For r = 1 To .Rows.Count
    .Cell(r, 1).Range.Select
    Selection.Collapse
    For c = 1 To .Columns.Count
    If Selection.Cells(1).Width = w Then
    If Not Selection.InRange(.Cell(1, 1).Range) Then
    ActiveDocument.MailMerge.Fields.AddNext Range:=Selection.Range
    End If
    ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    , Text:="""MailingAddress"""
    End If
    If Not Selection.InRange(.Cell(.Rows.Count, .Columns.Count).Range) Then
    Selection.MoveRight wdCell, 1
    End If
    Next c
    Next r

    Thanks,
    Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cell / Range (Word 2003)

    Try this (note the reversal of the insertion of the two fields. This is because they are inserted at the beginning of the cell.

    Dim w As Single
    Dim cl As Cell
    Dim rn As Range
    w = ActiveDocument.Tables(1).Cell(1, 1).Width
    For Each cl In ActiveDocument.Tables(1).Range.Cells
    If cl.Width > w * 0.9 Then ' skip gutter columns
    Set rn = cl.Range
    rn.Collapse
    ActiveDocument.Fields.Add Range:=rn, _
    Type:=wdFieldMergeField, Text:="""MailingAddress"""
    ActiveDocument.Fields.Add Range:=rn, _
    Type:=wdFieldNext
    End If
    Next cl
    ActiveDocument.Fields(1).Delete ' remove {Next} from 1st cell

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Cell / Range (Word 2003)

    Thanks very much.
    Richard

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Cell / Range (Word 2003)

    Hans, I've been using Fields.AddNext to avoid the addition of * MergeFormat to the NextRecord field (see below code). Is there a way that I can avoid MergeFormat as part of the field? Next isn't accurate, but on a label, NextRecord * MergeFormat ALWAYS pushes the two fields to two lines, and this confuses the user when formatting the sheet of labels. Next works just fine; in fact, it converts to NextRecord as the merge runs. I'd just prefer to present the user with a "real" merge-type field if that can be done. Thanks, Richard

    ActiveDocument.MailMerge.Fields.AddNext Range:=rng ' adds Next field
    'ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldNext ' adds /* Merge Format to NextRecord field

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cell / Range (Word 2003)

    Try

    ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldNext, PreserveFormatting:=False

    or

    ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldEmpty, Text:="NEXT", PreserveFormatting:=False

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Cell / Range (Word 2003)

    Thank you, as always. I should have thought of that.
    Richard

Posting Permissions

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