Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autocomplete function in Word 1

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hi friends,

I am looking for a way to create some sort of auto-complete function for Word.
I know, that theoretically the best way would probably be to create a custom dictionary and use this as default for spell-checking, or to use a list of auto-text entries.
This however is not applicable in this special case.

Background: The function will be used during creation of technical documentation, and shall ensure that the text is validated against a glossary.
If I ran the check on the completed document, I could not ensure that all terms would be correctly checked because I cannot foresee all sorts of typos.

Is there an event that will fire upon typing in Word that I could use, similar to Excel's "Worksheet_Change" event?

I know it will be another task to keep the performance of Word at an acceptable level, but as I said: that'll be another task. :p

I'd be glad for any ideas you could give me!

Thanks & cheers,
Andy

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
If you are talking about a real-time event, the simple answer is no, there is no Document_Change event. Other than AutoCorrect.
If I ran the check on the completed document, I could not ensure that all terms would be correctly checked because I cannot foresee all sorts of typos.
That is why we have technical writers/editors. We can't foresee all typos.

Gerry
My paintings and sculpture
 
Thanks Gerry!

This is not very encouraging. [sadeyes]
But there simply MUST be a way.

If nothing else works, I might have to think about using a VB app running in the background, using a keylogger or the like...

Thanks any way.
Still open for any ideas, no matter how freaky!
:)

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
But there simply MUST be a way.
Well...there is...and you stated it. A VB app running ALL the time as a keylogger.

Think of the problem. If it is real-time, then if you type "Tek-Tips is", it has to run like:

"T".....run the code.
"e".....run the code.
"K".....run the code.
"-".....run the code.
"T".....run the code.
"i".....run the code.
"p".....run the code.
"s".....run the code.
" ".....run the code.
"i".....run the code.
"s".....run the code.


There simply is not an event to fire for every keystroke, which is what you are talking about. It has to be every keystroke, because how is it supposed to know when to start and stop?

Also, the Worksheet_Change event only fires AFTER focus moves from the cell. If you type "aaaaaaa" into a cell, Worksheet_Change does not fire after "a", or after "aa", or after "aaa" etc. It fires when focus is changed from the cell. It then does whatever you have as code, when focus is changed from THAT cell being changed. In other words...it is not real-time either.

Now take that back to Word. There are no cells to have focus, or no focus. WHAT would cause something to fire????

Type: "Tek-Tips is"

After the "T"? After the "e"? There is NO real-time event for typing in Word...which actually makes sense. Again...WHEN would it fire? In Word, it is the whole document that has "focus".

AutoCorrect is an internal process that - other than being able to add and remove entries (and of course actually USE it) - is just not exposed to VBA.

I would be happy to hear about something that could do what you are asking about. Other than AutoCorrect. Having a background keylogger constantly checking every keystroke seems drastic and a bit of a hog on resources.
If I ran the check on the completed document, I could not ensure that all terms would be correctly checked because I cannot foresee all sorts of typos.
Probably true...but I fail to see how any real-time keylogger will do it either.

Gerry
My paintings and sculpture
 
I see the problem.
Thanks Gerry.

There might be a different solution to this, then:
Using Word was only my first choice for simplicity's sake, but I might not have to use it.
The text will be used in Quark XPress anyway. As the chances for doing such a check in Quark are NIL, I thought of Word.

How about using a VB stand alone exe? Is there a control which would allow me such checks?
I would then program something in vb which would create a text (or perhaps even rtf?) output.

You see, I'm not willing to give up. :p


