Google Answers Logo
View Question
 
Q: Automation Microsoft Word in VB : finding my place in Word. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Automation Microsoft Word in VB : finding my place in Word.
Category: Computers > Software
Asked by: octo-ga
List Price: $5.00
Posted: 14 Aug 2002 14:31 PDT
Expires: 13 Sep 2002 14:31 PDT
Question ID: 54665
I use automation from Visual Basic to open a word document. If I go to
the end of the document and start typing I can pick up the words I
have just typed using MyLastWord =
MyD0cName.Application.ActiveDocument.Words.Item(MyD0cName.Application.ActiveDocument.Words.Count
- 1) .  The index of the item in this case is easy: just the total
word count for the entire document.  What I need to know however is
how to pick up the word I just typed in the document if I move the
cursor and begin typing at any random location in the document.. 
Briefly: I need the index of the word just typed from anywhere in the
Word document.
Answer  
Subject: Re: Automation Microsoft Word in VB : finding my place in Word.
Answered By: maniac-ga on 14 Aug 2002 19:22 PDT
Rated:5 out of 5 stars
 
Hello Octo,

I looked up information from the object browser in Visual Basic for MS
Word and have a few ideas on how you can do what you want. You may
want to follow along...

I started with a blank document and typed...
  Now is the time for all good people to come to the aid of their
party.
I selected the words "good people to come" with the mouse, then did
  Tools -> Macros -> Visual Basic
in Visual Basic I did 
  View -> Immediate Window
and then typed into that window
  print selection.words.first
which displays the value
  good
which shows the first word in the selection. You get the same result
if the cursor is positioned before the word good as well. A few other
commands and results included...
  print selection.words.count
    4
  print selection.words
  (error)
then position the cursor before the word good (no text selected)...
  print selection.words.count
   1 
  print selection.words.first
  good 
  print selection.words.characters
  (error)
  print selection.range
  good
  print selection.range.start
   24 
So, you can find the character position of the current selection using
this method. I wrote a quick subroutine for testing...
Sub X()
For i = 1 To ActiveDocument.Words.Count
  If Selection.Range.Start < ActiveDocument.Words(i).Start Then
    j = i
  End If
Next i
End Sub
set a breakpoint on j = i and run the macro X. You will find that the
function stops when you find the first word after the start of the
current selection (which may not start at the start of the word...).

This is a pretty ugly solution, but I can't seem to find any better
way to find out "which" word in ActiveDocument.Words that
Selection.Words.Start happens to be. If you want me to dig a little
more, please ask for clarification.

If you want to search some more on your own, with the object browser,
type in the phrase into the second field at the top to get a list of
objects and fields that have that phrase in their name. Use the help
librerally (including examples) to find the items that may be able to
help.

--Maniac
octo-ga rated this answer:5 out of 5 stars
Easy to follow and solved my problem.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy