Can I find an answer here about a Word Macro issue? I am trying to apply a macro to a Protected Form document. The user will click on a button to "Add a new Activity" table, which is saved in annother file to be inserted using the macro. But when I apply the macro, this message appears: "Run-time error '4605' The object refers to a Protected document..." I found this code in some forum, but don't know how to configure to make my macro work...

Sub User_name()
' Macro by JanOSX
'
Dim nameStr As String, bName As String
Dim bookNum As Integer
nameStr = Environ("USERNAME")

bookNum = Selection.BookmarkID
ActiveDocument.Sections(2).ProtectedForForms = False
ActiveDocument.FormFields(bookNum).Select
Selection.TypeText (nameStr)
ActiveDocument.Sections(2).ProtectedForForms = True

End Sub

Anyone know how to program Word macros?

Hi,

If you want to unprotect a document copy a selection from another document, paste in the unprotected form, and reprotect, use the below sub:

Sub CopyProt()

    Windows("Doc2.doc").Activate
    Selection.Copy
    
    Windows("Doc1.doc").Activate
    ActiveDocument.Protect wdNoProtection
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.PasteAndFormat (wdPasteDefault)
    ActiveDocument.Protect wdAllowOnlyFormFields
    
End Sub

Thank you, I will try this. If you need any help with design, I'll be around...

Hi,

If you want to unprotect a document copy a selection from another document, paste in the unprotected form, and reprotect, use the below sub:

Sub CopyProt()

    Windows("Doc2.doc").Activate
    Selection.Copy
    
    Windows("Doc1.doc").Activate
    ActiveDocument.Protect wdNoProtection
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.PasteAndFormat (wdPasteDefault)
    ActiveDocument.Protect wdAllowOnlyFormFields
    
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.