[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Sorry, but I fail to see the urgency, or the problem, that seems to be implied. If I understand it correctly, the issue is you have technical documentation that you want to validate EVERY SINGLE THING THAT IS TYPED, in real-time.

Again, sorry, but that seems, IMHO, utterly silly.

Validate it against your glossary when you are ready to do so. If you think you are really going to be able to have a technical document written with full and effective spell checking and term validation in real-time, and trust that you have missed NOTHING....with all due respect, you are dreaming. I have been writing technical documentation for more than 25 years. Unless it is only a couple of pages, I always print it and edit it - actually READ it - for any final proofing.

I doubt if I would ever trust any coded solution enough to believe it will catch every single possible typo or mis-use of terms. And that is with something validating at the end of the writing. YOU are talking about such a solution doing it real-time, as you type.

Good luck. If you come up with something that does 100% accurate proofing, let us know. Although maybe after you patent it and take it to market. There certainly would be a market for it.

Gerry
My paintings and sculpture
 
Although if you want something that WILL do what you seem to asking....

Have a userform with a single Textbox.
Have the TextBox1_Change event check to see if the last character is a space.
If it is a space - which I assume means it is a "word" of some kind - validate that word against something.

If it validates, then type it into the document.
Clear the textbox control so you can type in the next word.

Something like:
Code:
Private Sub TextBox1_Change()
If Right(TextBox1.Text, 1) = " " Then
   Call ValidateWord(TextBox1.Text)
End If
End Sub

Sub ValidateWord(strIn As String)
   ' check the text/word against
   ' a text file?
   ' a databse source?
   ' If validated...Then
   Selection.TypeText Text:=strIn
   TextBox1.Text = ""
   TextBox1.SetFocus
End Sub
The _Change event will, in fact, fire for every single character typed.

I just did a test of validating every word typed into the textbox against a list of words (about 100) in a text file.

Hmmmmm, this is not a very efficient way of writing......

But it does answer your question - "Is there a control which would allow me such checks?" Yup...there is, sort of. I think I would be willing to bet any user that was forced to write with this kind checking would say....hmmmm, I don't think so.

Gerry
My paintings and sculpture
 
Don't worry, Gerry:

I told you I wouldn't give up and that there MUST be a way and lo: there IS a way!
[afro]

And it is quite simple and effective, and pretty much what I was looking for!!! [bigcheeks]


==>If you can't beat 'em, join 'em! - use Word's built in Auto text feature, and just add your list of words with a little macro like this one, using words stored in an access table:

Code:
Sub AddMyListOfWords()
Dim newEntry As String
Dim ConStr As String, Con As New ADODB.Connection
Dim rs, i As Integer, j As Integer

On Error Resume Next

datapath = "...\whatever_glossary.mdb"
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & datapath & ";Persist Security Info=False"
Con.Open ConStr
Set rs = Con.Execute("SELECT MyData From Glossary")

Do While Not rs.EOF
    j = UBound(Split(rs("MyData"), ",")) 'just in case
        For i = 0 To j
            newEntry = LTrim(Split(rs("MyData"), ",")(i))
            With NormalTemplate.AutoTextEntries
                .Add newEntry, Selection.Range
                .Item(newEntry).Value = newEntry
            End With
        Next i
    rs.MoveNext
Loop
Con.Close
set Con = Nothing

End Sub

Wasn't that complicated after all...

But Thank you any way, Gerry, you have saved me from trying to programm some memory killer after all!

[thumbsup]

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Sheeeesh!!! If I may remind you of what YOU posted in your first post??? Hmmmm????
I know, that theoretically the best way would probably be to create a custom dictionary and use this as default for spell-checking, or to use a list of auto-text entries.
This however is not applicable in this special case.

So...ummm...are you saying it IS applicable in this case?

However, I notice you have the grace to be slightly embarrassed. Good for you.

May I still point out that this is still not a real-time typing check?

Gerry
My paintings and sculpture
 
Well, it is close enough to real time, and with auto text I can create a template with autotext entries for each department based on their respective glossary.

I thought it would not be applicable, because
a) there are currently ~ 2000 entries, which I had no intention of adding (manually) to the autotext list. Didn't know t was accessible that easily via code.

b) the list will grow with each project, hence a lot of new entries have to be added afterwards, and there are different projects whose glossaries must not be mixed

c) I don't want to "spoil" the authors' normal.dot and with this their custom macros etc...

==>
However, by adding the entries to a different dot than normal.dot, and using this as the attached global template, that problem, too, is solved.

So, I'll end up with a few DOTs, each containing the latest glossary in form of auto text entries.

Forgive me, Gerry: I am not a technical writer, neither do I usually work with them, which is why I have absolutely no experience with this sort of Word manipulation.
[blush]

Murphy's Law no. xyz: Never assume things. You'll always guess wrong...

P.S: There is also another freeware tool that comes close to a real-time typing check: it's called "Typing Assistant"

However, I deffo prefer the autotext solution, because there I can split the entries project wise..

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Oh, sorry. I was not intending to be critical.

And yes, keeping autotext entries in differnt templates for different projects is a VERY good idea. If I may suggest something. Global templates may also be added by code. Maybe make up a menu to add/remove globals so they match the currently used project template. Or, when using template X for a document, have it load template X_autotext automatically.

I am glad you found the proper and working solution.

Gerry
My paintings and sculpture
 
Thanks Gerry,

already done that!
:)
Code:
Sub activate_glossary()
    myPath = ThisDocument.FullName
    With ActiveDocument
        .UpdateStylesOnOpen = False
        .AttachedTemplate = myPath
    End With
End Sub

I've also added the AutoText to this respective template already.
Now, I'll create a little sub that will allow switching the glossary on/off, with the current status indicated in the tool button.

Once I've created the templates for the other glossaries, I will make a toolbar or a listbox/option buttons/whatever in which the user can activate/deactivate the suitable glossary.
:)

Now is the time to fine-tune things.

P.S: Of course you were critical - and that's good!
I know who it is coming from and what it is worth. ;-)

P.P.S: Who knows what it might be worth to others reading this thread...

Thanks again & cheers,
Andy

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top