Forms and AutoText Problem

J

jerem

I have a form and I am using the following code to test if a check box is
checked and if so, to insert Autotext. Everything works fine, however, right
after the autotext is inserted I would like the cursor to return to the first
fill in formfield of the autotext just inserted. Right now, it is going to
the next fillin formfield after the checkbox. I've tried various code after
the line ".AttachedTemplate.AutoTextEntries(ATEntryName).Insert myRange,
True" and after the line InsertATEntry "Check1", "MoreClientInfo", True, but
have had no luck. I'm right now scouring the internet for solutions, but if
anybody can answer this I would greatly appreciate it. Please note, I need
to retain the Check1 Bookmark for a marker as to where to put the Autotext so
I can't mess with that (or at least that’s my thinking). Thanks for your
help.


Sub AdditionalClientList()
'
Dim ffname As String
ffname = Selection.FormFields(1).Name
With ActiveDocument
.Unprotect
If .FormFields(ffname).CheckBox.Value = True Then
InsertATEntry "Check1", "MoreClientInfo", True
Else
.Protect wdAllowOnlyFormFields, noreset
Exit Sub
End If
.Protect wdAllowOnlyFormFields, noreset
End With

End Sub

Public Sub InsertATEntry(BkmkName As String, ATEntryName As String, Optional _
bRestoreBkmk As Boolean = False)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) = True Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
On Error GoTo ATEError
.AttachedTemplate.AutoTextEntries(ATEntryName).Insert myRange,
True
.FormFields("Check1").CheckBox.Value = False
If bRestoreBkmk = True Then .Bookmarks.Add BkmkName, myRange
Else: MsgBox "Cannot find the bookmark " & BkmkName & "."
End If
End With
Exit Sub

ATEError:
MsgBox "The required AutoText entry " & ATEntryName & " could not be
found.", _
vbCritical, "AutoText Error"
End Sub
 
J

jerem

Just realized an additional problem here - the scenario is this - There is a
segment in the form that the user answers (a-j questions about a particular
client) then below that segment is a box that asks do you need to add new
client information. If the box is checked, a new set of a-j questions
appears (this comes from Autotext)and the user inputs answers to these
question for the second client, etc., etc. In addition to having the cursor
to return to the first fill in formfield of the autotext just inserted, I
need the additional a-j questions to go in sequence. Just realized that I'm
getting additional a-j questions but the new questions are always being added
in the "Check1" bookmark. So..... I'm wondering how to reset the bookmark to
the end of the previously inserted Autotext. Now, I know this makes perfect
sense to me, but this may get lost in translation.

Briefly stated, I need each Autotext insertion to come right behind the
previous one (right now my new insertions are always coming where my Check1
box and that is a problem) and, if possible, I'd like to be able to have the
cursor to be ready to input at the first formfield of the recently inserted
Autotext. Well, this may not be that brief, but I gave it a shot. Thanks
for your help.
 
G

Graham Mayor

If you are using the same autotext to insert more fields then undoubtedly
the fields inserted will be unnamed. It would be better to build the new
section using a macro - see the section "Repeat a block of formatted text
and form fields based upon the content of another form field" or "An
alternative method of adding a row to a protected table" at
http://www.gmayor.com/word_vba_examples.htm You can then select the next
field by name.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jerem

Hey Graham,

