HOW TO:建立範圍時排除段落標記
更新:2007 年 11 月
適用於 |
---|
本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。 專案類型
Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
每當您根據段落建立 Range 物件時,所有非列印字元 (如段落標記) 都會包括在範圍內。您可能要將文字從來源段落插入目的段落。如果您不想將目的段落分割成個別段落,則必須先移除來源段落的段落標記。此外,因為段落格式化資訊儲存在段落標記內,所以在將範圍插入現有段落時,您可能不想包含該資訊。
下列範例程序會宣告兩個字串變數,擷取現用文件的第一段和第二段內容,然後交換它們的內容。下列範例示範使用 MoveEnd 方法,從範圍移除段落標記,並在段落內插入文字。
若要在插入文字時控制段落結構
針對第一和第二個段落建立兩個範圍變數,然後使用 Text 屬性擷取其內容。
下列程式碼範例可以用於文件層級自訂中。
Dim firstRange As Word.Range = Me.Paragraphs(1).Range Dim secondRange As Word.Range = Me.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text
Word.Range firstRange = this.Paragraphs[1].Range; Word.Range secondRange = this.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text;
下列程式碼範例可以用於應用程式層級的增益集中。這個程式碼使用主動式文件 (Active Document)。
Dim document As Word.Document = Me.Application.ActiveDocument Dim firstRange As Word.Range = document.Paragraphs(1).Range Dim secondRange As Word.Range = document.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text
Word.Document document = this.Application.ActiveDocument; Word.Range firstRange = document.Paragraphs[1].Range; Word.Range secondRange = document.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text;
指派 Text 屬性,並將這兩個段落的文字互換。
firstRange.Text = secondString secondRange.Text = firstString
firstRange.Text = secondString; secondRange.Text = firstString;
依次選取每個範圍並暫停,以在訊息方塊中顯示結果。
firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text)
firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text);
使用 MoveEnd 方法調整 firstRange,讓段落標記不再是 firstRange 的一部分。
firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1)
object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move);
取代第一段中的其餘文字,指派新字串給該範圍的 Text 屬性。
firstRange.Text = "New content for paragraph 1."
firstRange.Text = "New content for paragraph 1.";
取代 secondRange 中的文字,包括段落標記。
secondRange.Text = "New content for paragraph 2."
secondRange.Text = "New content for paragraph 2.";
選取 firstRange 並暫停,以在訊息方塊中顯示結果,然後對 secondRange 執行同樣的動作。
因為已重新定義 firstRange 來排除段落標記,所以會保留段落的原始格式。不過,secondRange 中的段落標記上插入了句子,所以移除了個別段落。
firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text)
firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text);
這兩個範圍的原來內容都儲存為字串,所以您可以將文件還原成原來的狀態。
使用 MoveEnd 方法移動一個字元位置,以重新調整 firstRange,讓它包含段落標記。
firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1)
move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move);
刪除 secondRange。這會將段落三還原為原始的位置。
secondRange.Delete()
secondRange.Delete(ref missing, ref missing);
還原 firstRange 中原始段落文字。
firstRange.Text = firstString
firstRange.Text = firstString;
使用 Range 物件的 InsertAfter 方法,將原來第二段的內容插入 firstRange 之後,然後選取 firstRange。
firstRange.InsertAfter(secondString) firstRange.Select()
firstRange.InsertAfter(secondString); firstRange.Select();
文件層級自訂範例
若要在文件層級自訂中插入文字時控制段落結構
下列範例顯示文件層級自訂的完整方法。若要使用這個程式碼,請從專案中的 ThisDocument 類別 (Class) 中執行。
Private Sub ReplaceParagraphText() Dim firstRange As Word.Range = Me.Paragraphs(1).Range Dim secondRange As Word.Range = Me.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text firstRange.Text = secondString secondRange.Text = firstString firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1) firstRange.Text = "New content for paragraph 1." secondRange.Text = "New content for paragraph 2." firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1) secondRange.Delete() firstRange.Text = firstString firstRange.InsertAfter(secondString) firstRange.Select() End Sub
private void ReplaceParagraphText() { Word.Range firstRange = this.Paragraphs[1].Range; Word.Range secondRange = this.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text; firstRange.Text = secondString; secondRange.Text = firstString; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move); firstRange.Text = "New content for paragraph 1."; secondRange.Text = "New content for paragraph 2."; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move); secondRange.Delete(ref missing, ref missing); firstRange.Text = firstString; firstRange.InsertAfter(secondString); firstRange.Select(); }
應用程式層級增益集範例
若要在應用程式層級增益集中插入文字時控制段落結構
下列範例顯示應用程式層級增益集的完整方法。若要使用這個程式碼,請從專案中的 ThisAddIn 類別中執行。
Private Sub ReplaceParagraphText() Dim document As Word.Document = Me.Application.ActiveDocument Dim firstRange As Word.Range = document.Paragraphs(1).Range Dim secondRange As Word.Range = document.Paragraphs(2).Range Dim firstString As String = firstRange.Text Dim secondString As String = secondRange.Text firstRange.Text = secondString secondRange.Text = firstString firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=-1) firstRange.Text = "New content for paragraph 1." secondRange.Text = "New content for paragraph 2." firstRange.Select() MessageBox.Show(firstRange.Text) secondRange.Select() MessageBox.Show(secondRange.Text) firstRange.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1) secondRange.Delete() firstRange.Text = firstString firstRange.InsertAfter(secondString) firstRange.Select() End Sub
private void ReplaceParagraphText() { Word.Document document = this.Application.ActiveDocument; Word.Range firstRange = document.Paragraphs[1].Range; Word.Range secondRange = document.Paragraphs[2].Range; string firstString = firstRange.Text; string secondString = secondRange.Text; firstRange.Text = secondString; secondRange.Text = firstString; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); object charUnit = Word.WdUnits.wdCharacter; object move = -1; // move left 1 firstRange.MoveEnd(ref charUnit, ref move); firstRange.Text = "New content for paragraph 1."; secondRange.Text = "New content for paragraph 2."; firstRange.Select(); MessageBox.Show(firstRange.Text); secondRange.Select(); MessageBox.Show(secondRange.Text); move = 1; // move right 1 firstRange.MoveEnd(ref charUnit, ref move); secondRange.Delete(ref missing, ref missing); firstRange.Text = firstString; firstRange.InsertAfter(secondString); firstRange.Select(); }