#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
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.
|
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
I need to request permission from the Sheriff to post the actual document. I will get back to you and appreciate the follow up.
|
#6
|
|||
|
|||
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.
|
#7
|
||||
|
||||
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 |
#8
|
||||
|
||||
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 |
#9
|
|||
|
|||
Thank you very much. That does exactly what I need and perfectly.
|
#10
|
|||
|
|||
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.
|
#11
|
|||
|
|||
Nevermind. I got it. Thanks for all the help.
|
Tags |
macro, macro error |
|
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 |
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 |