Aracılığıyla paylaş


Nasıl Yapılır: Belgelerde Metin Biçimlendirme

Bir Microsoft Office Word belgesinde metin biçimlendirmek için Range nesnesini kullanabilirsiniz.

Uygulama alanı: Bu konudaki bilgiler Word 2007 ve Word 2010 uygulamalarının belge düzeyi projelerine ve uygulama düzeyi projelerine yöneliktir. Daha fazla bilgi için bkz. Office Uygulamalarında Kullanılabilir Özellikler ve Proje Türü.

Aşağıdaki örnekte, belgenin ilk paragrafı seçilir ve yazı tipi boyutu, adı ve hizalaması değiştirilir. Ardından aralık seçilir ve kodun sonraki bölümünün yürütülmesine ara vermek için bir ileti kutusu görüntülenir. Sonraki bölümde, belge düzeyi özelleştirmelerde Microsoft.Office.Tools.Word.Document konak öğesinin, uygulama düzeyi eklentilerde Microsoft.Office.Interop.Word.Document sınıfının Undo yöntemi üç kere çağrılır. Normal Indent (Normal Girintileme) stili görüntülenir ve koda ara vermek için bir ileti kutusu görüntülenir. Ardından Undo yöntemi bir kere çağrılır ve bir ileti kutusu görüntülenir.

Belge Düzeyi Özelleştirmesi Örneği

Belge düzeyi özelleştirmesi kullanarak metin biçimlendirme

  • Aşağıdaki kod örneği belge düzeyi özelleştirmelerinde kullanılabilir. Bu kodu kullanmak için projenizdeki ThisDocument sınıfından kodu çalıştırın.

    Private Sub RangeFormat()
    
        ' Set the Range to the first paragraph.
        Dim rng As Word.Range = Me.Paragraphs(1).Range
    
        ' Change the formatting. To change the font size for a right-to-left language, 
        ' such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14
        rng.Font.Name = "Arial"
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    
        rng.Select()
        MessageBox.Show("Formatted Range")
    
        ' Undo the three previous actions.
        Me.Undo(Times:=3)
    
        rng.Select()
        MessageBox.Show("Undo 3 actions")
    
        ' Apply the Normal Indent style.
        rng.Style = "Normal Indent"
    
        rng.Select()
        MessageBox.Show("Normal Indent style applied")
    
        ' Undo a single action.
        Me.Undo()
    
        rng.Select()
        MessageBox.Show("Undo 1 action")
    End Sub
    
    private void RangeFormat() 
    { 
        // Set the Range to the first paragraph. 
        Word.Range rng = this.Paragraphs[1].Range;
    
        // Change the formatting. To change the font size for a right-to-left language, 
        // such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14; 
        rng.Font.Name = "Arial"; 
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
        rng.Select(); 
        MessageBox.Show("Formatted Range"); 
    
        // Undo the three previous actions. 
        object numTimes3 = 3; 
        this.Undo(ref numTimes3); 
    
        rng.Select(); 
        MessageBox.Show("Undo 3 actions"); 
    
        // Apply the Normal Indent style. 
        object indentStyle = "Normal Indent"; 
        rng.set_Style(ref indentStyle); 
    
        rng.Select(); 
        MessageBox.Show("Normal Indent style applied"); 
    
        // Undo a single action. 
        object numTimes1 = 1; 
        this.Undo(ref numTimes1); 
    
        rng.Select(); 
        MessageBox.Show("Undo 1 action"); 
    }
    

Uygulama Düzeyi Eklentileri Örneği

Uygulama düzeyi eklentisini kullanarak metin biçimlendirme

  • Aşağıdaki kod örneği uygulama düzeyi eklentilerinde kullanılabilir. Bu örnekte etkin belge kullanılır. Bu kodu kullanmak için projenizdeki ThisAddIn sınıfından kodu çalıştırın.

    Private Sub RangeFormat()
    
        ' Set the Range to the first paragraph.
        Dim document As Word.Document = Me.Application.ActiveDocument
        Dim rng As Word.Range = document.Paragraphs(1).Range
    
        ' Change the formatting. To change the font size for a right-to-left language, 
        ' such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14
        rng.Font.Name = "Arial"
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    
        rng.Select()
        MessageBox.Show("Formatted Range")
    
        ' Undo the three previous actions.
        document.Undo(Times:=3)
    
        rng.Select()
        MessageBox.Show("Undo 3 actions")
    
        ' Apply the Normal Indent style.
        rng.Style = "Normal Indent"
    
        rng.Select()
        MessageBox.Show("Normal Indent style applied")
    
        ' Undo a single action.
        document.Undo()
    
        rng.Select()
        MessageBox.Show("Undo 1 action")
    End Sub
    
    private void RangeFormat()
    {
        // Set the Range to the first paragraph. 
        Word.Document document = this.Application.ActiveDocument;
        Word.Range rng = document.Paragraphs[1].Range;
    
        // Change the formatting. To change the font size for a right-to-left language, 
        // such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14;
        rng.Font.Name = "Arial";
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
        rng.Select();
        MessageBox.Show("Formatted Range");
    
        // Undo the three previous actions. 
        object numTimes3 = 3;
        document.Undo(ref numTimes3);
    
        rng.Select();
        MessageBox.Show("Undo 3 actions");
    
        // Apply the Normal Indent style. 
        object indentStyle = "Normal Indent";
        rng.set_Style(ref indentStyle);
    
        rng.Select();
        MessageBox.Show("Normal Indent style applied");
    
        // Undo a single action. 
        object numTimes1 = 1;
        document.Undo(ref numTimes1);
    
        rng.Select();
        MessageBox.Show("Undo 1 action");
    }
    

Ayrıca bkz.

Görevler

Nasıl Yapılır: Belgelerde Aralıkları Tanımlama ve Seçme

Nasıl Yapılır: Word Belgelerine Metin Ekleme

Nasıl Yapılır: Belgelerde Metin Arama

Başvuru

SizeBi()