PDA

View Full Version : Solved: Work out the number of spaces until end of line



FhM
11-27-2009, 07:05 AM
I need to insert a string onto a line but I want it to be at the end of the line, so I want it to be able to find the number of spaces after the bookmark I am using the end of the line and pad the string out. The line currently contains text.

At the moment I have it simply putting the string after the bookmark

ActiveDocument.Bookmarks(objSelectedBookmark).Range.InsertAfter strSig

So I need something like.

go to position after bookmark and select range from there to eol

Calculate number of spaces.

FinalString = strSig

for i=0 to numspaces
FinalString = chr(32) + strSig
Loop

ActiveDocument.Bookmarks(objSelectedBookmark).Range.InsertAfter FinalString

Can someone fill in the blanks for me please.

Thanks in advance

Dave
11-27-2009, 07:35 AM
This seems somewhat similiar. Perhaps it will give you a start. Dave
http://www.vbaexpress.com/forum/showthread.php?t=11542

FhM
11-27-2009, 07:55 AM
From what I can determine it seems the size of the rest of your document is always the same? If so mine are variable so I am not sure I can use this. Is this the case?

fumei
11-27-2009, 02:51 PM
1. declare a range object
2. set the range object to the bookmark range
3. expand the range object to the line (wdLine)
4. collapse the range object to its End
5. insert the string

macropod
11-28-2009, 03:29 AM
Hi FhM,

Unless you're using a fixed-width font, you can't equate a number of spaces with any given number of other characters.

FhM
11-28-2009, 05:07 AM
Is there a way of padding with spaces until the next chr is a newline or eol?

macropod
11-28-2009, 05:31 AM
Hi FhM,

Word has no 'eol', as such. What you need to understand is that Word uses the current print driver and its settings to work out how to lay out a document. Change the print driver or its settings, change the printer, the OS or OS version (any of which probably require different drivers) and the layout is prone to changing. In a paragraph, where a given line ends depends on these factors, plus whether and what kind of justification has been applied. About the only viable way of doing what you ask is to keep adding spaces until a line-wrap occurs, but that can only give a 'correct' result with the current print driver at its current settings.

Perhaps you could explain in more detail what it is you're trying to achieve?

FhM
11-28-2009, 05:47 AM
All I want is to add a signature line to the last line of a bookmarked section but the document has "Employee name:" on the left and after a form is filled out I want the input text to be right aligned so "Employee name:" and "Joe Bloggs" are at opposite ends of the line. This is just the format they want for the document.

FhM
11-28-2009, 12:54 PM
Ok what I might do to make life easier is make a 2 column 1 row table with no lines and right align the right cell. These things are printed out so it shouldnt ruin the presentation unless someone comes up with something else.

Dave
11-28-2009, 07:08 PM
Just to be clear ""Employee name:" and "Joe Bloggs" are at opposite ends of the line".... this is all that is on 1 line. ie. Employee name: (left justify) then a bunch of spaces and then Joe Bloggs(right justify). I assume that Joe Bloggs is filling in the bookmark? So you really want to control the length of the string of blanks in between based on a variable length name (bookmark) like Joe Bloggs. Doesn't seem impossible. Find out the line that you are on. Then add the text. Check to see if your still on the same line or on the next line. If your still on the same line, remove the original text add another blank to the string and re-check by adding the new text and again finding out the line thing (ie. loop until the middle blank string forces the name out to be right justified.). If you add the text and you find that it takes two lines then remove the text, remove a blank and re-check by adding the new text and again finding out the line thing (ie. loop until the middle blank string contracts so that the name is right justified.). The advantage of this trial and error approach is that I believe it addresses the issues put forward by macropod. The table thing will also work. Dave

macropod
11-28-2009, 11:11 PM
Hi FhM,

So all you really need is a single tab after 'Employee name:' and a right-aligned tab stop for the paragraph so that 'Joe Bloggs' ends up at the right-hand end of the line ... You really don't need any mucking around with spaces for padding.

FhM
11-29-2009, 06:11 AM
Ok Tab stops I didn't know about them. Just playing with them now. So if I set a tab stop in the document then paste the sig after the bookmark with a tab proceeding it, it will go over to the right. That looks perfect and as simple as I had hoped. I will have to do it in VBA as the macro also deletes part of the document as well as you select a statement so things will end up on different line. I have found this sample code so this should be straight forward.

Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(6.63), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

Thanks Macropod, once again you have come through for me, it's greatly appreciated.

macropod
11-29-2009, 03:22 PM
Hi FhM,

Done correctly, you almost never need to select anything ...

FhM
11-30-2009, 07:13 AM
I have had a go but I can't come up with the solution. I can insert the text string but I am not sure about the tabstop without using a selection. Would you care to share what your thoughts were so I can learn good practice early on ;-) . The bookmark is identified earlier in the code so I am currently using this

ActiveDocument.Bookmarks(StrSelectedBookmark).Range.InsertAfter vbTab & strSig
So how would I expand this to add the tabstop ?

fumei
11-30-2009, 11:47 AM
1. why are youi adding a tab stop via code? Should this not be there already?

2. a reiteration of macropod's comment:

"Done correctly, you almost never need to select anything ..."

You do not need to select anything.

3. demo attached. Click "End Of Line" on the top toolbar. This demonstrates two things.

a) you can add text at the end of paragraph, no matter the length - i.e. no need to count spaces/characters. There are two bookmarks with different amounts of text after them. The added text (" Added text") is inserted at the end, regardless of the amount of text following the bookmark.

b) "Joe Bloggs" is inserted into a bookmark (at a right-aligned tab) after "Employee name:"

No padding with spaces.

I made the inserted text in all cases use a Character style (larger font and Bold) to show clearly the inserted text.

FhM
12-01-2009, 01:28 AM
Thanks for this but the code is protected so I cant look at the code. Amazon should be delivering a nice new book for me today so hopefully I wont ask so many annoying questions in the future.
My main concern was the first part of my code will delete 4 out of 5 bookmarks, once the user makes a selection. I was just concerned, as I was adding the tab stop manually it seemed to be specific to the current line but it seems it keeps it properties even if it has moved up or down. I look forward to having a look at the code attached as it will clear up many different things for me that I am struggling with. Thank you for your patience.

fumei
12-01-2009, 12:39 PM
1. but does it seem to do what you want to happen? Who knows, as you do not say.

2. "the first part of my code will delete 4 out of 5 bookmarks, once the user makes a selection. "

Who knows why....as you have not posted any code that indicates anything like that.

3. " once the user makes a selection. "

WHAT selection? What is being selected? Who knows? I will repeat something:

2. a reiteration of macropod's comment:

"Done correctly, you almost never need to select anything ..."


Here is what you wrote as to what you want:

All I want is to add a signature line to the last line of a bookmarked section but the document has "Employee name:" on the left and after a form is filled out I want the input text to be right aligned so "Employee name:" and "Joe Bloggs" are at opposite ends of the line.
Does the demo do this, or not? If it does, then set up your template exactly that way.

Emploree name: [Tab - right-aligned] [insertion bookmark]

No need to select anything. No need for manually making a tab stop.

FhM
12-01-2009, 01:17 PM
Ok my fault sorry for being annoying. I was trying to see a more complicated solution to an easy problem. I have received my O'Reilly book so I am going to have a good read through this.

The code I mentioned is in this post http://www.vbaexpress.com/forum/showpost.php?p=200472&postcount=10

for which you have kindly helped me with. Unfortunately I still get some weird error even though the correct code and form is now part of the document. But it works when the orignal template is present anyway.....

Thanks for the help and patience.