Rellenar mediante programación tablas de Word con propiedades de documento

En el siguiente ejemplo se crea una tabla de Microsoft Office Word en la parte superior del documento y se rellena con las propiedades del documento host.

Se aplica a: la información de este tema se aplica a proyectos de nivel de documento y proyectos de complementos de VSTO para Word. Para obtener más información, consulte Características disponibles por aplicación de Office lication y tipo de proyecto.

Rellenar tablas en una personalización de nivel de documento

Para crear una tabla y rellenarla con propiedades de documento

  1. Establezca el intervalo en la parte superior del documento.

    object start = 0, end = 0; 
    Word.Range rng = this.Range(ref start, ref end);
    
  2. Inserte un título para la tabla e incluya marcas de párrafo.

    rng.InsertBefore("Document Statistics"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End);
    
  3. Agregue la tabla al documento en el intervalo.

    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Dé formato a la tabla y aplique un estilo.

    Word.Table tbl = this.Tables[1];
    tbl.Range.Font.Size = 12; 
    tbl.Columns.DistributeWidth(); 
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Inserte las propiedades del documento en celdas.

    tbl.Cell(1, 1).Range.Text = "Document Property";
    tbl.Cell(1, 2).Range.Text = "Value";
    
    tbl.Cell(2, 1).Range.Text = "Subject";
    tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
    tbl.Cell(3, 1).Range.Text = "Author";
    tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    

    En el ejemplo siguiente se muestra el procedimiento completo. Para usar este código, ejecútelo desde la clase ThisDocument del proyecto.

    private void CreateDocumentPropertyTable() 
    { 
        object start = 0, end = 0; 
        Word.Range rng = this.Range(ref start, ref end); 
    
        // Insert a title for the table and paragraph marks. 
        rng.InsertBefore("Document Statistics"); 
        rng.Font.Name = "Verdana"; 
        rng.Font.Size = 16; 
        rng.InsertParagraphAfter(); 
        rng.InsertParagraphAfter(); 
        rng.SetRange(rng.End, rng.End); 
    
        // Add the table.
        rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
        // Format the table and apply a style. 
        Word.Table tbl = this.Tables[1];
        tbl.Range.Font.Size = 12; 
        tbl.Columns.DistributeWidth(); 
    
        object styleName = "Table Professional";
        tbl.set_Style(ref styleName); 
    
        // Insert document properties into cells. 
        tbl.Cell(1, 1).Range.Text = "Document Property";
        tbl.Cell(1, 2).Range.Text = "Value";
    
        tbl.Cell(2, 1).Range.Text = "Subject";
        tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
        tbl.Cell(3, 1).Range.Text = "Author";
        tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    }
    

Rellenar tablas en un complemento de VSTO

Para crear una tabla y rellenarla con propiedades de documento

  1. Establezca el intervalo en la parte superior del documento.

    object start = 0, end = 0;
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Range(ref start, ref end);
    
  2. Inserte un título para la tabla e incluya marcas de párrafo.

    rng.InsertBefore("Document Statistics");
    rng.Font.Name = "Verdana";
    rng.Font.Size = 16;
    rng.InsertParagraphAfter();
    rng.InsertParagraphAfter();
    rng.SetRange(rng.End, rng.End);
    
  3. Agregue la tabla al documento en el intervalo.

    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Dé formato a la tabla y aplique un estilo.

    Word.Table tbl = document.Tables[1];
    tbl.Range.Font.Size = 12;
    tbl.Columns.DistributeWidth();
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Inserte las propiedades del documento en celdas.

    tbl.Cell(1, 1).Range.Text = "Document Property";
    tbl.Cell(1, 2).Range.Text = "Value";
    
    tbl.Cell(2, 1).Range.Text = "Subject";
    tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
    tbl.Cell(3, 1).Range.Text = "Author";
    tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    

    En el ejemplo siguiente se muestra el procedimiento completo. Para usar este código, ejecútelo desde la clase ThisAddIn del proyecto.

    private void CreateDocumentPropertyTable()
    {
        object start = 0, end = 0;
        Word.Document document = this.Application.ActiveDocument;
        Word.Range rng = document.Range(ref start, ref end);
    
        // Insert a title for the table and paragraph marks. 
        rng.InsertBefore("Document Statistics");
        rng.Font.Name = "Verdana";
        rng.Font.Size = 16;
        rng.InsertParagraphAfter();
        rng.InsertParagraphAfter();
        rng.SetRange(rng.End, rng.End);
    
        // Add the table.
        rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
        // Format the table and apply a style. 
        Word.Table tbl = document.Tables[1];
        tbl.Range.Font.Size = 12;
        tbl.Columns.DistributeWidth();
    
        object styleName = "Table Professional";
        tbl.set_Style(ref styleName);
    
        // Insert document properties into cells. 
        tbl.Cell(1, 1).Range.Text = "Document Property";
        tbl.Cell(1, 2).Range.Text = "Value";
    
        tbl.Cell(2, 1).Range.Text = "Subject";
        tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
        tbl.Cell(3, 1).Range.Text = "Author";
        tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    }