?
Solved

Autocomplete feature in MS Word using VSTO 2010

Posted on 2011-04-27
12
Medium Priority
?
1,564 Views
Last Modified: 2013-11-26
Hello All,
I am creating a Microsoft Word application Addin using VSTO 2010. I need to add autocomplete feature in it in such a way that user will first create its common sentences (which he mostly use) repository and then when he start typing in document, application will suggest him the matching sentences.

In this requirements I have following two challenges.
1. How to capture the user typing event in Microsoft Word as VSTO does not have any such event in it. The solution should be secure and fast.
2. How can I show the small auto-complete window below the cursor (where user is typing) in MS Word document.

Thanks in advance.
0
Comment
Question by:malikirfan28
  • 7
  • 4
11 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35474871
Sounds a bit like re-inventing the wheel. Why not create Autocorrect entries and let Word do it?
0
 
LVL 4

Author Comment

by:malikirfan28
ID: 35474922
Can you explain how can we add our custom sentences in Autocorrect? What I know Autocorrect will suggest you if you have type something wrong but it does not allow to complete feature.
Let me explain with example. Give below is the sentence which user used mostly in his document so he want to add it in auto-complete repository

When I wend to the doctor.

Now when user will start typing like "Whe" our application will suggest the above sentence.

Let me know if Autocorrect give such functionality.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 800 total points
ID: 35475538
Manually, in Word 2007, it is accessed via Optons, Proofing. You can add entry of Whe in the Replace box and the phrase in the With box.

Then when typing, Whe followed by something that terminates the word (space, enter, full stop), will appear as the full phrase.

New entries can be added programatically:

In VBA and legacy VB, the syntax is:

        Set ac = AutoCorrect.Entries.Add("Whe", "When I wend to the doctor")

If it needs to carry specific formatting, is is:
 
        Set ac = AutoCorrect.Entries.AddRichText("Whe", rng)

where rng is a document range that contains the formatted text.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 4

Author Comment

by:malikirfan28
ID: 35476174
Again this is Auto Correction, not a suggestion. Even I dont want to use this sentence but Word will force me like I typed something wrong. For example if I want to type "Whee" and mistakenly press space after "Whe" then Word will replace Whe with my sentence.

Kindly note that I want to give suggestion when user start typing that do you want to use this sentence. Can't force user to use it in any way. And definitely option will be narrowed as user keep typing. For example, user added following three sentences in repository

When I went to the doctor
When you will come to me
What is time now

So when user type "W" all three options come. If he start typing and reach "When" then only above two option come. etc

I hope I clear it now.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35476997
If you needed that precise functionality, I think that you would have to work outside of the document object model -- with Windows APIs  to monitor keystrokes. As you have discovered, there isn't a built-in event that fires for text changes.
0
 
LVL 4

Author Comment

by:malikirfan28
ID: 35477200
I don't have any experience with Windows API and don't know how much secure it is?
Could you point out any valid example which work in my scenario?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35479228
I thought that there were several keystroke capturing routines in VB, but they all seem to be a timer-controlled loop that uses a API function to test the state of every key on the keyboard, which sounds very resource-hungry to me

http://www.freevbcode.com/ShowCode.Asp?ID=1610

It could be adapted to find if the active window were the active Word document window, the selection point could be found and the previous three characters examined.



Subclassing might be a more elegant way. One of the items here seems to suggest that any window could be subclassed via its handle and hence its message events listened to.

http://support.microsoft.com/kb/311317


This article shows (in C language)  how to get the meaning of the messages.

http://msdn.microsoft.com/en-us/library/ms646268%28v=vs.85%29.aspx
0
 
LVL 4

Assisted Solution

by:malikirfan28
malikirfan28 earned 0 total points
ID: 35503131
Thank you Graham,

I have found a very good article for Global Keyboard and mouse hook which I used in MS Word and capturing the user typing excellently.

http://www.codeproject.com/KB/cs/globalhook.aspx

Now second part left of the question that how can I show a small window form below the cursor (where user is typing) position which will have my possible sentences so that user can select one and can use it in document.
0
 
LVL 4

Author Comment

by:malikirfan28
ID: 35713143
As I could not get any solution so should I assume that it is not possible in Microsoft Word?

Any hint will be appreciated.
0
 
LVL 4

Accepted Solution

by:
malikirfan28 earned 0 total points
ID: 35761430
At last I created a Custom Task Pane window in Word document and displaying the possible suggestion in this task pane.

It works.
0
 
LVL 4

Author Closing Comment

by:malikirfan28
ID: 35791865
Although I could not achieve my basic requirements as I believe it is not possible using VSTO. But I found an alternative of Custom Task Pane as I commented above.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question