HOW TO:在文件中格式化文字
更新:2007 年 11 月
適用於 |
---|
本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。 專案類型
Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
您可以使用 Range 物件,格式化 Microsoft Office Word 文件中的文字。
下列範例會選取文件中的第一段,並且變更字型大小、字型名稱和對齊方式。接著,會選取這個範圍,並在執行下一個程式碼區段之前顯示訊息方塊來暫停。下一個區段會呼叫 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. 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. 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. 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. 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"); }