Inserting text to a document according to the selected text by looking up to a database

A

Amir

Hi!

I'm a newbie with Word scripting, but I know Access quite fine, and also a
bit of VBA.

I want to do the following and don't know where to start:

I want to make Word to do the following actions when I press 'F7' after I've
selected a text in the document (or any other F Key):
1. If there is no selected text, quit the script/macro/code/whatever.
2. Look at the CatDetails table in c:\database.mdb.
the CatDetails table contains two fields:
Field Name Data Type
CatID Text
CatText Text

I want the code to search CatDetails table for CatID which is equal to the
current selected text in the Word editor.
3. If there is no record which has CatID = SelectedText, then I want word
to beep and quit the code.
4. If there is a record which has CatID = SelectedText, I want the code to
automatically add the following text after the selected text:
" - Text about the Cat: " & CatText '(according to the selected CarID)
5. I want the (keyboard) cursur to move to the end of the text that was
added.

For example, if i'm editing the current line in word:
"Eddie2 is a nice ID for a cat."
and i'm selecting "Eddie2", and pressing for example F7, I want the code to
look at the CarDetails table, find that the CatText for "Eddie2" is "He is a
nice one", and change the line to:
"Eddie2 - Text about the Cat: He is a nice one is a nice ID for a cat"*
(* marks the place where I want the keyboard cursur to be when the code
ends.)

Thank you very much for your help.
I'm sorry if it's not the forum for that question, and I will be grateful if
you answer this question or tell me which forum to address it.

Regrads,
Amir.
 
A

Amir

Hi!, Thank you very much!

I have two more questions about this solution:
1. How can I make Word not to select the "space" char that comes after the
selected word, or how can I make the search in the database to search
without that spacebar? Almost every time I select a word it's selected with
the spacebar after it, like that: Eddie2_ instead of Eddie (I used the _ to
represent spacebar). How can I cut the spacebar from the selection or from
the search in the DB?
2. Can I make word to automatically select the current word that the
keyboard cursur is on, so that I won't have to select it everytime?

Thank you very much again!

Kind Regards,
Amir.
 
A

Amir

I've solved my first questions using the next code to replace the text and
remove the last character if it's Chr(13),
But I still don't know how to select the current word if Len(pText) = 0.


Dim strSelectionWithoutLastSpace As String
Dim strChar As String
Dim i As Integer

If Len(pText) = 0 Then
Exit Sub
End If

For i = 1 To Len(pText)
strChar = Mid$(pText, i, 1)
If i < Len(pText) Then
strSelectionWithoutLastSpace = strSelectionWithoutLastSpace &
strChar
Else
If (strChar = Chr(13)) Then
'Do Nothing
Else
strSelectionWithoutLastSpace = strSelectionWithoutLastSpace
& strChar
End If
End If
Next i


Regards,
Amir.
 
A

Amir

Thank you very much!

Regards,
Amir.

Jezebel said:
Much simpler way to get rid of the trailing space is the Trim$() function --

pText = trim$(Selection)

To get the word containing the selection --

pText = selection.Words(1)

Bear in mind that "Word" in this context is a fairly loose term. A
punctuation mark is considered a "word" in this collection.


equal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top