Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am using word COM object to get some data from an existing word document. All i need to know how to get the text at the cursor position from oWord.ActiveDocument. Almost all google links are ponting to insert text in word document. But i need to know how to get a word, sentence, a line of text from word. And that is in the current cursor position. I know a scripting language named AutoIt. In AutoIt, i can easily tell my program to catch word or senetence or char from either backward of the cursor or forward of the cursor. But i don't know how to do it in VB.NET. Please help.

And this is i am up to.
VB

VB
Dim oWord As Word.Application
       Dim Sel As Word.Section

       oWord = GetObject(, "Word.Application")
       Sel = oWord.Selection
       Dim oRange As Word.Range = Sel.Range()
Posted
Comments
Ralf Meier 30-Jul-15 3:08am    
I'm not sure, what you want to achieve.
Do you want to now the position of your Search-Text ... or di you want to do something with this Search-Text - for example replace it through another text ?
Vinod Kc 30-Jul-15 3:52am    
@Ralf Meier , I am planning to make a text expander for word. I know there is autocorrect. But autocorrect will change mt formatted text's font aftre a restart. So i decided to make one. If user types "prod", then my program catch that string and compare it with an array. If array contains a full text of "prod" then it will select the word "prod" and replace it with new text. I hope i have made the point clear.
Ralf Meier 30-Jul-15 8:09am    
I added a Solution ...

next time please use Reply for an answer. In this case I automaticly get a notification ...
Vinod Kc 30-Jul-15 14:48pm    
Sure. Thank you

1 solution

You could use the Word-intern functionality for that :
VB
' connection to MS-Word
 objWordApplication = CreateObject("Word.Application")
 objWordApplication.Visible = False
 Dim objDoc As Word.Document
 objDoc = objWordApplication.Documents.Add(myFileName)
 objDoc.Protect(Word.WdProtectionType.wdNoProtection)
 objDoc.Activate()

 ' replace Text-elements
 With objWordApplication.Selection.Find
     .Text = "Date"
     .Replacement.Text = Now.ToString("dd.MM.yyyy")
     .Execute(Replace:=Word.WdReplace.wdReplaceAll)

      .Text = "Name"
     .Replacement.Text = myName
     .Execute(Replace:=Word.WdReplace.wdReplaceAll)

     .Text = "VorName"
     .Replacement.Text = myForeName
     .Execute(Replace:=Word.WdReplace.wdReplaceAll)

    End With


In this sample each appearence of the keyword will be replaced by a string from my application ...

I hope it helps you ...
 
Share this answer
 
v2
Comments
Vinod Kc 30-Jul-15 14:51pm    
@Ralf Meier,
Thank you for this solution. Well, i have to put this "Selection.Find" inside a loop. Anyway, let me try. I will sure inform you.
Ralf Meier 30-Jul-15 14:53pm    
You are welcome.

I think, you don't need to put it in a loop - but I'm also not sure if I completely know what you try to achieve.
Anyway - if there are further questions feel free to contact me ... ;)
Vinod Kc 30-Jul-15 15:00pm    
Without a loop, how do i know what the user types now. And if i don't need a loop then, do i need to add this code inside a timer's tick event ?
Ralf Meier 30-Jul-15 15:12pm    
Tell me by an example what you (exactly) want to do (and when you want it to do).
I'm not quiet sure if we speak/think about the same thing ...
Vinod Kc 30-Jul-15 15:20pm    
Ok, if user types a word in word document, my program continously monitoring what he types and search that word in a database. If text on the left side of the cursor matches to the text in database column1, then my program select the range and replace the text with a text from database column2. For getting speed replaces, i am planning to open the database at starting time and load the columns into an array.
I forgot to add an example. here it is;
assume my array (or dictionary) contains these two text -
array(0,0) = "Perso"
array(0,1) = "Personal Experience"
When user types "P", program checks for "P" in array. And when program finds that array(0,0) and text from word document is same, then it will paste the array(0,1) to word.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900