HOW TO:以程式設計方式在文件中設定文字格式
您可以使用 Range 物件,格式化 Microsoft Office Word 文件中的文字。
**適用於:**本主題中的資訊適用於 Word 2013 和 Word 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
下列範例會選取文件中的第一段,並且變更字型大小、字型名稱和對齊方式。接著,會選取這個範圍,並在執行下一個程式碼區段之前顯示訊息方塊來暫停。下一個區段會呼叫 Microsoft.Office.Tools.Word.Document 主項目 (適用於文件層級自訂) 或 Microsoft.Office.Interop.Word.Document 類別 (Class) (適用於應用程式層級增益集) 的 Undo 方法三次。該範例會套用「內文縮排」樣式,並且顯示訊息方塊來暫停程式碼。然後,程式碼會呼叫 Undo 方法一次,並顯示訊息方塊。
文件層級自訂範例
若要使用文件層級自訂來格式化文字
下列範例可以用於文件層級自訂中。若要使用這個程式碼,請從專案中的 ThisDocument 類別中執行。
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"); }
應用程式層級增益集範例
若要使用應用程式層級增益集來格式化文字
下列範例可以用於應用程式層級的增益集中。本範例使用主動式文件。若要使用這個程式碼,請從專案中的 ThisAddIn 類別中執行。
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"); }
請參閱
工作
參考
SizeBi