PDA

View Full Version : Copying, then Deleting Table Contents



rbh123456789
02-23-2012, 12:01 PM
Using Word 2003

Hey everyone,

-I have a table with 2 columns and 5 rows
-There is 1 command button to copy the ENTIRE table, and another button to delete the contents of column 2 (beginning with row 2)
-Using the Protect Document feature, i have applied Editing Restrictions to the document (No Changes - read only), and added Exceptions for rows 2, 3, 4, 5 or column 2, which can be edited
-Users are ONLY able to populate the cells in column 2, rows 2 through 5

Below is my VBA code. The copy1 works fine, but i can't get the clear1 to perform the above.

Any help would be appreciated.


Private Sub copy1_Click()
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, _
Count:=1, Name:=""
Selection.MoveRight Unit:=wdCharacter, Count:=11, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=12, Extend:=wdExtend
Selection.Copy
End Sub

Private Sub clear1_Click()
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, _
Count:=1, Name:=""
Selection.MoveRight Unit:=wdCharacter, Count:=14
Selection.MoveDown Unit:=wdLine, Count:=12, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

macropod
02-23-2012, 11:09 PM
Hi RBH,

Try:
Private Sub copy1_Click()
With ActiveDocument
.Range(.Tables(1).Cell(2, 2).Range.Start, .Tables(1).Cell(5, 2).Range.End).Copy
End With
End Sub
Private Sub clear1_Click()
With ActiveDocument
.Range(.Tables(1).Cell(2, 2).Range.Start, .Tables(1).Cell(5, 2).Range.End).Delete
End With
End Sub
PS: Please use the VBA tags when pasting code.

rbh123456789
02-24-2012, 06:23 AM
macropod, THANK YOU!

it works perfectly; awesome work!

rbh123456789
02-24-2012, 09:30 PM
Hi RBH,

Try:
Private Sub copy1_Click()
With ActiveDocument
.Range(.Tables(1).Cell(2, 2).Range.Start, .Tables(1).Cell(5, 2).Range.End).Copy
End With
End Sub
Private Sub clear1_Click()
With ActiveDocument
.Range(.Tables(1).Cell(2, 2).Range.Start, .Tables(1).Cell(5, 2).Range.End).Delete
End With
End Sub
PS: Please use the VBA tags when pasting code.

One last question. I have some users using Word 2002 (not sure which SP).
2002 does not have the same features as 2003 for protecting the document.

Does anyone have any suggestions?

macropod
02-24-2012, 11:04 PM
Perhaps you should consider having the entire document protected for forms, with formfields for the permitted content in the second column. It's quite easy to populate/copy/clear formfield contents with code.

macropod
02-24-2012, 11:34 PM
-I have a table with 2 columns and 5 rows
-There is 1 command button to copy the ENTIRE table, and another button to delete the contents of column 2 (beginning with row 2)
-Using the Protect Document feature, i have applied Editing Restrictions to the document (No Changes - read only), and added Exceptions for rows 2, 3, 4, 5 or column 2, which can be edited
-Users are ONLY able to populate the cells in column 2, rows 2 through 5

Below is my VBA code. The copy1 works fine, but i can't get the clear1 to perform the above.
Cross-posted at: http://www.excelforum.com/word-formatting-and-general/816411-copy-then-delete-table-contents.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184