MS Office Forum / Word / Programming / March 2005
Any helper re setting tab stops (thank you!)
|
|
Thread rating:  |
singeredel - 01 Mar 2005 02:01 GMT Please see my last two posts under "Set tabs based on distance from right margin." I am trying to set two tabs a specific distance from the right margin based on the length of string variables, but I get a run-time error on the code at the end when setting the tabs that the measurement must be between -1584 pt and 1584 pt. Do I need to convert my tab calculations somehow?
Thank you!
 Signature singeredel
Helmut Weber - 01 Mar 2005 12:51 GMT Hi Julie,
you'd better stay in the same thread, unless it isn't weeks old.
First, a string variable hasn't a physical length at all. It is something abstract.
Second, if you think all characters in a monospaced font would have always the same length on the screen, you are wrong. Create a new paragraph, type "XXXXXXXXXXXXXXXXXXX", put the cursor at the paragraph's start, and try the following:
Dim l As Long Dim p1 As Single Dim p2 As Single For l = 1 To 12 p1 = Selection.Information(wdHorizontalPositionRelativeToPage) Selection.MoveRight p2 = Selection.Information(wdHorizontalPositionRelativeToPage) MsgBox p2 - p1 Next
I get results from 7.34 to 6.75 !
From that I deduce that only an approximate solution will be possible. Maybe quite other ways could be recommended, like putting the text in a table and format the rightmost cell as leftaligned.
For calculating the position of the tabstop, whereby to find the right approximation factor is up to you:
Dim sTm As String ' temporary string Dim sRM As Single ' right margin Dim sPW As Single ' page width Dim sGt As Single ' gutter width Dim sPs As Single ' position of tabstop With ActiveDocument.PageSetup sRM = .RightMargin sPW = .PageWidth sGt = .Gutter End With sPs = sPW - sGt - sRM sTm = "Have some fun" ' deduce from tab position on left margin ' the approximately lenght of the string sPs = sPs - Len(sTm) * 7.5 * ApproxFactor ' ??? Selection.Paragraphs(1).TabStops.Add Position:=sPs ' Plus some unknown constants, possibly
Greetings from Bavaria, Germany
Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
singeredel - 01 Mar 2005 16:27 GMT Hi Helmut,
Thank you for your reply. I do not understand this "approximate factor." When I ran your code, I got 7.13 to 7.19, but I don't know what this means. Also, I ran your code for setting the tab, and it set it off the page. What is the "7.5" number you have in your sample code? This should be used plus the approximate factor? I tried using a number 7 where you had "ApproxFactor", but I ended up with no tab at all.
Thanks from Julie
> Hi Julie, > [quoted text clipped - 53 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 01 Mar 2005 20:35 GMT Hi Julie,
>When I ran your code, I got 7.13 to 7.19, but I don't know what this means. 7.13 to 7.19 would be the width of a character in points, according to the measuring method applied, which seems to be constantly 6, by the way, for courier new 10 point size with zoom factor 200 percent.
>I do not understand this "approximate factor." If you get different values for the width of a character, you have to decide, whether you want to use the minimum width, maximum width or the average or just add or deduct some constant value from the possibly varying width of the complete string. You might also call it a "corrective".
What was missing in my calculation was the left margin. :-(
So we got this, not regarding gutter width, which I left out:
Sub CalcTabPositionCell() Dim sTm As String ' temporary string Dim sRM As Single ' right margin Dim sLM As Single ' left margin Dim sPW As Single ' page width Dim sPs As Single ' position of tabstop With ActiveDocument.PageSetup sLM = .LeftMargin sRM = .RightMargin ' 70 sPW = .PageWidth ' 595 End With sPs = sPW - (sLM + sRM) sTm = "To be or not to be" ' deduce from tab position on left margin ' the approximately lenght of the string sPs = sPs - (Len(sTm)) * 6 - 2 ' corrective 2 Selection.Paragraphs(1).TabStops.Add _ Position:=sPs, _ Alignment:=wdAlignTabLeft End Sub
which puts a tabstop left from the right margin, just left enough to hold "To be or not to be". Given that a character is 6 points wide plus 2 points to correct fractions or inconsistencies in the calculation.
Tested with zoom 200 percent on 21 cm wide paper (DIN A4), left margin = 2.5 cm right margin = 2.5 cm no gutter
Greetings from Bavaria, Germany
Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
singeredel - 01 Mar 2005 21:37 GMT Thank you so much. Your test was successful using your string sTm; however, when I applied your code using my variables, for some reason it sets the tab at 6.31", which does not accommodate the length of the variable with a margin of 6.5". I am sorry to try your patience. Perhaps you would be kind enough to look at my latest version and see if you spot something I am doing wrong. My additions are indented. I never though this would be that difficult! :O
PatientName$ = "Smith, Joe" SSN$ = "555-55-5555" AttentionLine$ = "John Doe" DOE$ = "02/28/05"
Dim sTm As String ' temporary string Dim sRM As Single ' right margin Dim sLM As Single ' left margin Dim sPW As Single ' page width Dim sPs As Single ' position of tabstop Dim sLen As Single Dim sLen1 As Single Dim sLen2 As Single Dim sLen3 As Single Dim sLen4 As Single sLen1 = Len(PatientName$) sLen2 = Len(SSN$) sLen3 = Len(AttentionLine$) sLen4 = Len(DOE$)
If sLen1 >= sLen2 And sLen1 >= sLen3 And sLen1 >= sLen4 Then sTm = sLen1 If sLen2 >= sLen1 And sLen2 >= sLen3 And sLen2 >= sLen4 Then sTm = sLen2 If sLen3 >= sLen1 And sLen3 >= sLen2 And sLen3 >= sLen4 Then sTm = sLen3 If sLen4 >= sLen1 And sLen4 >= sLen2 And sLen4 >= sLen3 Then sTm = sLen4
With ActiveDocument.PageSetup sLM = .LeftMargin sRM = .RightMargin ' 72 sPW = .PageWidth ' 612 End With
sPs = sPW - (sLM + sRM) 'sTm = "To be or not to be" ' deduce from tab position on left margin ' the approximately lenght of the string sPs = sPs - (Len(sTm)) * 6 - 2 ' corrective 2 Selection.Paragraphs(1).TabStops.Add _ Position:=sPs, _ Alignment:=wdAlignTabLeft
Julie
> Hi Julie, > [quoted text clipped - 55 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 01 Mar 2005 22:07 GMT Hi Julie,
with a sense of humor,
I wish I never had answered before (not really) ;-) Now my obligation seems to be to finish it. I don't know whether I can do it.
To the co-readers: Any other input is most welcome!
Are you using a monospaced font?! What is all that
>If sLen1 >= sLen2 And sLen1 >= sLen3 And sLen1 >= sLen4 Then about?
Oh, I'm seeing something. You are assignign a variable of type single to a string. !!!
> Dim sTm As String ' temporary string > Dim sLen4 As Single If sLen4 >= sLen1 [...] sLen4 >= sLen3 Then sTm = sLen4
Prone to cause problems.
Greetings from Bavaria, Germany
Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
singeredel - 01 Mar 2005 23:21 GMT Okay, this is what I am looking for as a final result:
DEPARTMENT OF SOCIAL SERVICES RE: SMITH, JOE Disability and Adult Programs SSN: 555-55-5555 P.O. Box 60999 ATTN: JOHN DOE Los Angeles, CA 90060
The length of the data of the three items above called "RE:", "SSN:" and "ATTN:" (designated as PatientName$, SSN$, and AttentionLine$) needs to be determined so that all three of those lines can have two tabs set, one left-aligned tab to accommodate the longest of the three items which needs to line up with the right margin, and a second right-justified tab to line up RE:, SSN:, AND ATTN: This fluctuates depending on which of the three items is the longest. There are bookmarks at these three locations to insert the variable data.
I see the variable sTm was assigned as a String instead of a Single. However, this did not seem to make any difference after it was changed. The first tab stop showed up at 6.14", which still does not accommodate the longest variable data.
I hope this helps to clear up what I am trying to do. I know I have been a pain, but your help is very much appreciated. Thank you!
> Hi Julie, > [quoted text clipped - 26 lines] > Word XP, Win 98 > http://word.mvps.org/ singeredel - 02 Mar 2005 04:21 GMT Helmut -- Finally success! I discovered what the problem was. In your code you had: sPs = sPs - (Len(sTm)) * 6 - 2 ' corrective 2 sTm is already a length, and this was taking the length of a length. So I removed the Len(sTm) and made it just sTm, or: sPs = sPs - sTm * 6 - 2 ' corrective 2 But you got me there! Thank you!
Julie
> Hi Julie, > [quoted text clipped - 26 lines] > Word XP, Win 98 > http://word.mvps.org/
|
|
|