Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-14-2016, 09:26 AM
h0mebrewer h0mebrewer is offline Problems Copy table content and reset contentControls Windows 7 64bit Problems Copy table content and reset contentControls Office 2010 64bit
Novice
Problems Copy table content and reset contentControls
 
Join Date: Dec 2016
Posts: 1
h0mebrewer is on a distinguished road
Default Problems Copy table content and reset contentControls

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
Reply With Quote
  #2  
Old 12-14-2016, 05:37 PM
macropod's Avatar
macropod macropod is offline Problems Copy table content and reset contentControls Windows 7 64bit Problems Copy table content and reset contentControls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



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
Problems Copy table content and reset contentControls copy content of a table inside email to excel sheet megatronixs Outlook 17 10-10-2014 05:20 AM
Problems Copy table content and reset contentControls 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft