Rohit
Rohit

Reputation: 10236

Move my cursor to the end of the Text in MsWord using C#?

This question might sound very simple but I am unable to find any Solution for it.What I am trying to do is to Move my Cursor Positon in MsWord to the end of the text.My code is as follows

  object StartPos = 0;
  object Endpos = 1;
  Microsoft.Office.Interop.Word.Range rng= oDoc.Range(ref StartPos, ref Endpos);
  rng.Text = "This is first line Word from C#";

The output is

I This is first line Word from C#

however i want something like this

This is first line Word from C# I

Thanks All

Upvotes: 4

Views: 11295

Answers (6)

Mohammad Sadeq Sirjani
Mohammad Sadeq Sirjani

Reputation: 650

The only thing you need is that access to Word documents and find the current range. Then you have to find the current position of the cursor. You need to use this code:

var selection = Document.Application.Resource.Range;
object position = selection.Resource.StoryLength - 1;
var range = Document.Range(ref position, ref position);
range.Select();

Now, the cursor is at the end of its position.

Upvotes: 0

Rohit
Rohit

Reputation: 10236

Well thank you all for your response I seem to have figured out a simple Solution.I tried to modify Hassan's Solution.There might be a much easier approach but as of now I have Found This

object NewEndPos = rng.StoryLength-1;
        rng = oDoc.Range(ref NewEndPos, ref NewEndPos);
        rng.Select();

Upvotes: 6

Simon MᶜKenzie
Simon MᶜKenzie

Reputation: 8664

How about this? It's the same as pressing Ctrl-Shift-End. Note that word is the word application, not the document. It's assumed that the correct active document is already selected.

word.Selection.EndKey(WdUnits.wdStory);

Upvotes: 2

wbit
wbit

Reputation: 192

Similar question - Position cursor at start/end of Word document

That answer goes into more detail about the which and the what. The answers are kind of mashed up between c# and vb so I include yet another answer here, that uses a slightly different method of going to the last part of the document.

My two cents:

//vb does this kind of thing for them
//but in c# we need an object we can pretend is null
object oMissing = System.Reflection.Missing.Value;

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc; //whenever i read this i think 'hodor'
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  ref oMissing, ref oMissing);

object StartPos = 0;
object Endpos = 1;

Microsoft.Office.Interop.Word.Range rng = oDoc.Range(ref StartPos, ref Endpos);
rng.Text = "This is first line Word from C#";

//object what = Word.WdGoToItem.wdGoToLine;
//I couldn't get wdGoToLine to work but wdGoToPercent was happy
object what = Word.WdGoToItem.wdGoToPercent;
object which = Word.WdGoToDirection.wdGoToLast;

oWord.Selection.GoTo(ref what, ref which, oMissing, oMissing);

This method is slightly different in that it doesn't tell word to move the cursor to the last line, but the last percentage of the document, which I will have to assume is 100. That would be the end of the line in a one-line document, but if the cursor is on the first line (at the beginning) and we tell Word to goto the last line, nothing happens: we are already there, at the beginning of the last line.

Upvotes: 1

Hassan Boutougha
Hassan Boutougha

Reputation: 3929

rng= oDoc.Range(ref Endpos, ref Endpos);
rng.Select();

Upvotes: 1

Oleg  Ignatov
Oleg Ignatov

Reputation: 865

Try this method:

oDoc.GoTo(ref what, ref which, ref missing, ref missing);

Upvotes: 0

Related Questions