Sdílet prostřednictvím


Postupy: Formátování textu v dokumentech prostřednictvím kódu programu

Lze použít Range objekt, který chcete formátovat text v dokumentu aplikace Microsoft Office Word.

Platí pro: Informace v tomto tématu se vztahují na projekty na úrovni dokumentu i na úrovni aplikace ve Wordu 2013 a ve Wordu 2010. Další informace najdete v tématu Dostupné funkce podle aplikací systému Office a typů projektu.

Následující příklad vybere prvního odstavce v dokumentu a změny velikosti písma, název písma a zarovnání.Pak vybere oblast a zobrazí okno se zprávou pozastavení před provedením další část kódu.Další část volání Undo metoda Document hostitel zboží (pro úpravy úrovni dokumentu) nebo Document třídy (pro aplikace úroveň doplněk) třikrát.Použije styl Normální odsazení a zobrazí okno se zprávou chcete-li pozastavit kód.Potom volání kódu Undo jednou, metoda a zobrazí okno se zprávou.

Příklad úpravy na úrovni dokumentu

Chcete-li formátovat text pomocí přizpůsobení úrovni dokumentu

  • V následujícím příkladu lze použít v dokumentu úroveň přizpůsobení.Chcete-li použít tento kód, spusťte jej z ThisDocument tříd v projektu.

    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"); 
    }
    

Aplikace úroveň doplněk příklad

Chcete-li formátovat text pomocí aplikace úroveň doplněk

  • V aplikace úroveň doplněk lze použít následující příklad.Tento příklad používá aktivní dokument.Chcete-li použít tento kód, spusťte jej z ThisAddIn tříd v projektu.

    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");
    }
    

Viz také

Úkoly

Postupy: Definování a výběr oblastí v dokumentech prostřednictvím kódu programu

Postupy: Vkládání textu do dokumentů aplikace Word prostřednictvím kódu programu

Postupy: Hledání a nahrazování textu v dokumentech prostřednictvím kódu programu