Сообщение от Человек=)
к сожалению, такой вариант не работает!
когда включаеш защиту — запись макроса исчезает….вот если бы узнать код VBA на включение защиты…
Visual Basic | ||
|
ActiveDocument.Unprotect Password:= «Пароль» — команда на снятие защиты
ActiveDocument.Protect Password:=»Пароль» — команда на установку защиты
Вот выдержка из справки:
UnProtect Method
Removes protection from the specified document. If the document isn’t protected, this method generates an error.
expression.UnProtect(Password)
expression Required. An expression that returns a Document object.
Password Optional Variant. The password string used to protect the document. Passwords are case-sensitive. If the document is protected with a password and the correct password isn’t supplied, a dialog box prompts the user for the password.
Remarks
Security Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.
Example
This example removes protection from the active document, using the value of the strPassword variable as the password.
Visual Basic | ||
|
This example removes protection from the active document. Text is inserted, and the document is protected for revisions.
Visual Basic | ||
|
Приготовишь себе правильный макрос, справка рулит))) Я думаю тут большая часть необходимого тебе кода для макроса))
Word Document Password Protection – 2 most used Methods
Yes, there are more than one methods available to password protect a Word document.
And it confused the programmers all the time about, which code to use to unprotect the document & read the content.
Here are 2 of the most used methods:
- Document level Password with Encryption
- Document level Protection for Editing.
Lets see each of these categories and how to deal them when we automate the process through VBA.
1.VBA Macro to Open Word Document with Password Protection
A word document can be saved with protection on & a password set for encryption. This option can be found in this menu navigation
- File -> Info -> Protect Document -> Encrypt with password
- Type password & click ok
- Save & close document
Now, everytime the word document is opened, it will ask for a password. It will only display the contents of the file, if the entered password is correct.
Well, manually you can type the password. But how to deal with these password protected files with a VBA code.
Sub openWordwithPassword()
'Define
Dim sFileName As String
Dim passwd As String
Dim oWord As Object
Dim oDoc As Object
'Init
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
sFileName = "C:Filepath.docx"
passwd = "enteryourpasswordhere"
'Open document using password
Set oDoc = oWord.Documents.Open(sFileName, PasswordDocument:=passwd)
'Do pocessing
Debug.Print oDoc.Content
oDoc.Save
'Save with password
oDoc.SaveAs Filename:=sFileName, Password:=passwd
'Close
oDoc.Close
End Sub
Once the document is opened using this method, its content can be edited or extracted. This is a document level password protection. In case your document is only writeprotected, then use the below method.
2. VBA Code to Unprotect Word Document – Enable Editing
The below code unprotects a word document with password for readonly. Then in the next step, you will be able to edit or read contents
Then, after processing the data, the code enables writeProtection on the document. i.,e edit is disabled & file is saved.
Sub UnprotectwordwithPassword()
'Define
Dim sFileName As String
Dim passwd As String
Dim oWord As Object
Dim oDoc As Object
'Init
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
sFileName = "C:Filepath.docx"
passwd = "enteryourpasswordhere"
'Open document using password
Set oDoc = oWord.Documents.Open(sFileName, PasswordDocument:=passwd)
If ActiveDocument.ProtectionType <> -1 Then oDoc.Unprotect passwd
'Do pocessing
Debug.Print oDoc.Content
'Protect again with password
objDoc.Protect 3, , passwd '3- - Refer end of the page for more parameters
oDoc.Save
'Close
oDoc.Close
End Sub
This VBA code is very useful, then you have multiple files to be marked as readonly or protected from users editing it. It is tough to open and edit each document one by one and then enable a password.
Instead, with this process, you can place all the word documents in a folder. Then make the macro to read files one by one and enable password protection.
With document.Protect function You can use one of these parameters which serves different purpose.
-> wdProtectionType & corresponding value
- wdAllowOnlyComments = 1
- wdAllowOnlyFormFields = 2
- wdAllowOnlyReading = 3
- wdAllowOnlyRevisions = 0
- wdNoProtection = -1
External Reference:
1. Here is another related topic from Microsoft forum about Word document password protection.
Tighten these settings to protect your computer from malware
Updated on February 27, 2020
Serkan Ismail / Getty Images
Macros for Microsoft Word are one of the best ways to boost your productivity but they’re not without risk. Macros are customized recordings of custom commands and actions to be performed in Word that streamline frequently performed tasks. When recording a macro, you can either assign the macro to a keyboard shortcut combination or to a button above the ribbon.
These instructions apply to Microsoft Word 2019, 2016, 2013, Word for Microsoft 365 and Word for Mac.
Security Risks and Precautions
Macros from unknown or unreliable sources may contain malicious code that compromise your security and privacy.
The default Macro security level in Word is set to high. This setting means that if a macro does not meet one of the following two requirements, Microsoft Word will not allow it to run.
- The macro you are trying to run must have been created using the copy of Microsoft Word that is installed on your computer.
- The Macro you are trying to run must have a digital signature from a verified and trustworthy source.
The reason that these security measures have been put in place is that people reported malicious code implanted in macros to Microsoft in the past. While this default setting is ideal for protecting most people, it will make it a bit more difficult for you to use macros from other sources that may not have digital certificates. There is, however, a workaround for those of us who need more lax security.
Word 2010 and Later
To edit your macro security settings in Word 2010, 2013, and Microsoft 365, you have several options.
Option 1
- If you open a document that has macros enabled, you will see a yellow bar at the top of your screen with a button that says Enable Content, which you can use to enable the macros for that document for single use.
Option 2
- If you see the yellow Enable Content bar, you can also make the document a “Trusted document” so that the macros in that document will always be allowed. If you want to make allow macros in the document follow these steps:
-
Press the File button when you see the warning bar.
-
Select Enable Content in the Security Warning area.
-
Select Always in the Enable All Content section to mark the document as trusted.
Option 3
- You can also change the macro security settings in the Trust Center similarly to the Word 2007 method.
-
Press File on the top left corner.
-
Press the Options button.
-
Select Trust Center then Trust Center Settings.
-
On the resulting page, click Macro Settings.
-
Select the Disable all macros with notification option so that the macros will be disabled but you will receive a popup window asking if you want to enable macros individually.
-
Select the OK button twice to make the changes.
-
Restart Word to finalize your changes.
Thanks for letting us know!
Get the Latest Tech News Delivered Every Day
Subscribe
Using VBA, I believe you’d be looking for the protect
method:
Application.ActiveDocument.Protect wdAllowOnlyRevisions, password:="password"
With protect
, you can specify the type of protection, the password, and various components of the protection (style lock, resetting of form fields, etc.).
More on protect
can be found here.
Likewise, VBA has a similar method for unprotecting a Word document, if you know the password. This is the aptly named unprotect
method.
Application.ActiveDocument.Unprotect "password"
More on unprotect
can be found here.
Topics Map > OS and Desktop Applications > Applications > Productivity
This document explains how to enable macro protection in older versions of Office so that macros do not run automatically when a document is opened. (This occurs when the lowest level of security has been selected).
To view instructions specific to the version of Word you are using click on one of the following links:
Windows |
Macintosh |
---|---|
Word 2000 | |
Word 2002 (XP) / 2003 | Word 2004 |
Word 2007 / 2010 | Word 2008 |
Instructions for Word running on a Windows machine:
Word 2000
- Start Word
- Click on Tools, highlight Macro and click on Security
- Click on either Medium or High to select it
- Click on OK
- Click on OK again to close out of the options menu
Word 2002 (XP) / 2003
- Start Word
- Click on Tools and select Options
- Select the Security tab and click on the Macro Security button
- Click on either Medium or High to select it
- Click on OK
- Click on OK again to close out of the options menu
Word 2007 / 2010
- Start Word
- Click on Office Button and select Word Options
- Select the Trust Center option and click on the Trust Center Settings… button
- Click on either Disable All Macros With Notification or Disable All Macros With Notification to select it
- Click on OK
- Click on OK again to close out of the Word Options menu
Instructions for Word running on a Macintosh system:
Word 2004
- Start Word
- Click on Word and select Preferences
- Click once on the Security category listed on the left-hand side of the window to highlight it
- Place a check in the box next to Warn before opening a file that contains macros
Word 2008
- Currently, macro protection can only be configured for Excel in Office 2008.
Note: Enabling macro protection in Microsoft Word and/or Access (aka: macro virus protection) will not detect specific macro viruses for you but will inform you if a macro exists on a document you are trying to open. Macros detected by macro virus protection are not necessarily viruses. However, if you are alterted to a macro attached to a document, you should be cautious as most users do not have macros in their documents.