Log in

View Full Version : Help formatting string variable



jimh
07-08-2010, 07:49 PM
I'm not overly experienced with scripting macros and need some help with something that has been stumping me all day. My code is similar to the following:

Sub thisStatement()
Dim MyText As String
MyText = "Use this date " & Format((Date + 90), "d/m/yy") Selection.TypeText (MyText)
End Sub

What I want to do though is highlight the text in yellow and put a border around it so that when the sub is called, the statement is already formatted.

Nothing I've tried has worked and I haven't been able to find any examples of something similar.

Can anyone help? Thanks

jimh

gmaxey
07-09-2010, 03:24 AM
Would something like this work for you?

Sub thisStatement()
Dim MyText As String
MyText = "Use this date " & Format((Date + 90), "d/m/yy")
Selection.Text = MyText
With Selection
.Range.HighlightColorIndex = wdYellow
With .Font
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
End With
End Sub

fumei
07-12-2010, 09:43 AM
String variables themselves do not have format. Thus Greg's code inserts the string, and THEN formats the text.

"the statement is already formatted"

This is not possible - at least in terms of using a string variable. String variables have no format.

As an alternative, you may want to consider using Styles. Demo attached. Click "This Statement on top toolbar. Now, it probably does not perfectly fit what you may want, as you do not state clearly exactly what you do want.

By highlight and boder, do you mean just the text? The paragraph? Will it BE a whole paragraph? Do you need an additonal paragraph to get the Selection to? Why exactly are you using Selection anyway? Are you want to put the text string: "Use this date " & Format((Date + 90), "d/m/yy")" at the Selection point?

In any case, the format is handled by a Style "MyDate" and uses:
Sub ThisStatement()
With Selection
.Collapse 1
.Style = "MyDate"
.TypeText "Use this date " & _
Format((Date + 90), "d/m/yy")
End With
End Sub
I do not see why you need a string variable at all frankly.