Thanks for the prompt reply. I must be doing something wrong here (which I
don't recognize) using the code below because I keep getting a Run time error
5941 at the line "sNum=ActiveDocument.FormFields("NoOfCases").Result.
Anything obvious jump out at you?

OR....any way of resetting bookmarks with the Autotext of the previous code
I submitted. Only reason why I'm enamored with that method is because all
the formatting is done right in the form - it avoids all the bold on/off, und
on/off, paragarph alignments, etc.. My form is a little more complex
(formatting-wise) than the example in your website (I've simplified it for
this example) and if I have to go digging for formatting code - that's
another long journey I'd rather not take. You know how people hate to go to
the dentist? Not me. I hate going to auto mechanics and scouring the
internet for code that fits my problem. Sometimes that's a massive task
especially when you're not sure what the Object is!




Dim sNum As Long
Dim oRng As Range
Dim bProtected As Boolean
Dim fCount As Long
Dim i As Long
Dim j As Long

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
sNum = ActiveDocument.FormFields("NoOfCases").Result
Selection.GoTo What:=wdGoToBookmark, Name:="bNoOfCases"
'Define the range object. This is where the cursor is at (start point)
Set oRng = Selection.Range
For i = 1 To sNum
With Selection
'Insert two blank lines
.TypeParagraph
.TypeParagraph
'Set the underline option
.Font.Underline = wdUnderlineSingle
'Set the bold option
'.Font.Bold = False
'Ensure that the paragraphs stay together on the same page
'.ParagraphFormat.KeepWithNext = True
.TypeText "Name of Client: "
.Font.Underline = wdUnderlineNone
'Add a form field
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
.Font.Underline = wdUnderlineSingle
'.Font.Bold = True
.TypeText "Description of matter: "
'Turn off the underline
.Font.Underline = wdUnderlineNone
'Turn off the bold option
'.Font.Bold = False
.TypeParagraph
.TypeParagraph
.TypeText "Value: "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
.TypeText "Scope (eg state/regional/national/international): "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
'.Font.Bold = True
.TypeText "Status (eg ongoing/concluded): "
'.Font.Bold = False
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
'.Font.Bold = True
.TypeText "Unique Features: "
'.Font.Bold = False
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
'.Font.Bold = True
.TypeText "Specific role played by the team: "
' .Font.Bold = False
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
'.Font.Bold = True
.TypeText "Role played by the wider firm, if applicable. This may
include the firm 's other departments or international offices: "
'.Font.Bold = False
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeParagraph
.TypeParagraph
'.Font.Bold = True
.ParagraphFormat.KeepWithNext = True
.TypeText "Team members involved and their role:"
.TypeParagraph
.TypeParagraph
'.Font.Bold = False
.TypeText "Members: "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeText "Their Role: "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
'Turn off the Keep with next option
.ParagraphFormat.KeepWithNext = False
.TypeText "Which firm(s) and attorney(s) represented the other
party/parties:"
.TypeParagraph
.TypeParagraph
'.Font.Bold = False
.TypeText "Firm: "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
.TypeText "Their Role: "
.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
'Turn off the Keep with next option
.ParagraphFormat.KeepWithNext = False
.TypeParagraph
.TypeParagraph
End With
Next i
'The selection has moved down the page. Redefine the end of the range object.
oRng.End = Selection.Range.End
'Recreate the bookmark.
ActiveDocument.Bookmarks.Add "bNoOfCases", oRng
'Count the form fields added to the range
fCount = oRng.FormFields.Count
'Give each of the added fields a unique bookmark name
For j = 1 To fCount
With oRng.FormFields(j)
.Name = "NoOfCases" & j 'Add a unique bookmark name
.Enabled = True 'Enable the field for user entry
.CalculateOnExit = False 'Uncheck the calculate on exit check box
End With
Next j
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
'Select the first of the fields in the range
ActiveDocument.FormFields("NoOfCases1").Select
End Sub
 
G

Graham Mayor

Runtime Error 5941 suggests there is no field name in the document that
matches "NoOfCases".

If the field names are otherwise unimportant, and you want to use your
autotext entry and the aim of the game is to select the first form field in
your autotext entry for completion, then you could set a range from the
start of the insertion to the end of the document and select the first field
in the range e.g.

Dim oRng As Range
Dim i As Long
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
'Locate the bookmark to place the entry
Set oRng = ActiveDocument.Bookmarks("bookmarkname").Range
NormalTemplate.AutoTextEntries("Autotextname").Insert _
Where:=orng
'set the end of the tange to the end of the document
oRng.End = ActiveDocument.Range.End
'select the first field in the range
oRng.FormFields(1).Select
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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