Hi all, i'm not sure what i'm doing wrong here and need some help please.
I wrote a macro (AddTable) to repeat (copy and paste) the table located
above the control button. Users can repeat the table as many times as they
like. I also wrote another macro (DelTable) to delete any of the above
tables. However, i don't want users to delete the very first table (I need at
least one table to be in the form).
In order to identify the first table (so that it doesn't get deleted), i
gave a name ("Client1") to the first Text Form Field in that table. The macro
that repeats the table, contains a line that changes that name for all the
new added tables.
This way, when users want to delete any of the tables, the 'Delete Macro'
will first check the name of the first Text Field, if it is "Client1" then
the table won't be deleted.

When i run the 'AddTable' macro, I get an error message: Compile Error,
Method or data member not found. This error seems to refer to the line that
renames the form field.

Here are both my codes AddTable and DelTable. Any ideas would be much
appreciated. I'm using word 2003.
Cheers, Tendresse

Sub AddTable()
' Select the table above the control button
Do
Selection.MoveUp wdLine, 1
Loop While Selection.Tables.Count = 0
' Copy and Paste Table
Selection.Tables(1).Select
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.Paste

' Rename the first text field in the new table
Do
Selection.MoveUp wdLine, 1
Loop While Selection.Tables.Count = 0

Dim myFields As FormFields
Set myFields = Selection.Tables(1).Range.FormFields

myFields(1).Select
' the next line is where the error happens
Selection.myFields(1).Name = "CanDelete"

' clear field contents in the new table
Dim i As Integer
For i = 1 To myFields.Count
Select Case myFields(i).Type
Case wdFieldFormTextInput
myFields(i).Result = myFields(i).TextInput.Default
Case wdFieldFormCheckBox
myFields(i).CheckBox.Value = myFields(i).CheckBox.Default
Case wdFieldFormDropDown
myFields(i).DropDown.Value = myFields(i).DropDown.Default
End Select
Next i

End Sub
________________________________________
Sub DelTable()
' Select table above control button
Do
Selection.MoveUp wdLine, 1
Loop While Selection.Tables.Count = 0

Dim myFields As FormFields
Set myFields = Selection.Tables(1).Range.FormFields

myFields(1).Select
If myFields(1).Name = "Client1" Then
MsgBox ("Sorry, you cannot delete this table")
Exit Sub

Else
Selection.Tables(1).Delete
Selection.TypeBackspace
End If

End Sub

Re: Macro to Repeat a table by Greg

Greg
Mon Sep 17 20:09:47 CDT 2007

Change that line to:
myFields(1).Name = "CanDelete"

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Tendresse wrote:
> Hi all, i'm not sure what i'm doing wrong here and need some help
> please.
> I wrote a macro (AddTable) to repeat (copy and paste) the table
> located above the control button. Users can repeat the table as many
> times as they like. I also wrote another macro (DelTable) to delete
> any of the above tables. However, i don't want users to delete the
> very first table (I need at least one table to be in the form).
> In order to identify the first table (so that it doesn't get
> deleted), i gave a name ("Client1") to the first Text Form Field in
> that table. The macro that repeats the table, contains a line that
> changes that name for all the new added tables.
> This way, when users want to delete any of the tables, the 'Delete
> Macro' will first check the name of the first Text Field, if it is
> "Client1" then the table won't be deleted.
>
> When i run the 'AddTable' macro, I get an error message: Compile
> Error, Method or data member not found. This error seems to refer to
> the line that renames the form field.
>
> Here are both my codes AddTable and DelTable. Any ideas would be much
> appreciated. I'm using word 2003.
> Cheers, Tendresse
>
> Sub AddTable()
> ' Select the table above the control button
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
> ' Copy and Paste Table
> Selection.Tables(1).Select
> Selection.Copy
> Selection.MoveDown Unit:=wdLine, Count:=1
> Selection.TypeParagraph
> Selection.Paste
>
> ' Rename the first text field in the new table
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
>
> Dim myFields As FormFields
> Set myFields = Selection.Tables(1).Range.FormFields
>
> myFields(1).Select
> ' the next line is where the error happens
> Selection.myFields(1).Name = "CanDelete"
>
> ' clear field contents in the new table
> Dim i As Integer
> For i = 1 To myFields.Count
> Select Case myFields(i).Type
> Case wdFieldFormTextInput
> myFields(i).Result = myFields(i).TextInput.Default
> Case wdFieldFormCheckBox
> myFields(i).CheckBox.Value = myFields(i).CheckBox.Default
> Case wdFieldFormDropDown
> myFields(i).DropDown.Value = myFields(i).DropDown.Default
> End Select
> Next i
>
> End Sub
> ________________________________________
> Sub DelTable()
> ' Select table above control button
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
>
> Dim myFields As FormFields
> Set myFields = Selection.Tables(1).Range.FormFields
>
> myFields(1).Select
> If myFields(1).Name = "Client1" Then
> MsgBox ("Sorry, you cannot delete this table")
> Exit Sub
>
> Else
> Selection.Tables(1).Delete
> Selection.TypeBackspace
> End If
>
> End Sub



Re: Macro to Repeat a table by Greg

Greg
Mon Sep 17 20:17:56 CDT 2007

On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
wrote:
> Hi all, i'm not sure what i'm doing wrong here and need some help please.
> I wrote a macro (AddTable) to repeat (copy and paste) the table located
> above the control button. Users can repeat the table as many times as they
> like. I also wrote another macro (DelTable) to delete any of the above
> tables. However, i don't want users to delete the very first table (I need at
> least one table to be in the form).
> In order to identify the first table (so that it doesn't get deleted), i
> gave a name ("Client1") to the first Text Form Field in that table. The macro
> that repeats the table, contains a line that changes that name for all the
> new added tables.
> This way, when users want to delete any of the tables, the 'Delete Macro'
> will first check the name of the first Text Field, if it is "Client1" then
> the table won't be deleted.
>
> When i run the 'AddTable' macro, I get an error message: Compile Error,
> Method or data member not found. This error seems to refer to the line that
> renames the form field.
>
> Here are both my codes AddTable and DelTable. Any ideas would be much
> appreciated. I'm using word 2003.
> Cheers, Tendresse
>
> Sub AddTable()
> ' Select the table above the control button
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
> ' Copy and Paste Table
> Selection.Tables(1).Select
> Selection.Copy
> Selection.MoveDown Unit:=wdLine, Count:=1
> Selection.TypeParagraph
> Selection.Paste
>
> ' Rename the first text field in the new table
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
>
> Dim myFields As FormFields
> Set myFields = Selection.Tables(1).Range.FormFields
>
> myFields(1).Select
> ' the next line is where the error happens
> Selection.myFields(1).Name = "CanDelete"
>
> ' clear field contents in the new table
> Dim i As Integer
> For i = 1 To myFields.Count
> Select Case myFields(i).Type
> Case wdFieldFormTextInput
> myFields(i).Result = myFields(i).TextInput.Default
> Case wdFieldFormCheckBox
> myFields(i).CheckBox.Value = myFields(i).CheckBox.Default
> Case wdFieldFormDropDown
> myFields(i).DropDown.Value = myFields(i).DropDown.Default
> End Select
> Next i
>
> End Sub
> ________________________________________
> Sub DelTable()
> ' Select table above control button
> Do
> Selection.MoveUp wdLine, 1
> Loop While Selection.Tables.Count = 0
>
> Dim myFields As FormFields
> Set myFields = Selection.Tables(1).Range.FormFields
>
> myFields(1).Select
> If myFields(1).Name = "Client1" Then
> MsgBox ("Sorry, you cannot delete this table")
> Exit Sub
>
> Else
> Selection.Tables(1).Delete
> Selection.TypeBackspace
> End If
>
> End Sub


P.S.

Seem like you are going to a lot of pains to ID the first table. Why
not something like:

If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
MsgBox "Can't Delete"
Else
Selection.Tables(1).Delete
End If





Re: Macro to Repeat a table by Tendresse

Tendresse
Mon Sep 17 20:32:00 CDT 2007

Hi Greg, Thanks for your reply.

i'm still getting an error, but a different one this time:
Run-time error '-2147467259 (80004005)':
Method 'Name' of object 'Form Field' failed

The new table gets added correctly. However, the name of its first text form
field is still 'Client1'. which is still the same name as in the original
table.!


"Greg Maxey" wrote:

> Change that line to:
> myFields(1).Name = "CanDelete"
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> Tendresse wrote:
> > Hi all, i'm not sure what i'm doing wrong here and need some help
> > please.
> > I wrote a macro (AddTable) to repeat (copy and paste) the table
> > located above the control button. Users can repeat the table as many
> > times as they like. I also wrote another macro (DelTable) to delete
> > any of the above tables. However, i don't want users to delete the
> > very first table (I need at least one table to be in the form).
> > In order to identify the first table (so that it doesn't get
> > deleted), i gave a name ("Client1") to the first Text Form Field in
> > that table. The macro that repeats the table, contains a line that
> > changes that name for all the new added tables.
> > This way, when users want to delete any of the tables, the 'Delete
> > Macro' will first check the name of the first Text Field, if it is
> > "Client1" then the table won't be deleted.
> >
> > When i run the 'AddTable' macro, I get an error message: Compile
> > Error, Method or data member not found. This error seems to refer to
> > the line that renames the form field.
> >
> > Here are both my codes AddTable and DelTable. Any ideas would be much
> > appreciated. I'm using word 2003.
> > Cheers, Tendresse
> >
> > Sub AddTable()
> > ' Select the table above the control button
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> > ' Copy and Paste Table
> > Selection.Tables(1).Select
> > Selection.Copy
> > Selection.MoveDown Unit:=wdLine, Count:=1
> > Selection.TypeParagraph
> > Selection.Paste
> >
> > ' Rename the first text field in the new table
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> >
> > Dim myFields As FormFields
> > Set myFields = Selection.Tables(1).Range.FormFields
> >
> > myFields(1).Select
> > ' the next line is where the error happens
> > Selection.myFields(1).Name = "CanDelete"
> >
> > ' clear field contents in the new table
> > Dim i As Integer
> > For i = 1 To myFields.Count
> > Select Case myFields(i).Type
> > Case wdFieldFormTextInput
> > myFields(i).Result = myFields(i).TextInput.Default
> > Case wdFieldFormCheckBox
> > myFields(i).CheckBox.Value = myFields(i).CheckBox.Default
> > Case wdFieldFormDropDown
> > myFields(i).DropDown.Value = myFields(i).DropDown.Default
> > End Select
> > Next i
> >
> > End Sub
> > ________________________________________
> > Sub DelTable()
> > ' Select table above control button
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> >
> > Dim myFields As FormFields
> > Set myFields = Selection.Tables(1).Range.FormFields
> >
> > myFields(1).Select
> > If myFields(1).Name = "Client1" Then
> > MsgBox ("Sorry, you cannot delete this table")
> > Exit Sub
> >
> > Else
> > Selection.Tables(1).Delete
> > Selection.TypeBackspace
> > End If
> >
> > End Sub
>
>
>

Re: Macro to Repeat a table by Tendresse

Tendresse
Mon Sep 17 20:54:02 CDT 2007

Yes, I see your point. However, later on in the same form, there is another
table for products (different than that for Clients). Users can also repeat
it if they want to.

Now, that table (for products) will not necessarily be the 'second' one in
the activedocument, given that i don't know how many times the first table
(for clients) will be repeated.

That's why i wanted to find a different way to identify the first table for
clients and the first table for products by giving a name to their first
fields. You see my point?

I can't understand why it's not letting me rename the field!


"Greg Maxey" wrote:

> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
> wrote:
> > Hi all, i'm not sure what i'm doing wrong here and need some help please.
> > I wrote a macro (AddTable) to repeat (copy and paste) the table located
> > above the control button. Users can repeat the table as many times as they
> > like. I also wrote another macro (DelTable) to delete any of the above
> > tables. However, i don't want users to delete the very first table (I need at
> > least one table to be in the form).
> > In order to identify the first table (so that it doesn't get deleted), i
> > gave a name ("Client1") to the first Text Form Field in that table. The macro
> > that repeats the table, contains a line that changes that name for all the
> > new added tables.
> > This way, when users want to delete any of the tables, the 'Delete Macro'
> > will first check the name of the first Text Field, if it is "Client1" then
> > the table won't be deleted.
> >
> > When i run the 'AddTable' macro, I get an error message: Compile Error,
> > Method or data member not found. This error seems to refer to the line that
> > renames the form field.
> >
> > Here are both my codes AddTable and DelTable. Any ideas would be much
> > appreciated. I'm using word 2003.
> > Cheers, Tendresse
> >
> > Sub AddTable()
> > ' Select the table above the control button
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> > ' Copy and Paste Table
> > Selection.Tables(1).Select
> > Selection.Copy
> > Selection.MoveDown Unit:=wdLine, Count:=1
> > Selection.TypeParagraph
> > Selection.Paste
> >
> > ' Rename the first text field in the new table
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> >
> > Dim myFields As FormFields
> > Set myFields = Selection.Tables(1).Range.FormFields
> >
> > myFields(1).Select
> > ' the next line is where the error happens
> > Selection.myFields(1).Name = "CanDelete"
> >
> > ' clear field contents in the new table
> > Dim i As Integer
> > For i = 1 To myFields.Count
> > Select Case myFields(i).Type
> > Case wdFieldFormTextInput
> > myFields(i).Result = myFields(i).TextInput.Default
> > Case wdFieldFormCheckBox
> > myFields(i).CheckBox.Value = myFields(i).CheckBox.Default
> > Case wdFieldFormDropDown
> > myFields(i).DropDown.Value = myFields(i).DropDown.Default
> > End Select
> > Next i
> >
> > End Sub
> > ________________________________________
> > Sub DelTable()
> > ' Select table above control button
> > Do
> > Selection.MoveUp wdLine, 1
> > Loop While Selection.Tables.Count = 0
> >
> > Dim myFields As FormFields
> > Set myFields = Selection.Tables(1).Range.FormFields
> >
> > myFields(1).Select
> > If myFields(1).Name = "Client1" Then
> > MsgBox ("Sorry, you cannot delete this table")
> > Exit Sub
> >
> > Else
> > Selection.Tables(1).Delete
> > Selection.TypeBackspace
> > End If
> >
> > End Sub
>
>
> P.S.
>
> Seem like you are going to a lot of pains to ID the first table. Why
> not something like:
>
> If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
> MsgBox "Can't Delete"
> Else
> Selection.Tables(1).Delete
> End If
>
>
>
>
>

Re: Macro to Repeat a table by Greg

Greg
Mon Sep 17 21:50:10 CDT 2007

I can. But it took me a while and some search through old code to remember.

If you insert a formfield in a new document and run this code:

ActiveDocument.FormFields(1).Name = "Testing"

It will rename the field from text1 to "Testing" as expected.

However, if you insert a formfield in a new document, then copy and paste it
and run this code:

ActiveDocument.FormFields(2).Name = "Testing"

It will generate a run time error.

The reason is that two formfields can't share the same name. When you
copied the first Text1 field and then pasted it, Word lets the field be
pasted and function but it has no identity. You can see this if you have
bookmarks displayed because the pasted field has no bookmark boundaries.
You can also look at your list of bookmarks and see that only one is listed.

I don't know why, but you can change the name of a formfield with .Name but
apparently you can't assign an identity to a formfield that was stripped by
pasting. You can:

myFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "CanDelete"
.Execute
End With

However this isn't going to solve your larger problem as only one field can
have the name "CanDelete"

You could bookmark the first table and bookmark the product table and:

Sub DeleteTable()
Selection.Tables(1).Select
If Selection.Bookmarks(1).Name = "TableClient" Or
Selection.Bookmarks(1).Name = "TableProduct" Then
MsgBox "Can't delete"
Else
Selection.Tables(1).Delete
End If
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Tendresse wrote:
> Yes, I see your point. However, later on in the same form, there is
> another table for products (different than that for Clients). Users
> can also repeat it if they want to.
>
> Now, that table (for products) will not necessarily be the 'second'
> one in the activedocument, given that i don't know how many times the
> first table (for clients) will be repeated.
>
> That's why i wanted to find a different way to identify the first
> table for clients and the first table for products by giving a name
> to their first fields. You see my point?
>
> I can't understand why it's not letting me rename the field!
>
>
> "Greg Maxey" wrote:
>
>> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
>> wrote:
>>> Hi all, i'm not sure what i'm doing wrong here and need some help
>>> please.
>>> I wrote a macro (AddTable) to repeat (copy and paste) the table
>>> located above the control button. Users can repeat the table as
>>> many times as they like. I also wrote another macro (DelTable) to
>>> delete any of the above tables. However, i don't want users to
>>> delete the very first table (I need at least one table to be in the
>>> form).
>>> In order to identify the first table (so that it doesn't get
>>> deleted), i gave a name ("Client1") to the first Text Form Field in
>>> that table. The macro that repeats the table, contains a line that
>>> changes that name for all the new added tables.
>>> This way, when users want to delete any of the tables, the 'Delete
>>> Macro' will first check the name of the first Text Field, if it is
>>> "Client1" then the table won't be deleted.
>>>
>>> When i run the 'AddTable' macro, I get an error message: Compile
>>> Error, Method or data member not found. This error seems to refer
>>> to the line that renames the form field.
>>>
>>> Here are both my codes AddTable and DelTable. Any ideas would be
>>> much appreciated. I'm using word 2003.
>>> Cheers, Tendresse
>>>
>>> Sub AddTable()
>>> ' Select the table above the control button
>>> Do
>>> Selection.MoveUp wdLine, 1
>>> Loop While Selection.Tables.Count = 0
>>> ' Copy and Paste Table
>>> Selection.Tables(1).Select
>>> Selection.Copy
>>> Selection.MoveDown Unit:=wdLine, Count:=1
>>> Selection.TypeParagraph
>>> Selection.Paste
>>>
>>> ' Rename the first text field in the new table
>>> Do
>>> Selection.MoveUp wdLine, 1
>>> Loop While Selection.Tables.Count = 0
>>>
>>> Dim myFields As FormFields
>>> Set myFields = Selection.Tables(1).Range.FormFields
>>>
>>> myFields(1).Select
>>> ' the next line is where the error happens
>>> Selection.myFields(1).Name = "CanDelete"
>>>
>>> ' clear field contents in the new table
>>> Dim i As Integer
>>> For i = 1 To myFields.Count
>>> Select Case myFields(i).Type
>>> Case wdFieldFormTextInput
>>> myFields(i).Result = myFields(i).TextInput.Default
>>> Case wdFieldFormCheckBox
>>> myFields(i).CheckBox.Value =
>>> myFields(i).CheckBox.Default Case wdFieldFormDropDown
>>> myFields(i).DropDown.Value =
>>> myFields(i).DropDown.Default End Select
>>> Next i
>>>
>>> End Sub
>>> ________________________________________
>>> Sub DelTable()
>>> ' Select table above control button
>>> Do
>>> Selection.MoveUp wdLine, 1
>>> Loop While Selection.Tables.Count = 0
>>>
>>> Dim myFields As FormFields
>>> Set myFields = Selection.Tables(1).Range.FormFields
>>>
>>> myFields(1).Select
>>> If myFields(1).Name = "Client1" Then
>>> MsgBox ("Sorry, you cannot delete this table")
>>> Exit Sub
>>>
>>> Else
>>> Selection.Tables(1).Delete
>>> Selection.TypeBackspace
>>> End If
>>>
>>> End Sub
>>
>>
>> P.S.
>>
>> Seem like you are going to a lot of pains to ID the first table. Why
>> not something like:
>>
>> If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
>> MsgBox "Can't Delete"
>> Else
>> Selection.Tables(1).Delete
>> End If



Re: Macro to Repeat a table by Tendresse

Tendresse
Tue Sep 18 18:52:04 CDT 2007

Thanks a lot, Greg. This got me puzzled until I got your reply.

I still need a little bit of your generous help. I don't know how to
'bookmark a table'. If i put the insertion point in the first cell of the
table (the heading cell) and click Insert > Bookmark. Would this bookmark the
entire table?

and when the table is copied and pasted, what happens to this bookmark then?
Doesn't it get copied and pasted as well?!

Or do you mean to insert a bookmark right above the first table (rather than
INSIDE the table)?

Looking forward to your reply.
Thanks again. Tendresse

"Greg Maxey" wrote:

> I can. But it took me a while and some search through old code to remember.
>
> If you insert a formfield in a new document and run this code:
>
> ActiveDocument.FormFields(1).Name = "Testing"
>
> It will rename the field from text1 to "Testing" as expected.
>
> However, if you insert a formfield in a new document, then copy and paste it
> and run this code:
>
> ActiveDocument.FormFields(2).Name = "Testing"
>
> It will generate a run time error.
>
> The reason is that two formfields can't share the same name. When you
> copied the first Text1 field and then pasted it, Word lets the field be
> pasted and function but it has no identity. You can see this if you have
> bookmarks displayed because the pasted field has no bookmark boundaries.
> You can also look at your list of bookmarks and see that only one is listed.
>
> I don't know why, but you can change the name of a formfield with .Name but
> apparently you can't assign an identity to a formfield that was stripped by
> pasting. You can:
>
> myFields(1).Select
> With Dialogs(wdDialogFormFieldOptions)
> .Name = "CanDelete"
> .Execute
> End With
>
> However this isn't going to solve your larger problem as only one field can
> have the name "CanDelete"
>
> You could bookmark the first table and bookmark the product table and:
>
> Sub DeleteTable()
> Selection.Tables(1).Select
> If Selection.Bookmarks(1).Name = "TableClient" Or
> Selection.Bookmarks(1).Name = "TableProduct" Then
> MsgBox "Can't delete"
> Else
> Selection.Tables(1).Delete
> End If
> End Sub
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> Tendresse wrote:
> > Yes, I see your point. However, later on in the same form, there is
> > another table for products (different than that for Clients). Users
> > can also repeat it if they want to.
> >
> > Now, that table (for products) will not necessarily be the 'second'
> > one in the activedocument, given that i don't know how many times the
> > first table (for clients) will be repeated.
> >
> > That's why i wanted to find a different way to identify the first
> > table for clients and the first table for products by giving a name
> > to their first fields. You see my point?
> >
> > I can't understand why it's not letting me rename the field!
> >
> >
> > "Greg Maxey" wrote:
> >
> >> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
> >> wrote:
> >>> Hi all, i'm not sure what i'm doing wrong here and need some help
> >>> please.
> >>> I wrote a macro (AddTable) to repeat (copy and paste) the table
> >>> located above the control button. Users can repeat the table as
> >>> many times as they like. I also wrote another macro (DelTable) to
> >>> delete any of the above tables. However, i don't want users to
> >>> delete the very first table (I need at least one table to be in the
> >>> form).
> >>> In order to identify the first table (so that it doesn't get
> >>> deleted), i gave a name ("Client1") to the first Text Form Field in
> >>> that table. The macro that repeats the table, contains a line that
> >>> changes that name for all the new added tables.
> >>> This way, when users want to delete any of the tables, the 'Delete
> >>> Macro' will first check the name of the first Text Field, if it is
> >>> "Client1" then the table won't be deleted.
> >>>
> >>> When i run the 'AddTable' macro, I get an error message: Compile
> >>> Error, Method or data member not found. This error seems to refer
> >>> to the line that renames the form field.
> >>>
> >>> Here are both my codes AddTable and DelTable. Any ideas would be
> >>> much appreciated. I'm using word 2003.
> >>> Cheers, Tendresse
> >>>
> >>> Sub AddTable()
> >>> ' Select the table above the control button
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>> ' Copy and Paste Table
> >>> Selection.Tables(1).Select
> >>> Selection.Copy
> >>> Selection.MoveDown Unit:=wdLine, Count:=1
> >>> Selection.TypeParagraph
> >>> Selection.Paste
> >>>
> >>> ' Rename the first text field in the new table
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>>
> >>> Dim myFields As FormFields
> >>> Set myFields = Selection.Tables(1).Range.FormFields
> >>>
> >>> myFields(1).Select
> >>> ' the next line is where the error happens
> >>> Selection.myFields(1).Name = "CanDelete"
> >>>
> >>> ' clear field contents in the new table
> >>> Dim i As Integer
> >>> For i = 1 To myFields.Count
> >>> Select Case myFields(i).Type
> >>> Case wdFieldFormTextInput
> >>> myFields(i).Result = myFields(i).TextInput.Default
> >>> Case wdFieldFormCheckBox
> >>> myFields(i).CheckBox.Value =
> >>> myFields(i).CheckBox.Default Case wdFieldFormDropDown
> >>> myFields(i).DropDown.Value =
> >>> myFields(i).DropDown.Default End Select
> >>> Next i
> >>>
> >>> End Sub
> >>> ________________________________________
> >>> Sub DelTable()
> >>> ' Select table above control button
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>>
> >>> Dim myFields As FormFields
> >>> Set myFields = Selection.Tables(1).Range.FormFields
> >>>
> >>> myFields(1).Select
> >>> If myFields(1).Name = "Client1" Then
> >>> MsgBox ("Sorry, you cannot delete this table")
> >>> Exit Sub
> >>>
> >>> Else
> >>> Selection.Tables(1).Delete
> >>> Selection.TypeBackspace
> >>> End If
> >>>
> >>> End Sub
> >>
> >>
> >> P.S.
> >>
> >> Seem like you are going to a lot of pains to ID the first table. Why
> >> not something like:
> >>
> >> If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
> >> MsgBox "Can't Delete"
> >> Else
> >> Selection.Tables(1).Delete
> >> End If
>
>
>

Re: Macro to Repeat a table by Tendresse

Tendresse
Tue Sep 18 19:18:01 CDT 2007

Hey Greg, I tried another idea and would like to know your opinion. In the
AddTable macro i added the following:

' after the table is copied and pasted

myFields(1).Select
Selection.Delete
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput

' rest of the code

This way, i'm putting a whole new field for every new table and Word will
automatically give it a default name (Text#) and only the first field in the
first table will be called (Client1).

It seems to be working fine. But just in case you can see something wrong
there that i can't see.

Thank u. Tendresse


"Greg Maxey" wrote:

> I can. But it took me a while and some search through old code to remember.
>
> If you insert a formfield in a new document and run this code:
>
> ActiveDocument.FormFields(1).Name = "Testing"
>
> It will rename the field from text1 to "Testing" as expected.
>
> However, if you insert a formfield in a new document, then copy and paste it
> and run this code:
>
> ActiveDocument.FormFields(2).Name = "Testing"
>
> It will generate a run time error.
>
> The reason is that two formfields can't share the same name. When you
> copied the first Text1 field and then pasted it, Word lets the field be
> pasted and function but it has no identity. You can see this if you have
> bookmarks displayed because the pasted field has no bookmark boundaries.
> You can also look at your list of bookmarks and see that only one is listed.
>
> I don't know why, but you can change the name of a formfield with .Name but
> apparently you can't assign an identity to a formfield that was stripped by
> pasting. You can:
>
> myFields(1).Select
> With Dialogs(wdDialogFormFieldOptions)
> .Name = "CanDelete"
> .Execute
> End With
>
> However this isn't going to solve your larger problem as only one field can
> have the name "CanDelete"
>
> You could bookmark the first table and bookmark the product table and:
>
> Sub DeleteTable()
> Selection.Tables(1).Select
> If Selection.Bookmarks(1).Name = "TableClient" Or
> Selection.Bookmarks(1).Name = "TableProduct" Then
> MsgBox "Can't delete"
> Else
> Selection.Tables(1).Delete
> End If
> End Sub
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> Tendresse wrote:
> > Yes, I see your point. However, later on in the same form, there is
> > another table for products (different than that for Clients). Users
> > can also repeat it if they want to.
> >
> > Now, that table (for products) will not necessarily be the 'second'
> > one in the activedocument, given that i don't know how many times the
> > first table (for clients) will be repeated.
> >
> > That's why i wanted to find a different way to identify the first
> > table for clients and the first table for products by giving a name
> > to their first fields. You see my point?
> >
> > I can't understand why it's not letting me rename the field!
> >
> >
> > "Greg Maxey" wrote:
> >
> >> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
> >> wrote:
> >>> Hi all, i'm not sure what i'm doing wrong here and need some help
> >>> please.
> >>> I wrote a macro (AddTable) to repeat (copy and paste) the table
> >>> located above the control button. Users can repeat the table as
> >>> many times as they like. I also wrote another macro (DelTable) to
> >>> delete any of the above tables. However, i don't want users to
> >>> delete the very first table (I need at least one table to be in the
> >>> form).
> >>> In order to identify the first table (so that it doesn't get
> >>> deleted), i gave a name ("Client1") to the first Text Form Field in
> >>> that table. The macro that repeats the table, contains a line that
> >>> changes that name for all the new added tables.
> >>> This way, when users want to delete any of the tables, the 'Delete
> >>> Macro' will first check the name of the first Text Field, if it is
> >>> "Client1" then the table won't be deleted.
> >>>
> >>> When i run the 'AddTable' macro, I get an error message: Compile
> >>> Error, Method or data member not found. This error seems to refer
> >>> to the line that renames the form field.
> >>>
> >>> Here are both my codes AddTable and DelTable. Any ideas would be
> >>> much appreciated. I'm using word 2003.
> >>> Cheers, Tendresse
> >>>
> >>> Sub AddTable()
> >>> ' Select the table above the control button
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>> ' Copy and Paste Table
> >>> Selection.Tables(1).Select
> >>> Selection.Copy
> >>> Selection.MoveDown Unit:=wdLine, Count:=1
> >>> Selection.TypeParagraph
> >>> Selection.Paste
> >>>
> >>> ' Rename the first text field in the new table
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>>
> >>> Dim myFields As FormFields
> >>> Set myFields = Selection.Tables(1).Range.FormFields
> >>>
> >>> myFields(1).Select
> >>> ' the next line is where the error happens
> >>> Selection.myFields(1).Name = "CanDelete"
> >>>
> >>> ' clear field contents in the new table
> >>> Dim i As Integer
> >>> For i = 1 To myFields.Count
> >>> Select Case myFields(i).Type
> >>> Case wdFieldFormTextInput
> >>> myFields(i).Result = myFields(i).TextInput.Default
> >>> Case wdFieldFormCheckBox
> >>> myFields(i).CheckBox.Value =
> >>> myFields(i).CheckBox.Default Case wdFieldFormDropDown
> >>> myFields(i).DropDown.Value =
> >>> myFields(i).DropDown.Default End Select
> >>> Next i
> >>>
> >>> End Sub
> >>> ________________________________________
> >>> Sub DelTable()
> >>> ' Select table above control button
> >>> Do
> >>> Selection.MoveUp wdLine, 1
> >>> Loop While Selection.Tables.Count = 0
> >>>
> >>> Dim myFields As FormFields
> >>> Set myFields = Selection.Tables(1).Range.FormFields
> >>>
> >>> myFields(1).Select
> >>> If myFields(1).Name = "Client1" Then
> >>> MsgBox ("Sorry, you cannot delete this table")
> >>> Exit Sub
> >>>
> >>> Else
> >>> Selection.Tables(1).Delete
> >>> Selection.TypeBackspace
> >>> End If
> >>>
> >>> End Sub
> >>
> >>
> >> P.S.
> >>
> >> Seem like you are going to a lot of pains to ID the first table. Why
> >> not something like:
> >>
> >> If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
> >> MsgBox "Can't Delete"
> >> Else
> >> Selection.Tables(1).Delete
> >> End If
>
>
>

Re: Macro to Repeat a table by Greg

Greg
Tue Sep 18 21:14:20 CDT 2007

To bookmark a table you just select the entire table and use the menu
Insert>Bookmark.

When the table is copied and pasted the bookmark stays with the original
table.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Tendresse" <Tendresse@discussions.microsoft.com> wrote in message
news:7133EACA-C7CD-4679-A1F3-D9AECB6E1F5F@microsoft.com...
> Thanks a lot, Greg. This got me puzzled until I got your reply.
>
> I still need a little bit of your generous help. I don't know how to
> 'bookmark a table'. If i put the insertion point in the first cell of the
> table (the heading cell) and click Insert > Bookmark. Would this bookmark
> the
> entire table?
>
> and when the table is copied and pasted, what happens to this bookmark
> then?
> Doesn't it get copied and pasted as well?!
>
> Or do you mean to insert a bookmark right above the first table (rather
> than
> INSIDE the table)?
>
> Looking forward to your reply.
> Thanks again. Tendresse
>
> "Greg Maxey" wrote:
>
>> I can. But it took me a while and some search through old code to
>> remember.
>>
>> If you insert a formfield in a new document and run this code:
>>
>> ActiveDocument.FormFields(1).Name = "Testing"
>>
>> It will rename the field from text1 to "Testing" as expected.
>>
>> However, if you insert a formfield in a new document, then copy and paste
>> it
>> and run this code:
>>
>> ActiveDocument.FormFields(2).Name = "Testing"
>>
>> It will generate a run time error.
>>
>> The reason is that two formfields can't share the same name. When you
>> copied the first Text1 field and then pasted it, Word lets the field be
>> pasted and function but it has no identity. You can see this if you have
>> bookmarks displayed because the pasted field has no bookmark boundaries.
>> You can also look at your list of bookmarks and see that only one is
>> listed.
>>
>> I don't know why, but you can change the name of a formfield with .Name
>> but
>> apparently you can't assign an identity to a formfield that was stripped
>> by
>> pasting. You can:
>>
>> myFields(1).Select
>> With Dialogs(wdDialogFormFieldOptions)
>> .Name = "CanDelete"
>> .Execute
>> End With
>>
>> However this isn't going to solve your larger problem as only one field
>> can
>> have the name "CanDelete"
>>
>> You could bookmark the first table and bookmark the product table and:
>>
>> Sub DeleteTable()
>> Selection.Tables(1).Select
>> If Selection.Bookmarks(1).Name = "TableClient" Or
>> Selection.Bookmarks(1).Name = "TableProduct" Then
>> MsgBox "Can't delete"
>> Else
>> Selection.Tables(1).Delete
>> End If
>> End Sub
>>
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>>
>> Tendresse wrote:
>> > Yes, I see your point. However, later on in the same form, there is
>> > another table for products (different than that for Clients). Users
>> > can also repeat it if they want to.
>> >
>> > Now, that table (for products) will not necessarily be the 'second'
>> > one in the activedocument, given that i don't know how many times the
>> > first table (for clients) will be repeated.
>> >
>> > That's why i wanted to find a different way to identify the first
>> > table for clients and the first table for products by giving a name
>> > to their first fields. You see my point?
>> >
>> > I can't understand why it's not letting me rename the field!
>> >
>> >
>> > "Greg Maxey" wrote:
>> >
>> >> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
>> >> wrote:
>> >>> Hi all, i'm not sure what i'm doing wrong here and need some help
>> >>> please.
>> >>> I wrote a macro (AddTable) to repeat (copy and paste) the table
>> >>> located above the control button. Users can repeat the table as
>> >>> many times as they like. I also wrote another macro (DelTable) to
>> >>> delete any of the above tables. However, i don't want users to
>> >>> delete the very first table (I need at least one table to be in the
>> >>> form).
>> >>> In order to identify the first table (so that it doesn't get
>> >>> deleted), i gave a name ("Client1") to the first Text Form Field in
>> >>> that table. The macro that repeats the table, contains a line that
>> >>> changes that name for all the new added tables.
>> >>> This way, when users want to delete any of the tables, the 'Delete
>> >>> Macro' will first check the name of the first Text Field, if it is
>> >>> "Client1" then the table won't be deleted.
>> >>>
>> >>> When i run the 'AddTable' macro, I get an error message: Compile
>> >>> Error, Method or data member not found. This error seems to refer
>> >>> to the line that renames the form field.
>> >>>
>> >>> Here are both my codes AddTable and DelTable. Any ideas would be
>> >>> much appreciated. I'm using word 2003.
>> >>> Cheers, Tendresse
>> >>>
>> >>> Sub AddTable()
>> >>> ' Select the table above the control button
>> >>> Do
>> >>> Selection.MoveUp wdLine, 1
>> >>> Loop While Selection.Tables.Count = 0
>> >>> ' Copy and Paste Table
>> >>> Selection.Tables(1).Select
>> >>> Selection.Copy
>> >>> Selection.MoveDown Unit:=wdLine, Count:=1
>> >>> Selection.TypeParagraph
>> >>> Selection.Paste
>> >>>
>> >>> ' Rename the first text field in the new table
>> >>> Do
>> >>> Selection.MoveUp wdLine, 1
>> >>> Loop While Selection.Tables.Count = 0
>> >>>
>> >>> Dim myFields As FormFields
>> >>> Set myFields = Selection.Tables(1).Range.FormFields
>> >>>
>> >>> myFields(1).Select
>> >>> ' the next line is where the error happens
>> >>> Selection.myFields(1).Name = "CanDelete"
>> >>>
>> >>> ' clear field contents in the new table
>> >>> Dim i As Integer
>> >>> For i = 1 To myFields.Count
>> >>> Select Case myFields(i).Type
>> >>> Case wdFieldFormTextInput
>> >>> myFields(i).Result = myFields(i).TextInput.Default
>> >>> Case wdFieldFormCheckBox
>> >>> myFields(i).CheckBox.Value =
>> >>> myFields(i).CheckBox.Default Case wdFieldFormDropDown
>> >>> myFields(i).DropDown.Value =
>> >>> myFields(i).DropDown.Default End Select
>> >>> Next i
>> >>>
>> >>> End Sub
>> >>> ________________________________________
>> >>> Sub DelTable()
>> >>> ' Select table above control button
>> >>> Do
>> >>> Selection.MoveUp wdLine, 1
>> >>> Loop While Selection.Tables.Count = 0
>> >>>
>> >>> Dim myFields As FormFields
>> >>> Set myFields = Selection.Tables(1).Range.FormFields
>> >>>
>> >>> myFields(1).Select
>> >>> If myFields(1).Name = "Client1" Then
>> >>> MsgBox ("Sorry, you cannot delete this table")
>> >>> Exit Sub
>> >>>
>> >>> Else
>> >>> Selection.Tables(1).Delete
>> >>> Selection.TypeBackspace
>> >>> End If
>> >>>
>> >>> End Sub
>> >>
>> >>
>> >> P.S.
>> >>
>> >> Seem like you are going to a lot of pains to ID the first table. Why
>> >> not something like:
>> >>
>> >> If Selection.Tables(1).Range = ActiveDocument.Tables(1).Range Then
>> >> MsgBox "Can't Delete"
>> >> Else
>> >> Selection.Tables(1).Delete
>> >> End If
>>
>>
>>



Re: Macro to Repeat a table by Greg

Greg
Tue Sep 18 21:16:16 CDT 2007

Other than is seems a bit like driving a tack with a 10 lbs hammer ;-) I see
nothing wrong with your approach.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Tendresse" <Tendresse@discussions.microsoft.com> wrote in message
news:8347B4BB-3A24-4F9E-9E79-67FCE442E914@microsoft.com...
> Hey Greg, I tried another idea and would like to know your opinion. In the
> AddTable macro i added the following:
>
> ' after the table is copied and pasted
>
> myFields(1).Select
> Selection.Delete
> Selection.FormFields.Add Range:=Selection.Range,
> Type:=wdFieldFormTextInput
>
> ' rest of the code
>
> This way, i'm putting a whole new field for every new table and Word will
> automatically give it a default name (Text#) and only the first field in
> the
> first table will be called (Client1).
>
> It seems to be working fine. But just in case you can see something wrong
> there that i can't see.
>
> Thank u. Tendresse
>
>
> "Greg Maxey" wrote:
>
>> I can. But it took me a while and some search through old code to
>> remember.
>>
>> If you insert a formfield in a new document and run this code:
>>
>> ActiveDocument.FormFields(1).Name = "Testing"
>>
>> It will rename the field from text1 to "Testing" as expected.
>>
>> However, if you insert a formfield in a new document, then copy and paste
>> it
>> and run this code:
>>
>> ActiveDocument.FormFields(2).Name = "Testing"
>>
>> It will generate a run time error.
>>
>> The reason is that two formfields can't share the same name. When you
>> copied the first Text1 field and then pasted it, Word lets the field be
>> pasted and function but it has no identity. You can see this if you have
>> bookmarks displayed because the pasted field has no bookmark boundaries.
>> You can also look at your list of bookmarks and see that only one is
>> listed.
>>
>> I don't know why, but you can change the name of a formfield with .Name
>> but
>> apparently you can't assign an identity to a formfield that was stripped
>> by
>> pasting. You can:
>>
>> myFields(1).Select
>> With Dialogs(wdDialogFormFieldOptions)
>> .Name = "CanDelete"
>> .Execute
>> End With
>>
>> However this isn't going to solve your larger problem as only one field
>> can
>> have the name "CanDelete"
>>
>> You could bookmark the first table and bookmark the product table and:
>>
>> Sub DeleteTable()
>> Selection.Tables(1).Select
>> If Selection.Bookmarks(1).Name = "TableClient" Or
>> Selection.Bookmarks(1).Name = "TableProduct" Then
>> MsgBox "Can't delete"
>> Else
>> Selection.Tables(1).Delete
>> End If
>> End Sub
>>
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>>
>> Tendresse wrote:
>> > Yes, I see your point. However, later on in the same form, there is
>> > another table for products (different than that for Clients). Users
>> > can also repeat it if they want to.
>> >
>> > Now, that table (for products) will not necessarily be the 'second'
>> > one in the activedocument, given that i don't know how many times the
>> > first table (for clients) will be repeated.
>> >
>> > That's why i wanted to find a different way to identify the first
>> > table for clients and the first table for products by giving a name
>> > to their first fields. You see my point?
>> >
>> > I can't understand why it's not letting me rename the field!
>> >
>> >
>> > "Greg Maxey" wrote:
>> >
>> >> On Sep 17, 8:26 pm, Tendresse <Tendre...@discussions.microsoft.com>
>> >> wrote:
>> >>> Hi all, i'm not sure what i'm doing wrong here and need some help
>> >>> please.
>> >>> I wrote a macro (AddTable) to repeat (copy and paste) the table
>> >>> located above the control button. Users can repeat the table as
>> >>> many times as they like. I also wrote another macro (DelTable) to
>> >>> delete any of the above tables. However, i don't want users to
>> >>> delete the very first table (I need at least one table to be in the
>> >>> form).
>> >>> In order to identify the first table (so that it doesn't get
>> >>> deleted), i gave a name ("Client1") to the first Text Form Field in
>> >>> that table. The macro that repeats the table, contains a line that
>> >>> changes that name for all the new added tables.
>> >>> This way, when users want to delete any of the tables, the 'Delete
>> >>> Macro' will first check the name of the first Text Field, if it is
>> >>> "Client1" then the table won't be deleted.
>> >>>
>> >>> When i run the 'AddTable' macro, I get an error message: Compile
>> >>> Error, Method or data member not found. This error seems to refer
>> >>> to the line that renames the form field.
>> >>>
>> >>> Here are both my codes AddTable and DelTable. Any ideas would be
>> >>> much appreciated. I'm using word 2003.
>> >>> Cheers, Tendresse
>> >>>
>> >>> Sub AddTable()
>> >>> ' Select the table above the control button
>> >>> Do
>> >>> Selection.MoveUp wdLine, 1
>> >>> Loop While Selection.Tables.Count = 0
>> >>> ' Copy and Paste Table
>> >>> Selection.Tables(1).Select
>> >>> Selection.Copy
>> >>> Selection.MoveDown Unit:=wdLine, Count:=1
>> >>> Selection.TypeParagraph
>> >>> Selection.Paste
>> >>>
>> >>> ' Rename the first text field in the new table
>> >>> Do
>> >>> Selection.MoveUp wdLine, 1
>> >>> Loop While Selection.Tables.Count = 0
>> >>>
>> >>> Dim myFields As FormFields
>