Rightasrainy
10-10-2012, 04:12 AM
Hello my clever friends!
I am afraid to say I am a complete VBA newb, so please excuse my simple explanation...
I am creating a new user form in Word, and need to make some fields mandatory. So far I am using;
Public Sub ExitText()
With GetCurrentFF
If Len(Trim(.Result)) = 0 Then
MsgBox "You can't leave " & .Name & " blank"
End If
End With
End Sub
Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function
Which works fine, BUT after the error message is given the cursor jumps to the next form field. I believe there is a bug that means the macro runs too quickly, and as a result, although the cursor does return to the original formfield for an instant, it then jumps to the formfield you tabbed to or clicked in... I have found this online;
Fortunately, you can get round this timing bug (as indeed you can get around most of Word VBA's timing bugs) by using Application.Ontime. The following pair of macros working together do work
Sub ExitText2()
With ActiveDocument.FormFields("Text2")
If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
MsgBox "The first three letters must be 'KLM'"
End If
End With
End Sub
Sub GoBacktoText2()
ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub
But I'm not sure how to apply the Application Online Macro to my current Macro??
If someone can solve this you will make me a very happy lady.
Thank you in advance!
Lorraine
I am afraid to say I am a complete VBA newb, so please excuse my simple explanation...
I am creating a new user form in Word, and need to make some fields mandatory. So far I am using;
Public Sub ExitText()
With GetCurrentFF
If Len(Trim(.Result)) = 0 Then
MsgBox "You can't leave " & .Name & " blank"
End If
End With
End Sub
Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function
Which works fine, BUT after the error message is given the cursor jumps to the next form field. I believe there is a bug that means the macro runs too quickly, and as a result, although the cursor does return to the original formfield for an instant, it then jumps to the formfield you tabbed to or clicked in... I have found this online;
Fortunately, you can get round this timing bug (as indeed you can get around most of Word VBA's timing bugs) by using Application.Ontime. The following pair of macros working together do work
Sub ExitText2()
With ActiveDocument.FormFields("Text2")
If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
MsgBox "The first three letters must be 'KLM'"
End If
End With
End Sub
Sub GoBacktoText2()
ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub
But I'm not sure how to apply the Application Online Macro to my current Macro??
If someone can solve this you will make me a very happy lady.
Thank you in advance!
Lorraine