Fred was telling us:
Fred nous racontait que :
Is it possible, using vb, to change the format of a Text form field in
a Table, dependent upon the data that has been entered ? I have the
situation where a cell, indicating the status of a kick-off meeting,
is to show any of the following values,
TbD To be Decided whether required or not
N/A Not Applicable/required to hold a meeting
100% Meeting held
dd-mmm-yyyy scheduled date for the meeting
I have it set at present to Regular Text, Unlimited. What I would like
to be able to do is switch to Date (dd-mmm-yyyy) or Number (%) or
leave it as text, based upon the data entered.
This can be a bit hairy and totally dependent on user good behaviour...
First, since the format may change from entering to leaving the field, you
may get a problem so that the user is not allow to leave the field if, for
example, it was set to date but it is changed to "TbD"; in this case, "TbD"
is not a valid date format and Word will not let the user leave the field.
This is why I included a macro for Entry to reset the field format to
regular text upon entry:
OnEntry macro:
'_______________________________________
Sub ResetFormat()
Dim ffTarget As FormField
Dim strFFResult As String
Set ffTarget = Selection.Paragraphs(1).Range.FormFields(1)
With ffTarget
strFFResult = .Result
.Result = ""
With .TextInput
.EditType Type:=wdRegularText, Format:=""
End With
.Result = strFFResult
End With
End Sub
'_______________________________________
OnExit macro:
'_______________________________________
Sub ChangeFormat()
Dim ffTarget As FormField
Dim strFFResult As String
Set ffTarget = Selection.Paragraphs(1).Range.FormFields(1)
With ffTarget
strFFResult = .Result
.Result = ""
If IsNumeric(strFFResult) Then
With .TextInput
.EditType Type:=wdNumberText, Format:="0%"
End With
strFFResult = CStr(CLng(strFFResult) / 100)
Else
If IsNumeric(Replace(strFFResult, "-", "")) Then
With .TextInput
.EditType Type:=wdDateText, Format:="dd-MMM-yyyy"
End With
Else
With .TextInput
.EditType Type:=wdRegularText, Format:=""
End With
End If
End If
.Result = strFFResult
End With
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
ISTOO
Word MVP site: http://www.word.mvps.org