Search on a protected document

C

constance

Hi everybody,
I have a word document that contains protected sections. I need to do a
search on this document using those command lines below :

Dim MyValue
MyValue = "Formation :"
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
..ClearFormatting
Counter = 0
Do While .Execute(FindText:=MyValue, Forward:=True) = True
Counter = Counter + 1
Loop
End With
If Counter >= 1 Then
Response = MsgBox("Error.", vbOKOnly, "Error")
Exit Sub
End If

but it doesn't seem to work on a protected document
what can I do
maybe I need to un protect my sections but the first time I execute my
mecro they don't exist so I will have an error

can somebody help me please ?
Maybe I can only do a search on one section ?????

Thanx for all your help

Lyne
 
S

Sally Green

Hi

In your code unprotect the document first. If you don't
know the code you can record this part of it. If the
first time you run it the document isn't protected it will
give an error which will have an error number. Write into
your code an error trap to trap that particular error and
resume the code from the point that it found the error.
So if the error number given was 1234 the code would be ...

Sub xxxx()
On error goto EH

lines of code go here.

Just before your End Sub put the following ...

Exit Sub

EH:

If err.number = 1234 then
Resume Next
Else
Msgbox err.number &": "& err.description
End if

End Sub

So what will happen is that when it finds that particular
error it will just skip over it. Hope this helps


Sally Green
 
C

Charles Kenyon

Or, rather than causing, then trapping, an error try adding the following
procedure and calling it when you need to unprotect a form:

Sub ProtectDocumentMacro()
If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End Sub

The unprotect procedure is even simpler:

Sub UnprotectDocumentMacro()
' Unprotect document
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
End Sub

There is code on the MVP website at
<url: http://www.mvps.org/word/FAQs/MacrosVBA/SpellcheckProtectDoc.htm> to
completely subtitute for the protect document dialogs so as to not reset
when protecting a form.

Hope this helps,
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.


Sally Green said:
Hi

In your code unprotect the document first. If you don't
know the code you can record this part of it. If the
first time you run it the document isn't protected it will
give an error which will have an error number. Write into
your code an error trap to trap that particular error and
resume the code from the point that it found the error.
So if the error number given was 1234 the code would be ...

Sub xxxx()
On error goto EH

lines of code go here.

Just before your End Sub put the following ...

Exit Sub

EH:

If err.number = 1234 then
Resume Next
Else
Msgbox err.number &": "& err.description
End if

End Sub

So what will happen is that when it finds that particular
error it will just skip over it. Hope this helps


Sally Green








-----Original Message-----

Hi everybody,
I have a word document that contains protected sections. I need to do a
search on this document using those command lines below :

Dim MyValue
MyValue = "Formation :"
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.ClearFormatting
Counter = 0
Do While .Execute(FindText:=MyValue, Forward:=True) = True
Counter = Counter + 1
Loop
End With
If Counter >= 1 Then
Response = MsgBox("Error.", vbOKOnly, "Error")
Exit Sub
End If

but it doesn't seem to work on a protected document
what can I do
maybe I need to un protect my sections but the first time I execute my
mecro they don't exist so I will have an error

can somebody help me please ?
Maybe I can only do a search on one section ?????

Thanx for all your help

Lyne


------------------------------------------------
[/url]
~~View and post usenet messages directly from http://www.ExcelForum.com/

.
 

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