Nasıl yapılır: Belge Özellikleriyle Word Tablolarını Program Aracılığıyla Doldurma

Aşağıdaki örnekte, belgenin başında Microsoft Office Word tablosu oluşturulur ve konak begesinin özellikleriyle doldurulur.

Uygulama hedefi: Bu konudaki bilgiler, Word 2013 ve Word 2010 için belge düzeyi projeler ve uygulama düzeyi projelere yöneliktir. Daha fazla bilgi edinmek için, bkz. Office Uygulaması ve Proje Türüne Göre Kullanılabilir Özellikler.

Belge Düzeyi Özelleştirmesinde Tablo Doldurma

Tablo oluşturmak ve belge özellikleriyle tabloları doldurmak için

  1. Belgenin başına aralığı ayarlayın.

    Dim rng As Word.Range = Me.Range(Start:=0, End:=0)
    
    object start = 0, end = 0; 
    Word.Range rng = this.Range(ref start, ref end); 
    
  2. Tablo için bir başlık ekleyin ve paragraf işaretleri ekleyin.

    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With
    
    rng.InsertBefore("Document Statistics"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End); 
    
  3. Tabloyu belgeye o aralıkta ekleyin.

    rng.Tables.Add(Range:=Me.Paragraphs.Item(2).Range, NumRows:=3, NumColumns:=2)
    
    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Tabloyu biçimlendirin ve bir stil uygulayın.

    With Me.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional" 
    End With
    
    Word.Table tbl = this.Tables[1];
    tbl.Range.Font.Size = 12; 
    tbl.Columns.DistributeWidth(); 
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName); 
    
  5. Hücrelere belge özelliklerini ekleyin.

    With Me.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"
    
        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With
    
    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();
    

Aşağıdaki örnekte tamamlanmış yordam gösterilmektedir.Bu kodu kullanmak için projenizdeki ThisDocument sınıfından kodu çalıştırın.

Private Sub CreateDocumentPropertyTable()
    Dim rng As Word.Range = Me.Range(Start:=0, End:=0)

    ' Insert a title for the table and paragraph marks. 
    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With 

    ' Add the table.
    rng.Tables.Add(Range:=Me.Paragraphs.Item(2).Range, NumRows:=3, NumColumns:=2)

    ' Format the table and apply a style. 
    With Me.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional" 
    End With 

    ' Insert document properties into cells. 
    With Me.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"

        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()

        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With 
End Sub
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();
}

Uygulama Düzeyi Eklentilerde Tabloları Doldurma

Tablo oluşturmak ve belge özellikleriyle tabloları doldurmak için

  1. Belgenin başına aralığı ayarlayın.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range( _
        Start:=0, End:=0)
    
    object start = 0, end = 0;
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Range(ref start, ref end);
    
  2. Tablo için bir başlık ekleyin ve paragraf işaretleri ekleyin.

    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With
    
    rng.InsertBefore("Document Statistics");
    rng.Font.Name = "Verdana";
    rng.Font.Size = 16;
    rng.InsertParagraphAfter();
    rng.InsertParagraphAfter();
    rng.SetRange(rng.End, rng.End);
    
  3. Tabloyu belgeye o aralıkta ekleyin.

    rng.Tables.Add(Range:=Me.Application.ActiveDocument.Paragraphs.Item(2).Range, _
        NumRows:=3, NumColumns:=2)
    
    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Tabloyu biçimlendirin ve bir stil uygulayın.

    With Me.Application.ActiveDocument.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional" 
    End With
    
    Word.Table tbl = document.Tables[1];
    tbl.Range.Font.Size = 12;
    tbl.Columns.DistributeWidth();
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Hücrelere belge özelliklerini ekleyin.

    With Me.Application.ActiveDocument.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"
    
        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With
    
    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();
    

Aşağıdaki örnekte tamamlanmış yordam gösterilmektedir.Bu kodu kullanmak için projenizdeki ThisAddIn sınıfından kodu çalıştırın.

Private Sub CreateDocumentPropertyTable()
    Dim rng As Word.Range = Me.Application.ActiveDocument.Range( _
        Start:=0, End:=0)

    ' Insert a title for the table and paragraph marks. 
    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With 

    ' Add the table.
    rng.Tables.Add(Range:=Me.Application.ActiveDocument.Paragraphs.Item(2).Range, _
        NumRows:=3, NumColumns:=2)

    ' Format the table and apply a style. 
    With Me.Application.ActiveDocument.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional" 
    End With 

    ' Insert document properties into cells. 
    With Me.Application.ActiveDocument.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"

        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()

        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With 
End Sub
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();
}

Ayrıca bkz.

Görevler

Nasıl yapılır: Program Aracılığıyla Word Tabloları Oluşturma

Nasıl yapılır: Word Tablolarında Hücrelere Program Aracılığıyla Metin ve Biçimlendirme Ekleme

Nasıl yapılır: Word Tablolarına Program Aracılığıyla Satır ve Sütun Ekleme

Kavramlar

Office Çözümlerinde İsteğe Bağlı Parametreler