IF statement, unprotect document only if protected

D

DavidRocastle

Hi i was wondering if anyone change help me. My code currently works on a
protected word document. I want the code to also execute if the document is
unprotected already. Would anyone know the if statement whereby if it is
protected then unprotect it else just carry on through the macro



Sub ChangeField()
'
' ChangeField Macro
' Macro recorded 19/06/2007 by ododd
'

ActiveDocument.Unprotect Password:="password"
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "FORMTEXT"
.Replacement.Text = "Title"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
ActiveDocument.Protect Password:="protect", NoReset:=False, Type:= _
wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
CommandBars("Task Pane").Visible = False
ActiveWindow.ActivePane.VerticalPercentScrolled = 0

Dim answer
answer = MsgBox("Fields have now been updated", 65, "Document Control")

End Sub
 
H

Helmut Weber

Hi David,

like this:

Sub ChangeField()
With ActiveDocument
If Not .ProtectionType = wdNoProtection Then
.Unprotect Password:="password"
End If
ActiveWindow.View.ShowFieldCodes = True
With .Range.Find
.Text = "FORMTEXT"
.Replacement.Text = "Title"
.Execute Replace:=wdReplaceAll
End With
End With
' ...
ActiveWindow.View.ShowFieldCodes = False
' ...
End Sub

If it helps, it is alright,
but I wonder whether that is the best way
to achieve what you want.

see in addition:
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top