Click here to Skip to main content
13,057,084 members (43,702 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello All,
I am stuck at one point where i am opening Microsoft application from C# program and then it writes small paragraph to word from C# and then person has to edit a bit manually in paragraph and leaves cursor anywhere in that paragraph and then C# program again writes paragraph but this time C# doesn't know about the position of cursor because it would be somewhere in middle of paragraph or anywhere in from C# i need to know the position of cursor and move it to last character of that document. i know it's possible but i don't know how to..
If any of you know how to solve this. Please Reply here.

Thanks and Best Regards,
Posted 5-May-11 9:53am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Go to Word - record a macro that does what you want - maybe edit what gets recorded to tidy it up.

Install and reference the relevant version of the Office / Word primary interop assemblies if you haven't done so already.

Copy the recorded macro to your VB code (or edit it to do the same in C#) - maybe some further editing might be needed, but hopefully not much - the VBA macros that get recorded are normally (in my experience) very close to what's needed in VB if you use the PIAs because both translate into / wrap the same COM objects.
mohit`12 6-May-11 9:23am
I Exactly do not know how word does that job otherwise i would have recorded MACRO.there's sure some function or piece of code in c#/C++/VB that does this job easily just need to findout what is it.. thx
NuttingCDEF 6-May-11 12:48pm
Start the macro recorder - go to Word - hit Ctrl-End - Stop recorder - look at the code it's recorded.
mohit`12 9-May-11 10:57am
yeah i found out how recording macro works but word doesn't do that job with their own functionality so i had to write a small piece of code for that function. Hittign END key takes cursor to end of that paragraph in my case i will have 1000 page and user will be editing any page but when user receives new data it has to go at the last line: P anyways thx i got to learn something new frm you ..
NuttingCDEF 9-May-11 18:10pm
Which version of Word are you using? In every (Windows) version I've used, Ctrl-End takes you to the end of the document. End to end of line. Shift-Down to end of paragraph.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

object oMissing = System.Reflection.Missing.Value;
object oEoF = WdUnits.wdStory;
wordApp.ActiveWindow.Selection.EndKey(ref oEoF, ref oMissing);
mohit`12 9-May-11 10:55am
awesome works like charm thx alot..and another question if u know when i open word app from c# it hides under my c# application/form. any way to call that window on top or onfront...
thanks alot..
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

try hooking the keyboard avaliable in codeproject, search for windows hooks
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Selection.EndKey Unit:=wdStory
Selection.TypeText Text:=""
phil.o 30-Sep-15 2:45am
Not C#. Moreover, what is the point to answer an already answered 4-years old question?
goodyboy 23-Mar-16 1:43am
point is .... if somebody stuck in the same situation. so they can find answer here....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.6 | Last Updated 30 Sep 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100