Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2006

Tip: Looking for answers? Try searching our database.

confused deciphering this code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Angyl - 09 May 2006 23:43 GMT
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?
Cindy M  -WordMVP- - 10 May 2006 15:15 GMT
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
[quoted text clipped - 51 lines]
> 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 :-)
Angyl - 10 May 2006 15:43 GMT
Makes perfect sense, Cindy, Thanks!

> Hi =?Utf-8?B?QW5neWw=?=,
>
[quoted text clipped - 76 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.