Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2021, 12:19 PM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default Word 2019 will not properly run macro

I have a document created in Word 2016. When I open and run the macro with Word 2019, the macro returns an error for Selection.paste. There is a 'button' that runs this macro on a police duty report. The macro copies and then adds a new entry form for another event if needed. Any help appreciated. The following is the VBA:

Private Sub CommandButton1_Click()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
With ActiveDocument
.Tables(2).Range.Copy
.Range.Select
Selection.Collapse wdCollapseEnd
Selection.Paste

Dim oRng As Range
Dim oFld As FormFields
Dim i As Long
Set oRng = ActiveDocument.Tables(2).Range
Set oFld = oRng.FormFields
For i = 1 To oFld.Count
oFld(i).Select
If oFld(i).Type = wdFieldFormDropDown Then


oFld(i).Result = oFld(i).DropDown.ListEntries(1).Name
Else
oFld(i).Result = ""
End If
Next
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
Private Sub OptionButton4_Click()

End Sub
Private Sub OptionButton6_Click()

End Sub
Reply With Quote
  #2  
Old 06-21-2021, 08:25 PM
gmayor's Avatar
gmayor gmayor is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your code works in Word 2019, assuming there are two tables in the document. It adds a copy of the table to the bottom of table 2. You can do it without the paste if you wish e.g.


Code:
Private Sub CommandButton1_Click()
Dim oRng As Range
Dim oFld As FormFields
Dim i As Long
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    With ActiveDocument
        Set oRng = ActiveDocument.Tables(2).Range
        oRng.Collapse wdCollapseEnd
        oRng.FormattedText = ActiveDocument.Tables(2).Range.FormattedText

        Set oFld = oRng.FormFields
        For i = 1 To oFld.Count
            oFld(i).Select
            If oFld(i).Type = wdFieldFormDropDown Then
                oFld(i).Result = oFld(i).DropDown.ListEntries(1).Name
            Else
                oFld(i).Result = ""
            End If
        Next
    End With
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 06-22-2021, 03:05 AM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

I very much appreciate your answer. Your code works, EXCEPT; instead of adding 1 more table at a time, it adds all existing tables plus 1. In other words, if you add 1 more table, the next click of the add button copies the 2 that are there and adds 1 more. So now there is 5 tables instead of just 2.
Reply With Quote
  #4  
Old 06-22-2021, 08:53 PM
gmayor's Avatar
gmayor gmayor is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Can you post a copy of your document so that we can see what it is that you are doing. The code I posted should (and does here) do exactly the same as the code you posted. It adds a copy of table 2 to the bottom of table 2. From your further comment, I suspect you want something different.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 06-23-2021, 02:45 AM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

I need to request permission from the Sheriff to post the actual document. I will get back to you and appreciate the follow up.
Reply With Quote
  #6  
Old 06-23-2021, 05:46 PM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

The document has a form for data entry. When you click on the button, it adds one more table for an additional entry. What is happening is when you hit the button again, instead of adding just one more table, it copies all the existing tables and adds one more. So after you have 2 table entries and hit the button to add another one, it now adds the 2 existing tables plus one more making it 5 tables instead of 3. The VBA is apparently copying all existing tables as you add them instead of just adding one more each click. Note though that if you run the VBA in Word 2016 or lower, it works fine. I am using Word 2019.
Reply With Quote
  #7  
Old 06-23-2021, 07:32 PM
Guessed's Avatar
Guessed Guessed is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

When two inline tables butt against each other, they join to become one table.

The code, as written, is duplicating the content of the second table. However, it is doing this immediately below the second table. (Without testing the code) I would expect this to be joined with Table(2) and hence Table(2) now includes the previous and duplicated content. If you run the macro again, you would expect that pair to double again (ie grow exponentially).

If you think the code is duplicating two separate tables, perhaps visually it appears as two tables but is actually just one. If you use the Select Table command, you should be able to see where a table actually starts and finishes.

The code could be amended to ensure there is a paragraph between the original and duplicated table to avoid the two accidently becoming merged. If the command button is pressed repeatedly, where do you want the new duplicate added? It could go after Table 2 always but then it sits between the original and the first duplicate (which might not be what you wanted).

FWIW, I believe you should be using a Repeating Content Control do be doing this without needing a macro. See this youtube video for an example of how to do this.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 06-23-2021, 10:27 PM
gmayor's Avatar
gmayor gmayor is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You code does exactly the same in earlier Word versions?
The following modification will copy the original table to the end of the table each time it is used. Set the value of iRows to the number of rows in Table 2.
As implied by Andrew, you should be thinking about converting the form to use content controls rather than legacy form fields for the future.
https://www.gmayor.com/insert_content_control_addin.htm
has an option to do that, but it won't modify your code to work with the changed fields.



Code:
Private Sub CommandButton1_Click()
Dim oRng As Range
Dim oFld As FormFields
Dim i As Integer, j As Integer
Const iRows As Integer = 2    'The number of rows in table 2

    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    With ActiveDocument
        Set oRng = ActiveDocument.Tables(2).Range
        oRng.Collapse wdCollapseEnd
        For i = 1 To iRows
            oRng.FormattedText = ActiveDocument.Tables(2).Rows(i).Range.FormattedText
            Set oFld = oRng.FormFields
            For j = 1 To oRng.Fields.Count
                If oFld(j).Type = wdFieldFormDropDown Then
                    oFld(j).Result = oFld(j).DropDown.ListEntries(1).Name
                Else
                    oFld(j).Result = ""
                End If
            Next j
            oRng.Collapse wdCollapseEnd
        Next i
    End With
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 06-24-2021, 03:25 AM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

Thank you very much. That does exactly what I need and perfectly.
Reply With Quote
  #10  
Old 06-26-2021, 03:12 AM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

If I want to protect the form with a password, can the code unprotect and then re-protect the form? Right now there is no password.
Reply With Quote
  #11  
Old 06-26-2021, 09:39 AM
Mark6455 Mark6455 is offline Word 2019 will not properly run macro Windows 10 Word 2019 will not properly run macro Office 2019
Novice
Word 2019 will not properly run macro
 
Join Date: Jun 2021
Posts: 7
Mark6455 is on a distinguished road
Default

Nevermind. I got it. Thanks for all the help.
Reply With Quote
Reply

Tags
macro, macro error

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's new in Word 2019, Excel 2019 and Outlook 2019 and Why upgrade to Office 2019 is recommended? einfomail Office 1 07-26-2019 09:44 AM
PgUp/PgDn not working as expected in Word 2019; macro workaround? AmyJSchneider Word VBA 0 01-19-2019 03:45 PM
Word 2019 will not properly run macro Macro Enabled Word Doc Does not Properly Work on Different Machine danjoe Word VBA 3 10-19-2017 05:49 AM
Macro does not run properly dherr Word VBA 2 02-19-2015 08:50 AM
Word 2000 Macro not working properly brianlb Word VBA 1 07-01-2009 07:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:21 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