PDA

View Full Version : Solved: Find spaces in a string and replace?



clhare
09-19-2006, 12:12 PM
Hi!

Is it possible to do replace spaces in a string with non-breaking spaces? I'm trying to make sure dates that are inserted by the macro do not split into two lines (especially after the month).

mdmackillop
09-20-2006, 06:24 AM
Sub AddDate()
strdate = Format(Now(), "dddd d mmmm yyyy")
strdate = Replace(strdate, Chr$(32), Chr$(160))
Selection.TypeText strdate
End Sub

clhare
09-28-2006, 04:13 AM
I tried to apply this solution to my procedure, but I must be doing something wrong. It still doesn't fix the spacing in the date, and it's not formatting the date either. The date will be entered on a user form. In the form's code, I have the following:

' Declare variables
Dim strAssumedPCD1 As Date
Dim strAssumedPCD2 As Date

' Assign values to variables
strAssumedPCD1 = Format(txtAssumedPCD.Value, , "mm-dd-yyyy")
strAssumedPCD2 = Format(strAssumedPCD1, "mmmm d, yyyy")
strAssumedPCD2 = Replace(strAssumedPCD2, Chr$(32), Chr$(160))

' Replace variables with user's text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
' FOR NUMERIC FORM OF DATE
.Text = "(date2n)"
.Replacement.Text = strAssumedPCD1
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
' FOR ALPHA FORM OF DATE
.Text = "(date2a)"
.Replacement.Text = strAssumedPCD2
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll


When I run my macro, the dates in base cases end up appearing as "1/1/2006" (I entered "1-1-2006" in the user form). So neither one is formatting as I tried to set it up to do in the macro.

RELATED QUESTION:
I also want to be able to replace spaces in a string when I don't know how many spaces are there (such as in a string that contains a person's name--they may or mat not have a middle initial, Jr. at the end, etc.) The name will be entered on a user form. Can I change spaces within the entered name to required spaces somehow?

Thank you for your help!

mdmackillop
09-28-2006, 06:00 AM
Is it working properly in your Word document?
If it doesn't work within a userform, I don't know what more to suggest.