Navigating a Range


To go to the next element of a particular type without affecting the selection, you can use the GoTo method of the Range object. GoTo doesn’t affect the Range object it is called on but instead returns a new Range object that represents the resulting Range after calling GoTo. The GoTo method takes by reference four optional object parameters. The first parameter, the What parameter, can be passed a member of the WdGoToItem enumeration:

  • wdGoToBookmark
  • wdGoToComment
  • wdGoToEndnote
  • wdGoToEquation
  • wdGoToField
  • wdGoToFootnote
  • wdGoToGrammaticalError
  • wdGoToGraphic
  • wdGoToHeading
  • wdGoToLine
  • wdGoToObject
  • wdGoToPage
  • wdGoToPercent
  • wdGoToProofreadingError
  • wdGoToRevision
  • wdGoToSection
  • wdGoToTable

The second parameter, the Which parameter, can be passed a member of the WdGoToDirection enumeration: wdGoToAbsolute, wdGoToFirst, wdGoToLast, wdGoToNext, wdGoToPrevious, or wdGoToRelative. The wdGoToAbsolute value can be used to go to the n-th item of the type specified by the What parameter.

The third parameter, the Count parameter, is passed the number of the item to get and is affected by the second parameter. For example, if What is passed wdGoToLine and Count is passed 1, then depending on the Which parameter, GoTo could go to the next line after the Range (wdGoToNext) or the first line in the document (wdGoToAbsolute) or the line previous to the current Range (wdGoToPrevious).

The fourth parameter, the Name parameter, can be passed a name if the What argument specifies an element identifiable by name: wdGoToBookmark, wdGoToComment, or wdGoToField.

GoToNext and GoToPrevious are simpler versions of the GoTo method that only take the What parameter and go to the next or previous instance of the type of object specified by the What parameter.

GoTo Methods Sample

Object missing = System.Reflection.Missing.Value;
 // Generate some random text in the document.
Word.Range r = Globals.ThisAddIn.Application.ActiveDocument.Range(ref missing, ref missing);</pre>
System.Text.StringBuilder builder = new System.Text.StringBuilder();
 Random rand = new Random();

// Generate random string
 for (int i = 0; i < 200; i++)
 {
 builder.AppendLine(rand.NextDouble().ToString());
 }

// Insert string into Range
 r.Text = builder.ToString();
 int maxPage = (int)r.get_Information(
 Word.WdInformation.wdNumberOfPagesInDocument);

// GoTo to navigate the pages
 for (int page = 1; page <= maxPage; page++)
 {
 object what = Word.WdGoToItem.wdGoToPage;
 object which = Word.WdGoToDirection.wdGoToAbsolute;
 object count = page;
 object sentence = Word.WdUnits.wdSentence;

Word.Range r2 = r.GoTo(ref what, ref which,
 ref count, ref missing);
 r2.Expand(ref sentence);

MessageBox.Show(String.Format(
 "First sentence is {0} starting at position {1}.",
 r2.Text, r2.Start));
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s