Protected documents

C

Chuck

Hi all

Is there any way to manipulate custom document properties and document
fields in non-protected sections of documents that contain protected sections?

I've got code that adds/changes custom doc properties and updates related
fields on open but any documents that have one or more sections protected
generate requests for the protection password -- cancelling past the password
request allows the macro to continue, but the properties/fields are not
added/deleted/changed.

Thanks for any thoughts...
Chuck
 
D

Doug Robbins

Show us the code.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
C

Chuck

Hi Doug

Here's the AutoOpen macro. I'm aware that I could hard code the document
protection password into the macro to unprotect it, change the document
property and then re-protect it, but I'd rather not do that because
protection passwords can vary.

Also I should mention that I've included code that checks the document view
and then at the end resets the view to the original view because looping
through the story ranges to update the document property fields changes the
view to Normal when it hits things like footers and footnotes. I don't know
why but I had to use the Select Case code at the end to reset the view
because simply resetting the view to the numeric value (stored as nCurView)
didn't actually change the view.

In any case my real question is how do I change document properties in a
protected document without having to provide the password? Thanks for any
help...

Chuck -- code follows:

Sub AutoOpen()

On Error GoTo errorhandler

Const DMS_DOC_REF_NAME As String = "DMSLink.(Default).Reference"

Dim prpDocProp As DocumentProperty
Dim bFound As Boolean
Dim srStoryRange As Range
Dim fldField As Field
Dim s As String
Static nCurView As Long

bFound = False

nCurView = CLng(ActiveDocument.ActiveWindow.View)

For Each prpDocProp In ActiveDocument.CustomDocumentProperties
With prpDocProp
If .Name Like "DMSLink.*Reference" Then
.Name = DMS_DOC_REF_NAME
bFound = True
End If
Exit For
End With
Next prpDocProp

If bFound = False Then
ActiveDocument.CustomDocumentProperties.Add _
Name:=DMS_DOC_REF_NAME, LinkToContent:=False,
Value:="[Reference]", _
Type:=msoPropertyTypeString
End If

For Each srStoryRange In ActiveDocument.StoryRanges
For Each fldField In srStoryRange.Fields
If fldField.Type = wdFieldDocProperty Then
With fldField
If .Code.Text Like "*DMSLink.*Reference*" Then
.Code.Text = " DOCPROPERTY """ & DMS_DOC_REF_NAME &
""" \* MERGEFORMAT "
.Update
End If
End With
End If
Next fldField
Next srStoryRange

bFound = False

Select Case nCurView
Case 1
ActiveDocument.ActiveWindow.View = wdNormalView
Case 3
ActiveDocument.ActiveWindow.View = wdPrintView
Case 5
ActiveDocument.ActiveWindow.View = wdOutlineView
Case 6
ActiveDocument.ActiveWindow.View = wdWebView
End Select

Exit Sub

errorhandler:

MsgBox Err.Number & " " & Err.Description

End Sub
 

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