Variable question

L

LEU

I have the following macros that I can’t get to work. It draws my line
correctly but I can’t get it to put the text from my variable in front of it.
What am I doing wrong?


Sub test1()
Dim myText As String
myText = "PV"
Call test4
End Sub

Sub test2()
Dim myText As String
myText = "IV"
Call test4
End Sub

Sub test3()
Dim myText As String
myText = "SI"
Call test4
End Sub


Sub test4()
Dim myText
With Selection.ParagraphFormat
.RightIndent = InchesToPoints(0.48)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.Font.Underline = False
Selection.TypeText Text:=myText
i = Selection.Information(wdVerticalPositionRelativeToPage)
Set oFFline = ActiveDocument.Shapes.AddLine(554, i + 12, 524, i + 12)
With oFFline.Line
.Weight = 0.75
End With
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
 
P

Pesach Shelnitz

In your calls to test4 from test1, test2, and test3, you need to pass the
string defined in each case to Test4. To enable passage of the string, you
need to specify the string in the calls to test4 and to modify test4 so that
it will accept the string passed from the calling macros. To specify that the
string should be passed from test1, test2, and test3 to test4, change "Call
test4" to "Call test4(myText)" in test1, test2, and test3. To enable test4 to
receive and use the string passed, change the first line of test4 from "Sub
test4()" to "Sub test4(myText As String)" and remove the line "Dim myText" in
test4. This line needs to be removed because it declares the variable myText,
which is already declared in the first line of test4. The result of these
changes is as follows:

Sub test1()
Dim myText As String
myText = "PV"
Call test4(myText)
End Sub

Sub test2()
Dim myText As String
myText = "IV"
Call test4(myText)
End Sub

Sub test3()
Dim myText As String
myText = "SI"
Call test4(myText)
End Sub

Sub test4(myText As String)
With Selection.ParagraphFormat
.RightIndent = InchesToPoints(0.48)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.Font.Underline = False
Selection.TypeText Text:=myText
i = Selection.Information(wdVerticalPositionRelativeToPage)
Set oFFline = ActiveDocument.Shapes.AddLine(554, i + 12, 524, i + 12)
With oFFline.Line
.Weight = 0.75
End With
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
 
L

LEU

Pesach,

Thank you for the help. That worked.



Pesach Shelnitz said:
In your calls to test4 from test1, test2, and test3, you need to pass the
string defined in each case to Test4. To enable passage of the string, you
need to specify the string in the calls to test4 and to modify test4 so that
it will accept the string passed from the calling macros. To specify that the
string should be passed from test1, test2, and test3 to test4, change "Call
test4" to "Call test4(myText)" in test1, test2, and test3. To enable test4 to
receive and use the string passed, change the first line of test4 from "Sub
test4()" to "Sub test4(myText As String)" and remove the line "Dim myText" in
test4. This line needs to be removed because it declares the variable myText,
which is already declared in the first line of test4. The result of these
changes is as follows:

Sub test1()
Dim myText As String
myText = "PV"
Call test4(myText)
End Sub

Sub test2()
Dim myText As String
myText = "IV"
Call test4(myText)
End Sub

Sub test3()
Dim myText As String
myText = "SI"
Call test4(myText)
End Sub

Sub test4(myText As String)
With Selection.ParagraphFormat
.RightIndent = InchesToPoints(0.48)
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.Font.Underline = False
Selection.TypeText Text:=myText
i = Selection.Information(wdVerticalPositionRelativeToPage)
Set oFFline = ActiveDocument.Shapes.AddLine(554, i + 12, 524, i + 12)
With oFFline.Line
.Weight = 0.75
End With
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
 

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

Similar Threads

Assigning a String to a Variable in Word 2003 3
Multiple Tables 2
Set a range on a variable sheet? 2
Draw Table 4
Table resizing 2
Find and Replace Question 3
If Then Else question 3
Populating a Table 8

Top