Udostępnij za pośrednictwem


Porady: Programowane formatowanie tekstu w dokumentach

Można użyć Range obiektu do formatowania tekstu w dokumencie programu Microsoft Office Word.

Zastosowanie: Informacje przedstawione w tym temacie mają zastosowanie do projektów na poziomie dokumentu i aplikacji obsługiwanych w programach Word 2013 i Word 2010. Aby uzyskać więcej informacji, zobacz Funkcje dostępne w aplikacjach pakietu Office i typ projektu.

W poniższym przykładzie zaznacza pierwszy akapit w dokumencie i zmienia rozmiar czcionki, nazwa czcionki i wyrównanie.Następnie zaznacza zakres i wyświetla okno komunikatu, aby wstrzymać przed wykonaniem następnej sekcji kodu.Dalej wywołań sekcji Undo metody Document element hosta (dla dostosowania poziomu dokumentu) lub Document klasy (dla poziomu aplikacji dodatek) trzy razy.Stosuje styl Normalny wcięcie i wyświetla okno komunikatu, aby wstrzymać kod.A następnie wywołania kodu Undo raz, metoda i wyświetla okno komunikatu.

Przykład dostosowywania poziomie dokumentu

Aby sformatować tekst za pomocą dostosowania poziomu dokumentu

  • Poniższy przykład może służyć w dostosowania poziomu dokumentu.Aby użyć tego kodu, należy uruchomić go z ThisDocument klasy w projekcie.

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

Poziom aplikacji w przykładzie

Aby sformatować tekst przy użyciu dodatek programu poziomie aplikacji

  • Poniższy przykład może służyć w poziomie aplikacji dodatek.W tym przykładzie użyto aktywnego dokumentu.Aby użyć tego kodu, należy uruchomić go z ThisAddIn klasy w projekcie.

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

Zobacz też

Zadania

Porady: Programowane definiowanie i zaznaczanie zakresów w dokumentach

Porady: Programowane wstawianie tekstu w dokumentach programu Word

Porady: Programowane wyszukiwanie i zastępowanie tekstu w dokumentach