![]() |
|
|
|
#1
|
|||
|
|||
|
Hello,
I'm glad that I found this discussion board and I hope that someone could help me. I've some trouble to get my VBA Script working. My document is used as a form to get some user input. It has some tables, each of them has a button, which selects the content of the table, copy the content to the end of the table where the button was pressed. The table contains contentcontrols, to get the user input. I have two problems. 1. If I press my copy button, sometimes the table will be copied several times. 2. I would like to remove text, checkbox und date values form the fresh copy at the end of the table. Here is the code of my Macro: Code:
Sub copyTableStructure(tblNr As Long, startRow As Integer, endRow As Integer, endColumn As Integer, rowsToInsert As Integer, maxCopies As Integer)
Dim dataRange As Range
Dim lastRow As Range
Dim newDataRange As Range
Set oDoc = ActiveDocument
If (oDoc.ProtectionType <> wdNoProtection) Then
oDoc.Unprotect Password:="xxx"
End If
With ActiveDocument
Set dataRange = .Range(Start:=.Tables(tblNr).Cell(startRow, 1).Range.Start, End:=.Tables(tblNr).Cell(endRow, endColumn).Range.End)
dataRange.Select
Selection.Copy
End With
If (Selection.Rows.Count <= (maxCopies * (endRow - startRow))) Then
With ActiveDocument
Tables(tblNr).Select
Set lastRow = .Range(Start:=.Tables(tblNr).Cell(Selection.Rows.Count, 1).Range.Start, End:=.Tables(tblNr).Cell(Selection.Rows.Count, endColumn).Range.End)
lastRow.Select
Selection.InsertRowsBelow (rowsToInsert)
End With
With ActiveDocument
Tables(tblNr).Select
Set newDataRange = .Range(Start:=.Tables(tblNr).Cell(Selection.Rows.Count - rowsToInsert + 1, 1).Range.Start, End:=.Tables(tblNr).Cell(Selection.Rows.Count, endColumn).Range.End)
newDataRange.Select
Selection.Paste
newDataRange.Select
Dim oFF As FormField
Dim contentCtrl As contentControl
For Each contentCtrl In Selection.Range.contentControls
If contentCtrl.Type = wdContentControlCheckBox Then
contentCtrl.Checked = False
End If
If contentCtrl.Type = wdContentControlText Then
contentCtrl.Range.Text = ""
End If
Next contentCtrl
For Each oFF In Selection.Range.FormFields
oFF.Result = vbNullString
If oFF.Type = wdFieldFormCheckBox Then
oFF.Result = vbFalse
End If
If oFF.Type = wdFieldFormTextInput Then
oFF.TextInput.Clear
End If
Next oFF
Selection.Fields.Update
End With
Else
Dim msg As String
msg = "Not possible to add any other block"
MsgBox (msg)
End If
If (oDoc.ProtectionType = wdNoProtection) Then
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="xxx"
End If
End Sub
Private Sub schoolBtn_Click()
Call copyTableStructure(3, 2, 6, 2, 5, 99)
NoAbschlussBtn.Value = False
End Sub
Private Sub WorkBtn_Click()
Call copyTableStructure(5, 2, 8, 2, 7, 99)
NoAbschlussBtn.Value = False
End Sub
Private Sub UniveristyBtn_Click()
Call copyTableStructure(9, 2, 5, 2, 4, 99)
PraktikaBtn.Value = False
End Sub
I hope that someone could help me. Best regards H0mebrewer |
|
#2
|
||||
|
||||
|
Your problem description and code suggests you're using a mix of ActiveX controls, content controls and formfields. You really should only use one kind of control. Content controls and formfields, especially, weren't designed to work together and you can run into problems when doing so. ActiveX controls and content controls don't work on Macs, so that's no good if the document needs to be used on a Mac.
As for the problem of the table being copied several times, I note that your copyTableStructure macro includes a maxCopies value and that your calling subs each supply something other than 1 (99, actually ...). For code to conditionally add rows to a table, clearing & resetting content controls in the new row, see: https://www.msofficeforums.com/word-...html#post87989 https://www.msofficeforums.com/word-...html#post38461
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ActiveDocument.ContentControls(ID) not working | Tejas.T | Word VBA | 3 | 03-09-2015 06:50 PM |
| Using "reset" jumps content from the footnote to the headline every time | WasThatNecessary | PowerPoint | 0 | 10-24-2014 04:49 AM |
copy content of a table inside email to excel sheet
|
megatronixs | Outlook | 17 | 10-10-2014 05:20 AM |
Copy table content between documents
|
mcb09 | Word VBA | 14 | 06-12-2014 09:33 PM |
| Copy content control entries to next table next page | Mel_Herndon | Word VBA | 2 | 05-22-2014 05:07 PM |