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 / January 2006

Tip: Looking for answers? Try searching our database.

Using VBA insert table row with text form fields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sjane - 06 Jan 2006 18:16 GMT
I have a document form (protected) with a table containing three rows.  I
need to be able to run a macro that inserts a new row in the table and
inserts a text form field in each row.  The macro shown below does that,
however, only the first of the three text form fields is given a bookmark
name, i.e. Text12.  How do I get the remaining two text form fields to have a
bookmark name, i.e., Text13 and Text14?

Sub NewRow()

Dim rownum As Integer, i As Integer
ActiveDocument.Unprotect Password:="XYZ"
ActiveDocument.Tables(1).Rows.Add
rownum = ActiveDocument.Tables(1).Rows.Count
For i = 1 To ActiveDocument.Tables(1).Columns.Count
   ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,
Type:=wdFieldFormTextInput
Next i
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.FormFields(1).Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="XYZ"

End Sub

Thanks in advance for any and all assistance

sjane
Jay Freedman - 06 Jan 2006 18:31 GMT
See if this article helps:

http://word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> I have a document form (protected) with a table containing three
> rows.  I need to be able to run a macro that inserts a new row in the
[quoted text clipped - 25 lines]
>
> sjane
sjane - 06 Jan 2006 21:58 GMT
Jay - thank you for your quick response, but I do not see how the information
contained in the article you referred me to can insert a bookmark name in the
text form fields that are added by my macro (which only gives a sequential
bookmark name to the first text form field in the first column - the
remaining two when double clicked to open have a blank for bookmark name.  If
you have any other ideas, please do not hesitate to advise.

sjane

> See if this article helps:
>
[quoted text clipped - 29 lines]
> >
> > sjane
Doug Robbins - Word MVP - 06 Jan 2006 21:16 GMT
The following macro adds formfields with a bookmark name assigned to them:

Sub addrow()

'

' Macro created 02/02/03 by Doug Robbins

' To add a new row to a table containing formfields in every column

' automatically on exit from the last cell in the present last row of the
table

Dim rownum As Integer, i As Integer

ActiveDocument.Unprotect

ActiveDocument.Tables(1).Rows.Add

rownum = ActiveDocument.Tables(1).Rows.Count

For i = 1 To ActiveDocument.Tables(1).Columns.Count

   ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,
Type:=wdFieldFormTextInput

Next i

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro =
"addrow"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.FormFields(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I have a document form (protected) with a table containing three rows.  I
> need to be able to run a macro that inserts a new row in the table and
[quoted text clipped - 25 lines]
>
> sjane
sjane - 06 Jan 2006 21:46 GMT
Doug - thank you for the prompt response - unfortunately, while the macro
added the row and text form fields to each of the columns in the row, only
the first text form field in the row had a bookmark name

> The following macro adds formfields with a bookmark name assigned to them:
>
[quoted text clipped - 65 lines]
> >
> > sjane
Jean-Guy Marcil - 06 Jan 2006 22:39 GMT
sjane was telling us:
sjane nous racontait que :

> Doug - thank you for the prompt response - unfortunately, while the
> macro added the row and text form fields to each of the columns in
> the row, only the first text form field in the row had a bookmark name

I do not know what is wrong with your system, but if I run Doug's macro as
is, I get a different name for each formfield (Text3, Text4, Text5, etc.)

However, try this variation, it will name each formfield according to the
row number and column number:

Sub addrow()

Dim rownum As Integer, i As Integer
Dim myFF As FormField

ActiveDocument.Unprotect

ActiveDocument.Tables(1).Rows.Add

rownum = ActiveDocument.Tables(1).Rows.Count

For i = 1 To ActiveDocument.Tables(1).Columns.Count

   ActiveDocument.FormFields.Add _
       Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range, _
       Type:=wdFieldFormTextInput
   Set myFF = ActiveDocument.Tables(1).Cell(rownum, i).Range.FormFields(1)
   myFF.Name = "Text_" & rownum & "_" & i
   Set myFF = Nothing
Next i

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count, _
   ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1) _
   .ExitMacro = "addrow"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows _
   .Count, 1).Range.FormFields(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

sjane - 09 Jan 2006 14:22 GMT
Jean-Guy - thank you for your assistance.  Apparently my system does have a
problem which I am not certain how to address.  After running your macro, in
the bookmark list I have new bookmark names which the macro created.  
However, when you go to the specific text form field and double click on it,
the bookmark name box is blank.  Thanks again.

sjane

> sjane was telling us:
> sjane nous racontait que :
[quoted text clipped - 40 lines]
>
> End Sub
 
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.