WORD - Help with removing protection

garlarr

New Member
Joined
Sep 16, 2009
Messages
12
I need some VBA that will remove protection only if document has protection. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have a subfolder that has 20 docm, some have protection and some don’t. I need some code that will only remove protection on docm that are protected.<o:p></o:p>
I tried using this code with no success
Code:
[FONT=Arial][SIZE=3]If ActiveDocument.ProtectionType <> wdNoProtection Then 
ActiveDocument.Unprotect Password:=""[/SIZE][/FONT]
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Re: Help with removing protection

Garlarr, I believe there is a policy on the board of not being able to help unprotect workbooks or documents.
 
Upvote 0
Re: Help with removing protection

You are misunderstanding the question
I am not asking for a special code that will open a password protected workbook or document.
Read my previous question a little closer.
I already know the passwords, that not what I’m after.
I have a folder that holds 20 or so word documents that have link fields, some have protection and others do not. I’m trying to find some VBA that I can use with my existing code that will remove protection ONLY on the documents that have protection (I KNOW THE PASSWORD) so that I can unlink fields so that I can archive documents without having to worry about losing data because of link fields.
 
Upvote 0
Re: Help with removing protection

You can record a macro that removes the protection from a document. Use the recorded macro to remove it from all documents.

For each Document
Is the Document protected?
If so, remove protection
Loop

is slower then

For each Document
remove protection
Loop
 
Upvote 0
Re: Help with removing protection

Hi Mikerickson
I’m using Office 2007 and I was unable to record a macro that will unprotect document. I tried to do it a couple different ways, from a different docx and docm in question. I was also unable to record from one folder to another. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Here is the code I am using that allows me to copy a folder, remove link fields. <o:p></o:p>
this may give you a better understanding.

Code:
[FONT=Calibri][SIZE=3]Dim fso As Scripting.FileSystemObject[/SIZE][/FONT]
[SIZE=3][FONT=Calibri]  Dim fol As Scripting.Folder[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim fil As Scripting.File[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim wrd As Word.Application[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim doc As Word.Document[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim fld As Word.Field[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim strSource As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Dim strTarget As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  strSource = "C:\Documents and Settings\Reports"  ' Source folder path[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  strTarget = "C:\Documents and Settings\My Documents\Reports 2" ' Target folder path[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Set fso = CreateObject("Scripting.FileSystemObject")[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  If fso.FolderExists(strTarget) Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    MsgBox strTarget & " already exists!", vbExclamation[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  fso.CopyFolder strSource, strTarget[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Set wrd = CreateObject("Word.Application")[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Set fol = fso.GetFolder(strTarget)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  For Each fil In fol.Files[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    If fil.Name Like "*.doc*" Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      Set doc = wrd.Documents.Open(Filename:=fil.Path, _[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        AddToRecentFiles:=False)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      For Each fld In doc.Fields[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        If fld.Type = wdFieldLink Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]          fld.Update[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]          fld.Unlink[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]        End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      Next fld[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]      doc.Close SaveChanges:=True[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  Next fil[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]  wrd.Quit SaveChanges:=False[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]End Sub[/SIZE][/FONT]

<o:p>I was thinking of using something like this added to my existing code

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ""
End If

</o:p>
 
Upvote 0

Forum statistics

Threads
1,225,521
Messages
6,185,463
Members
453,295
Latest member
cheewah0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top