confused deciphering this code

A

Angyl

I found this code here on the board that more or less does exactly what I
want (insert a new row of fields in a table if you want it to), but I have a
problem with it:

Sub addrow()


response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect

Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)

Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text1row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text2row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "tex3row" & myRow
.Enabled = True
.ExitMacro = "addrow"
End With
myNewField = "text1row" & myRow
ActiveDocument.Protect NoReset:=True,
Type:=wdAllowOnlyFormFields
ActiveDocument.Range.FormFields(myNewField).Select
End If

End Sub
~~~~~~

The problem is that if I add other text fields to the document (and I need
to) there is an error with that last line:
ActiveDocument.Range.FormFields(myNewField).Select

I don't understand it. It takes the extra named form field I had in the
document and renames it, then there is the error. I've tried reading the
code to understand what it's doing but I'm boggled. Help?

Question 2:

The field to which I am adding this macro calculates figures from other
fields in its row. How would I modify the code to ADD that calculation for
the figures in ITS row the same way it's adding the macro?
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?QW5neWw=?=,
I found this code here on the board that more or less does exactly what I
want (insert a new row of fields in a table if you want it to), but I have a
problem with it:

Sub addrow()


response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect

Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)

Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text1row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text2row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "tex3row" & myRow
.Enabled = True
.ExitMacro = "addrow"
End With
myNewField = "text1row" & myRow
ActiveDocument.Protect NoReset:=True,
Type:=wdAllowOnlyFormFields
ActiveDocument.Range.FormFields(myNewField).Select
End If

End Sub
~~~~~~

The problem is that if I add other text fields to the document (and I need
to) there is an error with that last line:
ActiveDocument.Range.FormFields(myNewField).Select

I don't understand it. It takes the extra named form field I had in the
document and renames it, then there is the error. I've tried reading the
code to understand what it's doing but I'm boggled. Help?
I'm a bit boggled as well, but as a wild guess:

Formfield names are also bookmarks. Bookmark names must be unique in a
document. If the formfield name already exists when you insert a new formfield,
that formfield will insert without a name. When this happens, you cannot set
the formfield name using the code you show; you'd need to use special code,
which you'll find on the word.mvps.org website.

I try to avoid this situation altogether. So when I insert "new rows". I'll
create a table with a standard number of rows. I'll name the fields in one of
the rows with a "base name", then I select the row and save it as AutoText. I
next name all the form fields in the rows with incrementing numbers ("base1,
base2, etc"). When I want a new row, I insert the AutoText entry, then rename
the formfields already in it with the next incrementing number.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
A

Angyl

Makes perfect sense, Cindy, Thanks!

Cindy M -WordMVP- said:
Hi =?Utf-8?B?QW5neWw=?=,

I'm a bit boggled as well, but as a wild guess:

Formfield names are also bookmarks. Bookmark names must be unique in a
document. If the formfield name already exists when you insert a new formfield,
that formfield will insert without a name. When this happens, you cannot set
the formfield name using the code you show; you'd need to use special code,
which you'll find on the word.mvps.org website.

I try to avoid this situation altogether. So when I insert "new rows". I'll
create a table with a standard number of rows. I'll name the fields in one of
the rows with a "base name", then I select the row and save it as AutoText. I
next name all the form fields in the rows with incrementing numbers ("base1,
base2, etc"). When I want a new row, I insert the AutoText entry, then rename
the formfields already in it with the next incrementing number.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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