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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VB in Word 2

Status
Not open for further replies.

Cubby1

Programmer
May 24, 2004
5
US
I need to determine the current location of a text insertion in a Word document. I know that I can go to a particular location using Range(x,y), but how do I learn the values of x and y when at a particular location?
 
You wanted this ?
x = Selection.Start: y = Selection.End

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Describe this better.

ANY location is identfied in Word by Long Integers. It is not really (x,y) in that (x,y) normally implies a coordinate system x-axis, y-axis.

It is x, and y, but x = Start, and y = End. It describes a Range. Starting at x and ending at y. It always starts at 0, which is the beginning of the document.

Bookmarks are persistent location markers held in a document. They have a Range. In the simplest description, say:

The quick brown fox jumps over the lazy dog[/color red]. The quick brown fox jumps over the lazy dog.

The text in red is a bookmark named...Red.
Code:
Sub GetBMRangeNumbers()
MsgBox ActiveDocument.Bookmarks("Red").Range.Start _
    & "  " & ActiveDocument.Bookmarks("Red").Range.End
End Sub[/vba]
will display "20  43".  That is, the Start of the bookmark range is 20 characters from the start of the document.  The End of the bookmark range is 43 characters.

So, EXACTLY what are you trying to do.  Using bookmarks is the the best way to go to a specific location.  What you are asking is "how do I learn the values of x and y when at a particular location?"

If the Selection is only a point - that is, nothing is selected, you just have a cursor, then x and y = the same.

You can have code to determine that:
[code]Msgbox Selection.Range.Start & " " & Selection.Range.End
This will display the Start and End....but what good is this? So you know where the cursor is....generally speaking...big deal. If you describe what you want to happen I am sure we can help.

Gerry
 
Crap, did not close that tag very well did I? Hmmmmmm.

Gerry
 
Thanks for the information. I am familiar with VB6, but not VBA.

I am taking an existing Word Document and using it as a form to insert information from a database. I am creating the application using VB6, but open my Word Document in the VB6 program and then have the Word Application features available in VB6.

I am using bookmarks to locate the insertion points, and have completed the project except that if the text that is inserted is not the exact length of the existing bookmarked text, some text on the newly created document can be shifted right or left and some of the line lengths are wrong. The underlining feature of Word was used to draw lines that should all be the same length for appearance.

I was thinking that I would test to see if each line ended where it is supposed to and if not add or remove Tabs to correct it, but would like to hear your ideas.

Cubby
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top