Set Tabs based on distance from right margin

S

singeredel

I need to set two tabs to the left of the right margin based on the length of
a string variable. This is what I have, but I don't know how to specify the
right margin in a variable. Please don't laugh too hard! :D

Dim CTab As Variant
Dim CTab1 As Variant
Dim CTab2 As Variant
Dim sLen As Variant
Dim sLen1 As Variant
Dim sLen2 As Variant
Dim sLen3 As Variant
Dim sLen4 As Variant

sLen1 = Len(PatientName$)
sLen2 = Len(SSN$)
sLen3 = Len(AttentionLine$)
sLen4 = Len(DOE$)

rMar = ?? establish right margin here

CTab1 = rMar - Len(sLen) - (6 / 72)
CTab2 = CTab1 - 0.15

If sLen1 >= Len2 And Len1 >= Len3 And Len1 >= Len4 Then sLen = sLen1
If sLen2 >= Len1 And Len2 >= Len3 And Len2 >= Len4 Then sLen = sLen2
If sLen3 >= Len1 And Len3 >= Len2 And Len3 >= Len4 Then sLen = sLen3
If sLen4 >= Len1 And Len4 >= Len2 And Len4 >= Len3 Then sLen = sLen4

Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(rMar
- CTab1), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Selection.ParagraphFormat.TabStops.Add
Position:=InchesToPoints(CurTab2), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
 
S

singeredel

Doh! I am losing it! That gives me the right margin, but something else is
wrong in the code because it is not given me the tab position the length of
the string variable from the right margin.
 
D

Dave Lett

Hi Julie,

Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(rMar - CTab1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces


The second line: was this supposed to be just CTab1, as in the following
(you use rMar to calculate a value for CTab1 a couple lines earlier)?
Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(CTab1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces

And in the next block of code, you specify the position of the right-aligned
tabstop as CurTab2, but you've only defined CTab2, is this correct?

HTH,
Dave
 
S

singeredel

Hi Dave,

I had defined CTab1 = rMar - Len(sLen) - (6 / 72) previously. Was this
incorrect? You have it in the Add Tabstops section. Does this make a
difference? However, I get a run-time error 5149: The measurement must be
between -1584 pt and 1584 pt." I am wondering if it has to do with this
"Inches to Points" thing because I am already dealing with points, aren't I?
I couldn't find anything else that fit.
 
S

singeredel

Ignore the code in the last post and use the following:

Dim CTab As Variant
Dim CTab1 As Variant
Dim CTab2 As Variant
Dim sLen As Variant
Dim sLen1 As Variant
Dim sLen2 As Variant
Dim sLen3 As Variant
Dim sLen4 As Variant

sLen1 = Len(PatientName$)
sLen2 = Len(SSN$)
sLen3 = Len(AttentionLine$)
sLen4 = Len(DOE$)


If sLen1 >= sLen2 And sLen1 >= sLen3 And sLen1 >= sLen4 Then sLen =
sLen1
If sLen2 >= sLen1 And sLen2 >= sLen3 And sLen2 >= sLen4 Then sLen =
sLen2
If sLen3 >= sLen1 And sLen3 >= sLen2 And sLen3 >= sLen4 Then sLen =
sLen3
If sLen4 >= sLen1 And sLen4 >= sLen2 And sLen4 >= sLen3 Then sLen =
sLen4

rMar = ActiveDocument.PageSetup.RightMargin
CTab1 = Len(sLen + (6 / 72))
CTab2 = CTab1 - 0.15


Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(rMar - CTab1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces

Selection.ParagraphFormat.TabStops.Add _
Position:=InchestToPoints(CTab2), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
 
D

Dave Lett

So you have everything working then?

singeredel said:
Ignore the code in the last post and use the following:

Dim CTab As Variant
Dim CTab1 As Variant
Dim CTab2 As Variant
Dim sLen As Variant
Dim sLen1 As Variant
Dim sLen2 As Variant
Dim sLen3 As Variant
Dim sLen4 As Variant

sLen1 = Len(PatientName$)
sLen2 = Len(SSN$)
sLen3 = Len(AttentionLine$)
sLen4 = Len(DOE$)


If sLen1 >= sLen2 And sLen1 >= sLen3 And sLen1 >= sLen4 Then sLen =
sLen1
If sLen2 >= sLen1 And sLen2 >= sLen3 And sLen2 >= sLen4 Then sLen =
sLen2
If sLen3 >= sLen1 And sLen3 >= sLen2 And sLen3 >= sLen4 Then sLen =
sLen3
If sLen4 >= sLen1 And sLen4 >= sLen2 And sLen4 >= sLen3 Then sLen =
sLen4

rMar = ActiveDocument.PageSetup.RightMargin
CTab1 = Len(sLen + (6 / 72))
CTab2 = CTab1 - 0.15


Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(rMar - CTab1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces

Selection.ParagraphFormat.TabStops.Add _
Position:=InchestToPoints(CTab2), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
 
D

Dave Lett

Hi Julie,

Well, not knowing what you're values are puts me in a difficult position. At
the end of your routine (before "End Sub"), add the following and let's see
what we get:

MsgBox "CTab1:" & vbTab & CTab1 & vbCrLf & _
"CTab2:" & vbTab & CTab2 & vbCrLf & _
"rMar - CTab1:" & vbTab & rMar - CTab1

This way, we can see which values are outside the measurement range.

Dave
 
S

singeredel

Hi Dave,

Thanks for your help. The values displayed were:
CTab1 = 16
CTab2 = 15.85
rMar - CTab1 = 56

Obviously my logic is wrong. The right margin is set at 72 points (1") from
the right edge of the paper size of 8-1/2 x 11. It should probably be rMar +
CTab1. But the problem for me is how to convert this number to a tab location
based on points. Certainly CTab1 cannot be set at 70+16 or 86 points (86
points from the right margin maybe, but how do you write code to tell it
that?).

Thanks again!

Julie

Right now I am just using the following as strings to test:

PatientName$ = "Smith, Joe"
SSN$ = "555-55-5555"
AttentionLine$ = "John Doe"
DOE$ = "02/28/05"
 
D

Dave Lett

Hi Julie,

You have
rMar - CTab1 = 56

Both values are in points.

Then, the code has
Selection.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(rMar - CTab1), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces

See the problem? You're inflating something (rMar - CTab1), which is already
in points (i.e., 56 points), by treating it as if it were in Inches.

HTH,
Dave
 
S

singeredel

I did try taking the InchesToPoints out previously and still got an error,
but perhaps it was due to something else. I tried removing it again and now
the tabs are set from the left margin at .22" and .78". So I guess I need to
figure the distance from the left margin?? round and round and round I go! :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top