Hi,
I would like to create a macro that will prevent users from changing
certain text in the document.
The text is a field in Section 1 of the document. The field is
DOCPROPERTY Title. I always want them to use File > Properties to
update the title, not change the title within the document by
overwriting the field (most of the users would not know what a field
is). I would like that as soon as they try to delete or overwrite or
even select the title, that a message box comes up to say "Please go
to File > Properties" to change the title.

I do not want to use Document Protection as it is too restrictive for
the rest of the document.

I have seen some code that will prevent changes to the header or
footer, which I will implement for the DOCPROPERTY fields in the
header and footer, but I would like to be able to do it for the
document text itself also.

My thoughts on how to do it would be to restrict any changes within a
section (if they select any text within Section 1 pop up an error
message) or if they select any text between two bookmarks.

I'm just not sure how to code this, can anyone help?

Re: Prevent users from changing Text without Document Protection by Jean-Guy

Jean-Guy
Mon Feb 12 19:59:45 CST 2007

JodieM was telling us:
JodieM nous racontait que :

> Hi,
> I would like to create a macro that will prevent users from changing
> certain text in the document.
> The text is a field in Section 1 of the document. The field is
> DOCPROPERTY Title. I always want them to use File > Properties to
> update the title, not change the title within the document by
> overwriting the field (most of the users would not know what a field
> is). I would like that as soon as they try to delete or overwrite or
> even select the title, that a message box comes up to say "Please go
> to File > Properties" to change the title.
>
> I do not want to use Document Protection as it is too restrictive for
> the rest of the document.
>
> I have seen some code that will prevent changes to the header or
> footer, which I will implement for the DOCPROPERTY fields in the
> header and footer, but I would like to be able to do it for the
> document text itself also.
>
> My thoughts on how to do it would be to restrict any changes within a
> section (if they select any text within Section 1 pop up an error
> message) or if they select any text between two bookmarks.
>
> I'm just not sure how to code this, can anyone help?

You would need application level event procedures, see
http://msdn2.microsoft.com/en-us/library/aa140279(office.10).aspx

You would need a "WindowSelectionChange" event.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Prevent users from changing Text without Document Protection by JodieM

JodieM
Tue Feb 13 21:20:46 CST 2007

On Feb 13, 12:59 pm, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> JodieM was telling us:
> JodieM nous racontait que :
>
>
>
>
>
> > Hi,
> > I would like to create a macro that will prevent users from changing
> > certain text in the document.
> > The text is a field in Section 1 of the document. The field is
> > DOCPROPERTY Title. I always want them to use File > Properties to
> > update the title, not change the title within the document by
> > overwriting the field (most of the users would not know what a field
> > is). I would like that as soon as they try to delete or overwrite or
> > even select the title, that a message box comes up to say "Please go
> > to File > Properties" to change the title.
>
> > I do not want to use Document Protection as it is too restrictive for
> > the rest of the document.
>
> > I have seen some code that will prevent changes to the header or
> > footer, which I will implement for the DOCPROPERTY fields in the
> > header and footer, but I would like to be able to do it for the
> > document text itself also.
>
> > My thoughts on how to do it would be to restrict any changes within a
> > section (if they select any text within Section 1 pop up an error
> > message) or if they select any text between two bookmarks.
>
> > I'm just not sure how to code this, can anyone help?
>
> You would need application level event procedures, see
> http://msdn2.microsoft.com/en-us/library/aa140279(office.10).aspx
>
> You would need a "WindowSelectionChange" event.
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org- Hide quoted text -
>
> - Show quoted text -

Thank You for that Jean-Guy, I had seen the Header and Footer example,
I just did not look at it closely enough to see that it would help for
this also.

Can someone please help me with the bit of code that finds out what
text is selected.
eg if I want to see if the text selected is a Field, what code should
I use?
I tried selection.information and selection.type but neither of them
will tell me that my selection is a field? I could use row and column
positions but I think that is quite cumbersome.


Re: Prevent users from changing Text without Document Protection by Jean-Guy

Jean-Guy
Wed Feb 14 00:04:05 CST 2007

JodieM was telling us:
JodieM nous racontait que :

>> JodieM was telling us:
>> JodieM nous racontait que :

<snip>

