Consulting

Results 1 to 2 of 2

Thread: Run Macro on Protected Word Document

  1. #1
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    1
    Location

    Exclamation Run Macro on Protected Word Document

    Good morning!
    I was hoping for some help on a simple VBA issue. I am new to this and still learning, so explicit instructions or examples would really help! I made a document with a button that functions to auto-create pre-formatted rows in a document that will be used by others in my company. However, I want to protect this document so that they can only edit in fill in forms and use the button to add new rows to the tables. But the issue arises when I protect the file.
    The button function is:


    Sub CommandButton1_Click
    Dim i As Integer
    For i = 2 To 2
    ActiveDocument.Tables(5).Rows(i).Select
    Selection.Copy
    ActiveDocument.Tables(5).Rows.Add
    ActiveDocument.Tables(5).Rows(ActiveDocument.Tables(5).Rows.Count).Range.Pa ste
    ActiveDocument.Tables(5).Rows.Last.Delete
    Next i
    End Sub

    This code works perfectly to add pre-created rows to a certain table. And I have two buttons that function to automatically add to two separate tables.

    Yet, when I protect the file, I get:

    Run-time error '4605'

    This method or property is not available because the object refers to a protected area of the document.

    When I click to debug, the highlighted issue is with the Selection.Copy (line 5 above).
    Thank you again for all your help in this, its a great learning experience and if I can find this solution, it would be super helpful to apply to a bunch of different documents.

    Thanks!!
    Becca

  2. #2
    You must unprotect the document then reprotect it after making the change - see the last example at https://www.gmayor.com/word_vba_examples_2.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •