PDA

View Full Version : Insertion point positioning in Word question



Retired
08-25-2015, 04:04 PM
I am updating an existing Word document that is in alphabetic sequence. It is basically an inventory of similar items that have single line entries. Beginning at the left margin I enter the information which is variable in length and depending on what the item is that I just typed I hit one of three function keys to invoke a basic macro that adds some additional information just to the right of what I had typed. Following the completion of that macro function I want to position the insertion point 11 positions from the right margin and I want to manually type the date this item was acquired in the mm/dd/yy format. I am a rank amateur when it comes to macro coding with VBA for Word. I know how to position the insertion point for the normal left, right, up and down functions. But, I can't figure out how to position the insertion point at the right margin. If positioning the insertion point at the right margin isn't possible, is it possible to obtain the numerical position of the current insertion point and subtract that numeric position from the numeric position of the right margin, assuming that numeric value is available, thus determining how many positions to the right that I must move the insertion point to begin typing the date in mm/dd/yy format 11 positions from the right margin?

Ideally if I can position the insertion point 11 positions to the left of the right margin I would just incorporate that coding into the existing three macros that I have already created.

Many thanks for any assistance that anyone can offer.

gmayor
08-26-2015, 01:14 AM
Set a right aligned tab at the right margin and add a tab after the text e.g.
Option Explicit

Sub AddADate()
Dim oDoc As Document
Dim oRng As Range
Dim lngRight As Long
'identify the document
Set oDoc = ActiveDocument
'Set a range to the paragraph the cursor is in
Set oRng = Selection.Paragraphs(1).Range
'Collapse the range to the end of the text in the paragraph
oRng.Collapse 0
'locate the right margin position
lngRight = oDoc.PageSetup.PageWidth _
- oDoc.PageSetup.LeftMargin _
- oDoc.PageSetup.RightMargin
'clear tabs from the paragraph
oRng.Paragraphs(1).Range.ParagraphFormat.TabStops.ClearAll
'add a right aligned tab at the right margin
oRng.Paragraphs(1).Range.ParagraphFormat.TabStops.Add _
Position:=lngRight, _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
'type a tab and the date
oRng.Text = Chr(9) & Format(Date, "mm/dd/yyyy")
'collapse the range to the end
oRng.Collapse 0
'select the end position
oRng.Select
lbl_Exit:
Set oRng = Nothing
Set oDoc = Nothing
Exit Sub
End Sub

Retired
08-26-2015, 01:29 PM
gmayor:

Many thanks for the excellent coding solution. I'll have to review it in detail because there is a good bit of it that I have questions about. A couple of the preliminary questions are as follows:

1) There are tab settings established already for the document and they are every two spaces. Because the data that I am entering is variable in length I needed a way to keep from having to continually depress the space bar to move to the right where I want to start typing in the date in a mm/dd/yy format. Using the tab key instead of the space bar will allow me to align the dates on the different lines one under the other. Aesthetically pleasing but definitely not an elegant solution, brute force rather than finesse. Because there are hundreds of items in this document each of which is only one line in length they all have used the tab key to space over to the right. If I clear all the tabs it seems to me that it will destroy the alignment of the date field in these hundreds of lines which is something I clearly don't want to do.

2) Is it possible to clear the tabs for just the current line that I am on? If it is couldn't I clear the tabs, set a tab at the right margin, which I assume that I could use at least part of your coding to do that and then do the following to position the insertion point 11 positions to the left where I could begin typing in the date:

Selection.EndKey Unit:=wdLine, Extend:=wdMove
Selection.MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdMove

Also can you recommend an excellent VBA for Word manual? I've got a copy of Mastering VBA for Office 2010 by Richard Mansfield but it's 900+ pages and at this point I am only interested in accomplishing this one task.

Thanks,
Retired

gmayor
08-26-2015, 09:37 PM
Did you test the macro? It clears the tabs in the current paragraph (from the Developer Tab > Document Template > Make sure that 'Automatically update styles' is unchecked) adds a tab at the right aligned tab at the right margin and inserts the date there. The date is always aligned at the right margin. There is no need to faff around with character counting.

Add the macro to a button on the QAT (Quick Access Toolbar) or ribbon (and/or create a keyboard shortcut) - http://www.gmayor.com/installing_macro.htm and then insert the cursor in the current line of text and run the macro.

If you are working with an existing document, you should add another line (oRng.End = oRng.End - 1) to the macro, where indicated below, or the date will go on the wrong line:


Set oRng = Selection.Paragraphs(1).Range
'Omit the end of paragraph character from the range
oRng.End = oRng.End - 1
'Collapse the range to the end of the text in the paragraph
oRng.Collapse 0

If you want the date left aligned at a point near the right margin, then you could change the tab e.g.
oRng.Paragraphs(1).Range.ParagraphFormat.TabStops.Add _
Position:=lngRight - 70, _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces70 is the distance in points from the right margin i.e. a smidgen less than an inch, but as the date format is mm/dd/yyyy. You can adjust that number up or down to accommodate your font size.

I cannot recommend a book on VBA as I have never read one. You learn best by experiment and for moving around a document, learn about ranges. For a basic primer see http://gregmaxey.mvps.org/word_tip_pages/vba_basics.html

Retired
08-27-2015, 06:01 AM
I did test it and it did put it on the wrong line. I also need to type the date in because I update this document and other similar ones on a fairly infrequent basis and there may be 20-50 entries to be made when I do the update, almost all of which have different dates when they were acquired. So, I need to position the insertion point where I can begin typing in the date in a mm/dd/yy format. I do understand that the font used will have an effect on the positioning.

I was a mainframe systems programmer long ago who used to write some operating system code among other things but I am having a challenging time finding information about the reserved names that are used for Word and what their specific functions are. For example, I would have thought that some simple type of move command associated with the insertion point such as RightMargin would just move the insertion point to the right margin but apparently that would be too logical. I do understand that I need to understand the protocol. I actually enjoy experimenting with code but at least at this point I doubt that I will do much else with VBA and Word other than getting this to work the way I want it to. Sometimes though once you get into it you find ways of automating things that you really hadn't thought about writing macros for.

Once again thanks for your invaluable assistance.

gmayor
08-27-2015, 09:19 PM
If you want to type the date then change the line

oRng.Text = Chr(9) & Format(Date, "mm/dd/yyyy") to
oRng.Text = Chr(9)
You can move the insertion point within text ranges, but moving the insertion point to a position related to the page is more challenging.

Word is not a page layout application. It is style driven and works by flowing text between preset margins, using the format supplied by the style(s). To place the insertion point at the right margin, the easiest method is to insert a right aligned tab at the right margin and tab the text to it. This is essentially what the macro does, by removing all the tabs, adding a single tab and inserting a tab after the range.

The second version of the tab addition uses the same method to locate the right margin, but then sets a left aligned tab 70 points in from that margin. This is more fiddly because the measurement is unrelated to the font and its point size. The smaller the text the less space it will need.

The paragraph range will include the text and the paragraph break, so the modification in my last post excluded the paragraph break to keep the insertion point in the same paragraph.

VBA is quite habit forming, as a few of my fellow contributors would testify :help