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