none
Insert merge field in table cell RRS feed

  • Question

  • I have some code to insert merge fields into a table row where 1st cell and last cell have 2 merge fields and all cells in middle have 1 merge field.

    I got command not available error during inserting 2nd merge field in 1st cell. I tried to collapse 1st cell range to end before inserting 2nd merge field but still got the error.

    What am I doing wrong? Please review code snippet below for issues. Thanks.

                var firstRow = table.Rows[1];
                var firstCellRange = firstRow.Cells[1].Range;
                firstCellRange.Collapse(ref objDirection);
                InsertMergedField(firstCellRange, "Start:" + element.FieldName);
                for (int i = 1; i <= firstRow.Cells.Count; i++)
                {
                    var cell = firstRow.Cells[i];
                    objDirection = (i == 1) ? WdCollapseDirection.wdCollapseEnd : WdCollapseDirection.wdCollapseStart;
                    cell.Range.Collapse(ref objDirection);
                    InsertMergedField(cell.Range, element.ElementList[i - 1].DataFieldName);
                }
                var lastCellRange = firstRow.Cells[firstRow.Cells.Count].Range;
                lastCellRange.Collapse(ref objDirection);
                InsertMergedField(lastCellRange, "End:" + element.FieldName);

     


            private bool InsertMergedField(Word.Range range, string fieldName)
            {
                Object missing = Type.Missing;
                Field field = null;
                if (range.Fields != null)
                {
                    object objType = Word.WdFieldType.wdFieldMergeField;
                    object objFieldName = fieldName;
                    field = range.Fields.Add(range, ref objType, ref objFieldName, ref missing);
                }
                return (field != null);
               
            }

     

     

     


    Huu Huyen
    Friday, September 16, 2011 4:15 PM

Answers

  • Hi Huu Huyen

    As your code sample doesn't include a lot of the actual values being used, it's rather difficult to follow exactly what it's doing and even more difficult to test, but...

    <<I got command not available error during inserting 2nd merge field in 1st cell. I tried to collapse 1st cell range to end before inserting 2nd merge field but still got the error.>>

    I suspect the problem might have to do with the table cell structures. In addition to any cell content you see, the little "sunshine" symbol at the bottom right corner of a table cell contains two characters: "\r"+(char)7. These essentially store internal information about the cell structure within the table structure (so that Word can display the text in a table form).

    It's quite possible that collapsing the cell range to the end is putting the Range within this structural area, rather than within the text area. (Although I would have expected it to place the range into the following cell, but if you're doing this on the last cell, then you're getting into the structural area of the row, instead).

    I have two suggestions:

    1. Try moving the end of the Range back by one, or two characters (Range.MoveEnd Word.WdUnits.wdCharacter, -1) Something like

        rng = wdApp.Selection.Cells(1).Range
        rng.Collapse(Word.WdCollapseDirection.wdCollapseStart)
        fld = rng.Fields.Add(rng, Word.WdFieldType.wdFieldPage)
        rng = Selection.Cells(1).Range
        rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        rng.MoveEnd(Word.WdUnits.wdCharacter, -1)

    2. Use the field object as your starting point. The Field.Result and the Field.Code properties returns Range objects. Collapse to the end of that, them move the Start to the right one or two characters so that you don't end up inside the field code structure.

    You'll need to test a bit...


    Cindy Meister, VSTO/Word MVP
    Sunday, September 18, 2011 9:32 AM
    Moderator
  • Hi,

    I had the same problem you have. The workaround I found was to redefine the range like this:

    Dim wdRange As Word.Range
    wdRange = wdTable.Cell(row, column).Range
    wdRange = doc.Range(CInt(wdRange.Start), wdRange.End - 1)
    
    

    And then add the field code, which in your case is a merge field.

    Kind regards,

    Carlos Mallen

    Tuesday, September 20, 2011 12:13 AM

All replies

  • Hi Huu Huyen

    As your code sample doesn't include a lot of the actual values being used, it's rather difficult to follow exactly what it's doing and even more difficult to test, but...

    <<I got command not available error during inserting 2nd merge field in 1st cell. I tried to collapse 1st cell range to end before inserting 2nd merge field but still got the error.>>

    I suspect the problem might have to do with the table cell structures. In addition to any cell content you see, the little "sunshine" symbol at the bottom right corner of a table cell contains two characters: "\r"+(char)7. These essentially store internal information about the cell structure within the table structure (so that Word can display the text in a table form).

    It's quite possible that collapsing the cell range to the end is putting the Range within this structural area, rather than within the text area. (Although I would have expected it to place the range into the following cell, but if you're doing this on the last cell, then you're getting into the structural area of the row, instead).

    I have two suggestions:

    1. Try moving the end of the Range back by one, or two characters (Range.MoveEnd Word.WdUnits.wdCharacter, -1) Something like

        rng = wdApp.Selection.Cells(1).Range
        rng.Collapse(Word.WdCollapseDirection.wdCollapseStart)
        fld = rng.Fields.Add(rng, Word.WdFieldType.wdFieldPage)
        rng = Selection.Cells(1).Range
        rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        rng.MoveEnd(Word.WdUnits.wdCharacter, -1)

    2. Use the field object as your starting point. The Field.Result and the Field.Code properties returns Range objects. Collapse to the end of that, them move the Start to the right one or two characters so that you don't end up inside the field code structure.

    You'll need to test a bit...


    Cindy Meister, VSTO/Word MVP
    Sunday, September 18, 2011 9:32 AM
    Moderator
  • Field.Result gives the range and after collapsing to the end, I can insert the next field using that range without moving back any characters. Thanks a lot for your help.
    Huu Huyen
    Monday, September 19, 2011 4:50 PM
  • Hi,

    I had the same problem you have. The workaround I found was to redefine the range like this:

    Dim wdRange As Word.Range
    wdRange = wdTable.Cell(row, column).Range
    wdRange = doc.Range(CInt(wdRange.Start), wdRange.End - 1)
    
    

    And then add the field code, which in your case is a merge field.

    Kind regards,

    Carlos Mallen

    Tuesday, September 20, 2011 12:13 AM