A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

'addrow' VB macro error: requested member of collection does not e



 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old January 7th, 2008, 05:03 PM posted to microsoft.public.word.tables
RDS
external usenet poster
 
Posts: 15
Default 'addrow' VB macro error: requested member of collection does not e

I am using a VB macro in Word 2003 to allow users to add rows to multiple
tables in a protected form while copying the existing form fields in the new
row. I used code posted to another site from a Word MVP (thank you Jay
Freedman!) as I am completely new to VB. I suddenly started getting an error
message when trying to add new rows to any table in the document: "The
requested member of the collection does not exist".

Is the cause for the error that some of my tables have merged cells? Is
there a way to revise the code to allow merged cells (as I can't work around
them)? Is there any general revisions to ensure users can avoid the error
message?


Sub addrow()
Dim response As Integer
Dim myRow As Long
Dim myCount As Long
Dim colCount As Long
Dim colIndex As Long
Dim myNewField As String

If Not Selection.Information(wdWithInTable) Then
Exit Sub
End If

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

colCount = Selection.Tables(1).Columns.Count

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

For colIndex = 1 To colCount - 1
Selection.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
..Name = "text" & colIndex & "row" & myRow
..Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Next colIndex

Selection.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
..Name = "text" & colCount & "row" & 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
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:48 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.