Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 537
Rohit Avatar asked Sep 06 '25 03:09

Rohit


2 Answers

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();
like image 88
Rohit Avatar answered Sep 09 '25 17:09

Rohit


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);
like image 41
Simon MᶜKenzie Avatar answered Sep 09 '25 18:09

Simon MᶜKenzie