Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA code problem with Copying and pasting table row in wrong table 1

Status
Not open for further replies.

YACHTIE

Technical User
Feb 17, 2004
77
CA
This is really continuation from "thread707-926686".
The code I am using now sort of works but still pastes in the wrong location and have not been able to correct that.
Also it erases the bookmarks from the actual Word document and therefore only works once and not indefintely.
And last but not least then in the third table that I do need to add another row to, it runs into a "vertically merged table" and chokes (runtime error 5991).
I have two command buttons in the document that activates and run the VBA code separately for each user initiated function.
Does it make any difference whether or not these macros are in "this Document" or as separate modules, ie Module1 etc.?
Could really use some help with this as I am unable to get it working (am too VBA challenged I guess)
Here is my present code:
Code:
Sub NewRows()
Dim mTable As Table
Dim r As Range
Dim myFields(2) As String
Dim i As Integer
ActiveDocument.Unprotect Password:="MES3052D"
' these are the three formfield names
  myFields(0) = "DocAffText32"
For Each mTable In ActiveDocument.Tables
' go to bookmark, just to make sure
' in the right table, then collapse
    Selection.GoTo what:=wdGoToBookmark, Name:=myFields(0)
    Selection.Collapse Direction:=wdCollapseStart
        
' set range object as last row in table
' make the Selection the range end
    Set r = ActiveDocument.Tables(i + 1).Rows(ActiveDocument.Tables(i + 1).Rows.Count).Range
    Selection.Start = r.End
'  copy range and paste it
    r.Copy
    r.Paste
'  release range object
        Set r = Nothing
        i = i + 1
Next
ActiveDocument.Protect (wdAllowOnlyFormFields), Password:="MES3052D"
End Sub
thanks for your help in advance
 
This one again.

OK.

1. myFields is an array of three (3) - but as it is 0 based it shows as myFields(2). If you only are going to use ONE formfield, there is no need to have an array. In your original posts it seemed there were formfields in each table. However, it now seems there is only one formfield in one table.

If there is only one table to deal with, then there is no need for the code "For Each table...." is there? Would you PLEASE clarify precisely the structure of this document?

You appeared to have multiple tables. I get that. Is there just ONE table that has the formfield whose row you want to copy? Or are there DIFFERENT tables, with DIFFERENT formfields to deal with? You have to make all this clear.

In the code you post above, you are only dealing with one formfield (and by implication only one table), yet the code still tries to deal with multiple tables.

2. You have a vertically merged column. Oh-oh. You are really up the creek with that one.



Gerry
 
Yes I will deal with one formfield in one table at a time to simplify things. (I have to do two different formfields in different tables on the same document but can copy the basic working code to do that simply)and after all they will be different separate macros activated by independent buttons anyway.

2. You have a vertically merged column. Oh-oh. You are really up the creek with that one.


thanks
 
Not bad use of formfields, but if you want to be able to get better information OUT, you should explicitly name all the fields.

NOTE: when you copy a range which includes a formfield, the pasted range DOES include a copy of the formfield, BUT the original formfield loses its name. the name is moved over to the copuied formfield. this leaves an unnamed formfield. This may or may not be an issue.

Gerry
 
Thanks Gerry for clearing that up, that would explain why I was losing the bookmark, however this was not the case with my previous "clumsy" code just an interesting observation from my perspective on how two different methods of code basically copying and pasting produce such different result.
Any again thanks for your help it appears thus far that your code works both for Office 2000 and XP.
THX again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top