Incredibly frustrated with Macro

K

Kim K

I have a macro that I am using that works like this, drop down selection has
5 choices, if choice 2 or 3 is selected then a message will pop up stating
that the next field needs further clarification and to please fill in, in
otherwards it will not allow the user to go past the next field if choice 2
or 3 is not clarified.

I can set up my document and get this in and test it - works fine. I then
build from there, adding header, footer, tables, check boxes, popup calendar
macro, list box macros, specify continuous breaks and protections, then test

All macros work except the choice 2-3 macro. It allows for bypass and does
not show pop up message.

What am I doing wrong?

This is word 07 saved in dot format for use on re is macro code I am using:

Option Explicit
Public mstrFF As String
Public Sub AOnExit()
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
With GetCurrentFF
Select Case .Name
Case Is = "SchoolType"
Select Case .Result
Case Is = "Other District School", "Private School/Home Schooled"
ActiveDocument.FormFields("MyText").Enabled = True
ActiveDocument.Bookmarks("MyText").Range.Fields(1).Result.Select
Case Else
ActiveDocument.FormFields("MyText").Result = ""
ActiveDocument.FormFields("MyText").Enabled = False
ActiveDocument.Bookmarks("MyText2").Range.Fields(1).Result.Select
End Select
Case Is = "MyText"
If Len(.Result) < 1 Then
mstrFF = .Name
End If
End Select
End With
End Sub
Public Sub AOnEntry()
Dim strCurrentFF As String
If LenB(mstrFF) > 0 Then
DoEvents
MsgBox "Please list Other District School or Private School/Home Schooled"
ActiveDocument.Bookmarks(mstrFF).Range.Fields(1).Result.Select
mstrFF = vbNullString
End If
End Sub
Public 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
 
J

Jean-Guy Marcil

Kim K said:
I have a macro that I am using that works like this, drop down selection has
5 choices, if choice 2 or 3 is selected then a message will pop up stating
that the next field needs further clarification and to please fill in, in
otherwards it will not allow the user to go past the next field if choice 2
or 3 is not clarified.

I can set up my document and get this in and test it - works fine. I then
build from there, adding header, footer, tables, check boxes, popup calendar
macro, list box macros, specify continuous breaks and protections, then test

All macros work except the choice 2-3 macro. It allows for bypass and does
not show pop up message.

What am I doing wrong?

There could be many things going on...
First, to make sure that the macro is indeed activated when you exit the
dropdown, temporarily add a Stop command at the top of the code:

Public Sub AOnExit()
Dim oDoc As Word.Document

Stop

Set oDoc = ActiveDocument
With GetCurrentFF

Then reprotect your document and try to leave the dropdown. The compiler
should stop on the Stop line if the macro is activated. From there, use F8 to
step through the code one step at the time and see what is going on (or not
going on, as the case maybe...!)

If the compiler does not take you in debugging mode to the Stop line, then
it means that the code is not activated when you leave your dropdown
formfield. Re-check its properties or the security level in Word...

By the way, I see in your code:

Dim oDoc As Word.Document
Set oDoc = ActiveDocument

But I do not see the oDoc object being used anywhere... So why bother
creating an object if you are not going to use it? If you created it, then
you may want to use it in those lines, for example:

ActiveDocument.FormFields("MyText").Enabled = True
ActiveDocument.Bookmarks("MyText").Range.Fields(1).Result.Select

could be

With oDoc.FormFields("MyText")
.Enabled = True
.Range.Fields(1).Result.Select
End With

and
ActiveDocument.FormFields("MyText").Result = ""
ActiveDocument.FormFields("MyText").Enabled = False
ActiveDocument.Bookmarks("MyText2").Range.Fields(1).Result.Select

could be

With oDoc.FormFields
With .Item("MyText")
.Result = ""
.Enabled = False
End With
.Item("MyText2").Range.Fields(1).Result.Select
End With
 
F

fumei via OfficeKB.com

"So why bother creating an object if you are not going to use it? "

Indeed.

You could go even further and make a formfield object of the collection.

Dim DocFF As FormFields
Set DocFF = ActiveDocument.FormFields

With DocFF("MyText")
.Enabled = True
.Range.Fields(1).Result.Select
End With


Or even more specifically/explicitly

Dim DocFF As FormFields
Dim oFF As FormField ' notice singular NOT plural

Set DocFF = ActiveDocument.FormFields
Set oFF = DocFF("MyText")

With oFF
.Enabled = True
.Range.Fields(1).Result.Select
End With


Jean-Guy Marcil said:
I have a macro that I am using that works like this, drop down selection has
5 choices, if choice 2 or 3 is selected then a message will pop up stating
[quoted text clipped - 10 lines]
What am I doing wrong?

There could be many things going on...
First, to make sure that the macro is indeed activated when you exit the
dropdown, temporarily add a Stop command at the top of the code:

Public Sub AOnExit()
Dim oDoc As Word.Document

Stop

Set oDoc = ActiveDocument
With GetCurrentFF

Then reprotect your document and try to leave the dropdown. The compiler
should stop on the Stop line if the macro is activated. From there, use F8 to
step through the code one step at the time and see what is going on (or not
going on, as the case maybe...!)

If the compiler does not take you in debugging mode to the Stop line, then
it means that the code is not activated when you leave your dropdown
formfield. Re-check its properties or the security level in Word...

By the way, I see in your code:

Dim oDoc As Word.Document
Set oDoc = ActiveDocument

But I do not see the oDoc object being used anywhere... So why bother
creating an object if you are not going to use it? If you created it, then
you may want to use it in those lines, for example:

ActiveDocument.FormFields("MyText").Enabled = True
ActiveDocument.Bookmarks("MyText").Range.Fields(1).Result.Select

could be

With oDoc.FormFields("MyText")
.Enabled = True
.Range.Fields(1).Result.Select
End With

and
ActiveDocument.FormFields("MyText").Result = ""
ActiveDocument.FormFields("MyText").Enabled = False
ActiveDocument.Bookmarks("MyText2").Range.Fields(1).Result.Select

could be

With oDoc.FormFields
With .Item("MyText")
.Result = ""
.Enabled = False
End With
.Item("MyText2").Range.Fields(1).Result.Select
End With
 

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

Top