Using macros in forms

D

drtomrug

I have prepared a template that includes text boxes, drop down menus, etc.
The bind I have is:
if I keep the document protected (and I am limited to filing in the forms),
none of my macros work (e.g., I prepared macros to type out text I use often
and set up a toolbar with the macro buttons). I get an error message stating:
Run-time error "4605": This method or property is not available because the
object refers to a protected area of the document."

Therefore, to use the drop down menus and other form features, i have to
constantly switch from protected (to use the drop down menus) and unprotected
) to write text so that I can use the macros and be time efficient.

any advice?
 
G

Graham Mayor

You need to add the lock/unlock code to your macro(s).. If you use a
password, insert it in the code between the two sets of quotes.

Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

'*********do your thing***************

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

drtomrug

Thank you very much for the good advice, but only one more problem...

When the macro re-protects the form, I lose the place where I was typing;
the entire field becomes selected (so that if I continue to type something,
the entire field is replaced with the typed input). Is there any way to have
the form maintain my placement, so that when I continue typing, I do not lose
my place...?

Alternatively, is there any way to use drop down menus without ever having
to use a protected form?

I am extremely grateful for your assistance...

drtomrug
 
G

Graham Mayor

Without knowing what you are doing in your code it is difficult to make a
suggestion how best to handle it, but you can select the field where you
want the cursor to be in your macro. If you don't want to lock the form then
you would have to use userforms to gather and select data.
See Word MVP FAQ - Userforms http://word.mvps.org/FAQs/Userforms.htm . Greg
Maxey also has some useful stuff on userforms on his web site
http://gregmaxey.mvps.org/word_tips.htm
If you have further questions, then if no-one picks them up I will have a
look tomorrow. Its time to put up my feet for the day :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

drtomrug

I tried your advice and experimental with User Forms. I created a Template
"UserForm" and used design mode to insert first a ComboBox (ComboBox1) and a
ListBox (ListBox1). Two problems...

I tried the
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "07"
.AddItem "08"
.AddItem "09"
End With
End Sub

And the array method to try to populate the ComboBox and the ListBox. When I
leave design mode, the boxes are not populated with these choices; they
remain blank when I click on them.

When I re-open the template (either as a document or as a template), the
Combo Box and List Box are present, but Visual Basic lacks the code I created
for these Boxes.

What gives? I am moderately frustrated at this point...

All I need is an option to use drop down menus plus free text in a manner
that allows me to use macros. The only two things I need the macros to do is
to insert some text at the point of the cursor without losing my place, and
sometimes find (in the up direction) a "bookmark" (ZZZ) that I use as a place
mark for necessary text.

Help...

drtomrug
 
G

Graham Mayor

You remain rather coy about *exactly* what you are trying to do which makes
it difficult to provide help.

You appear to have saved the code (of which that you have reproduced is only
part) somewhere other than with the form itself. Double click on a blank
area of the form in the vba editor and paste your code.

The box is populated when you display it in the document.

For userform questions you would do better asking in the Userform or vba
forums

Just a thought - you can use Dropdown form fields in a locked form and add
text to that available using vba. Add an extra list item called "Enter your
own" then run the following macro on exit from the form field (Dropdown1
shown here).

Sub EnterYourOwn()
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
Select Case oFld("Dropdown1").Result
Case Is = "Enter your own"
start:
sText = InputBox("Enter the text for this field")
If sText = "" Then
MsgBox "This field may not be left blank"
GoTo start:
End If
oFld("Dropdown1").Result = sText
Case Else
'Do nothing
End Select
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

drtomrug

I really appreciate your help. the last recommendation will serve me well.
here is an example of what needs to be done:

I have to create a write up from a patient encounter. I have to have free
text, such as in the history of present illness. part of that free text is
the same text from prior evaluations, so I created macros to print commonly
repeated segments, such as "There is no evidence of depression, hypomania, or
anxiety on detailed questioning." or "Anxiety has regularly stopped an
activity."

In other areas (such as in recommendations), there is an entire paragraph
that I need to include. In that case, I prepared the paragraph in a separate
template and set up a macro to pull up the template, copy all, close the
template, and paste into my document.

Since these require the use of macros, I complete these sections while the
document is unprotected.

However, there are other sections that would be much easier to use a drop
down menu system, such as in the Review of Systems. I will have drop down
menus for the system to be discussed (HEENT:, Respiratory:, Cardiovascular:,
etc.) next to a text field. Since these require the drop down menus, I need
to protect the document. However, when I type in a text field, I was unable
to use the macros until your recommendation above. However with your
recommendation above, if I use the drop down menu to choose, say,
"Psychiatriatric:" and then type in the text field "Out of control anger "
followed by the "has not been significantly reported recently. " macro, when
the macro is finished, the entire "Out of control anger has not been
significantly reported recently. " is highlighted, so that if I simply
continue to type, I replace the entire field. It would be nice if there was a
system that would allow me to use the macro and end up at the end of the
inserted macro so that I could just continue to type. Remember, the macro may
not be inserted at the end of the text (i.e., If the field was populated with
"Anxiety has been treated with ZZZ without side effects." I may highlight
"ZZZ" and hit a "Celexa" macro, or I may need to strike several sequential
macros, such as "Celexa" followed by a "and psychotherapy" macro. However, if
the entire field is highlighted, or if I embellish on the macro to go to the
end, the subsequent macro insertions are incorrectly placed.

When you advised using the UserForm, I went to the web pages you
recommended, and I used their code recommendations. When I had only one
ComboBox in the template, what you saw was the entire code -- it was not just
part of the code -- if there is something missing, I do not know what is
missing to get the ComboBox to populate.

This is rather lengthy, so you may understand why I was not being coy, I was
just trying to be efficient. Now, you can see exactlyt what I am trying to
do...

I will also post this at the UserForms forum to see what help I may be able
to get...

However, if you do have any recommendations, I would be very appreciative.
Thank you for the time you have spent on this request...

drtomrug
You remain rather coy about *exactly* what you are trying to do which makes
it difficult to provide help.

You appear to have saved the code (of which that you have reproduced is only
part) somewhere other than with the form itself. Double click on a blank
area of the form in the vba editor and paste your code.

The box is populated when you display it in the document.

For userform questions you would do better asking in the Userform or vba
forums
Just a thought - you can use Dropdown form fields in a locked form and add
text to that available using vba. Add an extra list item called "Enter your
own" then run the following macro on exit from the form field (Dropdown1
shown here).

Sub EnterYourOwn()
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
Select Case oFld("Dropdown1").Result
Case Is = "Enter your own"
start:
sText = InputBox("Enter the text for this field")
If sText = "" Then
MsgBox "This field may not be left blank"
GoTo start:
End If
oFld("Dropdown1").Result = sText
Case Else
'Do nothing
End Select
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

For free text you could include an unprotected section in your form.

On the case of the code you quoted, this will indeed populate the box, but
you need additional code to display the poulated form and yet more to handle
your selection. For list boxes in userforms
http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm is an excellent
primer and his cascading list boxes would seem an ideal solution to some of
your problems. You can download a sample document from that link that
contains the macros.

Cross his palm with silver and I am sure he will help you work it all out ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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