> Thank You for that Jean-Guy, I had seen the Header and Footer example,
> I just did not look at it closely enough to see that it would help for
> this also.
>
> Can someone please help me with the bit of code that finds out what
> text is selected.
> eg if I want to see if the text selected is a Field, what code should
> I use?
> I tried selection.information and selection.type but neither of them
> will tell me that my selection is a field? I could use row and column
> positions but I think that is quite cumbersome.

Try

'_______________________________________
If Selection.Range.Fields.Count > 0 Then
MsgBox "There is at least one field in the currently selected text."
Else
MsgBox "There are no fields in the currently selected text."
End If
'_______________________________________

But you will need to fine tune, for example, if only part of the field is
selected, the above will not work.

This will:
'_______________________________________
Dim rgeSelected As Range
Dim rge1 As Range
Dim rge2 As Range

Set rgeSelected = Selection.Range

With Selection
.Fields.ToggleShowCodes
.Fields.ToggleShowCodes
.Expand wdWord
'If the original selection was inside a field but didn't fully enclose
'the field, then running Selection,Fields.Count would return 0.
'The above 3 lines force the selection to enclose the field,
'if it is inside one.


If .Fields.Count = 0 Then
rgeSelected.Select
MsgBox "Selection contains no fields"
ElseIf .Fields.Count > 1 Then
rgeSelected.Select
MsgBox "Selection contains more than one field" _
& " (and therefore it is not inside a field)"
Else
'We now know it contains 1 field - but may also contain some text _
'outside of the field, so we need to check
Set rge1 = .Range
Set rge2 = .Fields(1).Result
If rge1 = rge2 Then
rgeSelected.Select
MsgBox "Selection is inside a field"
Else
rgeSelected.Select
MsgBox "Selection contains a field but contains" _
& " text as well"
End If
End If
End With
'_______________________________________

Test it with different fields of different length.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Prevent users from changing Text without Document Protection by JodieM

JodieM
Mon Feb 19 16:08:32 CST 2007

On Feb 14, 5:04 pm, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> JodieM was telling us:
> JodieM nous racontait que :
>
> >> JodieM was telling us:
> >> JodieM nous racontait que :
>
> <snip>
>
> > Thank You for that Jean-Guy, I had seen the Header and Footer example,
> > I just did not look at it closely enough to see that it would help for
> > this also.
>
> > Can someone please help me with the bit of code that finds out what
> > text is selected.
> > eg if I want to see if the text selected is a Field, what code should
> > I use?
> > I tried selection.information and selection.type but neither of them
> > will tell me that my selection is a field? I could use row and column
> > positions but I think that is quite cumbersome.
>
> Try
>
> '_______________________________________
> If Selection.Range.Fields.Count > 0 Then
> MsgBox "There is at least one field in the currently selected text."
> Else
> MsgBox "There are no fields in the currently selected text."
> End If
> '_______________________________________
>
> But you will need to fine tune, for example, if only part of the field is
> selected, the above will not work.
>
> This will:
> '_______________________________________
> Dim rgeSelected As Range
> Dim rge1 As Range
> Dim rge2 As Range
>
> Set rgeSelected = Selection.Range
>
> With Selection
> .Fields.ToggleShowCodes
> .Fields.ToggleShowCodes
> .Expand wdWord
> 'If the original selection was inside a field but didn't fully enclose
> 'the field, then running Selection,Fields.Count would return 0.
> 'The above 3 lines force the selection to enclose the field,
> 'if it is inside one.
>
> If .Fields.Count = 0 Then
> rgeSelected.Select
> MsgBox "Selection contains no fields"
> ElseIf .Fields.Count > 1 Then
> rgeSelected.Select
> MsgBox "Selection contains more than one field" _
> & " (and therefore it is not inside a field)"
> Else
> 'We now know it contains 1 field - but may also contain some text _
> 'outside of the field, so we need to check
> Set rge1 = .Range
> Set rge2 = .Fields(1).Result
> If rge1 = rge2 Then
> rgeSelected.Select
> MsgBox "Selection is inside a field"
> Else
> rgeSelected.Select
> MsgBox "Selection contains a field but contains" _
> & " text as well"
> End If
> End If
> End With
> '_______________________________________
>
> Test it with different fields of different length.
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org

Thanks Jean-Guy - Beautiful Code - I think it will work well!!!
Jodie