none
Cannot add field to word document in c# RRS feed

  • Question

  • Here is the scenario:

    My firm has a reporting format they use to report technical findings.  I created an addin that will autogenerate a blank, properly formatted table that can then be used to enter data.  However, for the title, I want to put in a LISTNUM field, so the the title would be "Finding XX: NAME", where XX is the autonumbering field.

    However, every time I try the following code I get either "This command is not available" or some drawing error.  This occurs with:

    doc.Fields.Add(block.Cell(1,2).Range, Word.wdFieldType.wdFieldListNum);

    Whenever I do:

    doc.Fields.Add(Globals.BlockAddin.Application.Selection.Range, Word.wdFieldType.wdFieldListNum);

    It works, but puts the field in the first cell.

    Here is slightly more code.  Any thoughts on how to make this work?

                Word.Document doc = Globals.BlockAddin.Application.ActiveDocument; //Shortcut reference to the doc

                Word.Range currRange = Globals.BlockAddin.Application.Selection.Range;  //Get the location of the cursor

                Word.Table block = doc.Tables.Add(currRange, 7, 2, Word.WdDefaultTableBehavior.wdWord9TableBehavior,
                    Word.WdAutoFitBehavior.wdAutoFitContent); //The table for the finding

                Word.WdColor shade = (Word.WdColor)(224 * 65536) + (224 * 256) + 224; //Calculate the 24-bit color from RGB(224,224,224)

                //Configure widths
                block.Columns[1].Width = (float)(1.46 * 72); //1 inch = 72 points.  For some reason the InchesToPoints method was causing an error.
                block.Columns[2].Width = (float)(5.2 * 72);

                //Configure cell contents and formatting
                block.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                string rating = getRating(cmbImpactRating.SelectedItem.Label, cmbExploitabilityRating.SelectedItem.Label);
                block.Cell(1, 1).Range.Text = rating;
                switch (rating)
                {
                    case "High":
                        block.Cell(1, 1).Shading.ForegroundPatternColor = (Word.WdColor)(255);
                        break;
                    case "Medium":
                        block.Cell(1, 1).Shading.ForegroundPatternColor = (Word.WdColor)(102 * 256) + (255);
                        break;
                    case "Low":
                        block.Cell(1, 1).Shading.ForegroundPatternColor = (Word.WdColor)(204 * 256) + (255);
                        break;
                }

                block.Cell(1, 2).Range.Text = (string)BlockAddin.Settings["findingTitlePrefix"];
                if ((bool)BlockAddin.Settings["findingAutoNumber"] == true)
                {
                    //block.Cell(1, 2).Range.Text += " " + ++BlockAddin.GenerateCount;
                    int count = ++BlockAddin.GenerateCount;
                    string text = (count == 1) ? @"FindingNum \s 01" : @"FindingNum";

    doc.Fields.Add(block.Cell(1,2).Range, Word.wdFieldType.wdFieldListNum);

                }
                block.Cell(1, 2).Range.Text += ": ";
                block.Cell(1, 2).Range.Text = block.Cell(1, 2).Range.Text.Replace("\r", ""); //The += apparently adds a newline every time?
                block.Cell(1, 2).Shading.BackgroundPatternColor = shade;

       

    //....more code

    • Moved by Lisa Zhu Wednesday, August 15, 2012 7:20 AM Word related (From:Visual C# General)
    Monday, August 13, 2012 5:23 PM

Answers

  • I can't do a C# version right now, but you may be able to translate the following VBA (I'm using a fixed table number and cell position for the example):

    Sub putFieldInCell()

    Dim fld As Field
    Dim rng As Range
    Set rng = ActiveDocument.Tables(1).Cell(2, 2).Range
    rng.SetRange rng.Start, rng.End - 1
    Set fld = rng.Fields.Add(rng, Word.WdFieldType.wdFieldListNum, , False)
    fld.Result.InsertAfter ": "
    Set fld = Nothing
    Set rng = Nothing
    End Sub


    Peter Jamieson

    Thursday, August 16, 2012 8:13 AM

All replies

  • So I was somewhat able to solve this, but had another issue:

                block.Cell(1, 2).Range.Paragraphs.Add();
                Word.Range rng = block.Cell(1, 2).Range.Paragraphs[1].Range;
                rng.Text = (string)BlockAddin.Settings["findingTitlePrefix"] + " ";
                if ((bool)BlockAddin.Settings["findingAutoNumber"] == true)
                {
                    //block.Cell(1, 2).Range.Text += " " + ++BlockAddin.GenerateCount;
                    int count = ++BlockAddin.GenerateCount;
                    object text = (count == 1) ? @"FindingNum \s 01" : @"FindingNum";

                    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);

                    object listnum = Word.WdFieldType.wdFieldListNum;
                    object preserve = true;

                    rng.Fields.Add(rng, ref listnum, ref text, ref preserve);
                }

                rng.Text += ": ";

    The issue here is that the colon is placed BEFORE the field.  However, if I change the collapse or remove it, thenthe field just replaces the text.

    Monday, August 13, 2012 6:46 PM
  • Hi Pendraggon87 ,

    From your description , I ‘d like to move this post to  the most related forum .

    You will get  better support  and  may have more luck getting answers here .

    Thanks for your understanding .

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 15, 2012 7:18 AM
  • I can't do a C# version right now, but you may be able to translate the following VBA (I'm using a fixed table number and cell position for the example):

    Sub putFieldInCell()

    Dim fld As Field
    Dim rng As Range
    Set rng = ActiveDocument.Tables(1).Cell(2, 2).Range
    rng.SetRange rng.Start, rng.End - 1
    Set fld = rng.Fields.Add(rng, Word.WdFieldType.wdFieldListNum, , False)
    fld.Result.InsertAfter ": "
    Set fld = Nothing
    Set rng = Nothing
    End Sub


    Peter Jamieson

    Thursday, August 16, 2012 8:13 AM