UserForm to modify Custom Doc Proeprties

D

DeputySmith770

I have a form which on open a UserForm promts the user to input common
case data. After clicking the UserForm closed, the data filled into
the UserForm creates custom document properties, and populates the
Word Form at pre-defined bookmark locations.

The issue is that if the user incorrectly entered data into the
UserForm, or missed something all together, they can't change it as
the code is written now. Can someone help me figure out how to re-
open the UserForm containing the current values of the Custom Doc
Proeprties, allow the user to change the values, and then re-populate
the Word Form with the new values?

Here is the code I am working with now:
*************************************
Private Sub CommandButton1_Click()

'Check what type of protection - if any - has been applied
Select Case ActiveDocument.ProtectionType

'This tells the program the protection type is set to allow changes to
formfields only.
Case wdAllowOnlyFormFields

'If we've got this far, it's protected for forms
'Now unprotect the document
ActiveDocument.Unprotect Password:="28990"
ActiveDocument.SpellingChecked = False

'Dim CaseNumber As String
'Dim IncidentType As String

With ActiveDocument.CustomDocumentProperties
.Add Name:="Case Number", LinkToContent:=False,
Value:=CaseNumber, Type:=msoPropertyTypeString
.Add Name:="Incident Type", LinkToContent:=False,
Value:=IncidentType, Type:=msoPropertyTypeString
.Add Name:="Report Date", LinkToContent:=False,
Value:=ReportDate, Type:=msoPropertyTypeString
.Add Name:="Report Time", LinkToContent:=False,
Value:=ReportTime, Type:=msoPropertyTypeString
.Add Name:="Occurred Date1", LinkToContent:=False,
Value:=OccurredDate1, Type:=msoPropertyTypeString
.Add Name:="Occurred Date2", LinkToContent:=False,
Value:=OccurredDate2, Type:=msoPropertyTypeString
.Add Name:="Occurred Time1", LinkToContent:=False,
Value:=OccurredTime1, Type:=msoPropertyTypeString
.Add Name:="Occurred Time2", LinkToContent:=False,
Value:=OccurredTime2, Type:=msoPropertyTypeString
.Add Name:="Deputy Name", LinkToContent:=False,
Value:=DeputyName, Type:=msoPropertyTypeString
.Add Name:="Deputy Radio", LinkToContent:=False,
Value:=DeputyRadio, Type:=msoPropertyTypeString
.Add Name:="Deputy DPSST", LinkToContent:=False,
Value:=DeputyDPSST, Type:=msoPropertyTypeString

End With

'This ends the selected section of the protected document.
End Select

ActiveDocument.Fields.Update
ActiveDocument.Repaginate

'Re-protect the document, and apply the same password protection.
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="28990"
Application.ScreenUpdating = True
Application.ScreenRefresh

OpenInfo.Hide

End Sub
***************************************

Thanks!
 
J

Jay Freedman

You don't need to change anything in the CommandButton1_Click()
procedure. What you need now is to write a UserForm_Initialize()
procedure that gets the value of each of the custom document
properties and places that value in the corresponding control on the
userform. It will be a series of statements like

CaseNumber.Text = ActiveDocument _
.CustomDocumentProperties("Case Number")

You will also need error trapping to handle the possibility that one
or more of the document properties don't exist.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

DeputySmith770

Jay,

I got the above part to work, but now I am stuck at how to 'update' or
modify the CustomDoc Properties when the user makes a change. My
thought is to somehow include in the initialize a statement that
clears the values from the DocProperties, but displays them on the
UserForm. Thus, when the user clicks to close the form, the new and
remaining values will be reentered into the CustomDoc Properties value
fields. Right?

How do I do that?

Please advise. Thank you for your help!

Deputysmith770
 
J

Jay Freedman

If you don't care that all the existing custom document properties get
wiped out, you can put this code at the beginning of the
CommandButton1_Click() procedure:

' clear all existing properties
Dim cProp As DocumentProperty
For Each cProp In ActiveDocument.CustomDocumentProperties
cProp.Delete
Next

Don't put it in the Userform_Initialize() procedure. The reason is
that the user might cancel the userform, and you don't want to lose
all the existing data if there's no way to put it back.

If you want a more "elegant" method that preserves any properties the
user didn't change, you can write a separate subroutine that either
adds a new property or simply changes the value of an existing one:

Private Sub CommandButton1_Click()
MakeOrUpdateCDP "Case Number", TextBox1.Text
MakeOrUpdateCDP "Case Name", TextBox2.Text
ActiveDocument.Fields.Update
Me.Hide
End Sub

Private Sub MakeOrUpdateCDP(strName As String, strVal As String)
Dim cProp As DocumentProperty, theProp As DocumentProperty
' find the property if it already exists
For Each cProp In ActiveDocument.CustomDocumentProperties
If LCase(cProp.Name) = LCase(strName) Then
Set theProp = cProp
Exit For
End If
Next

If Not theProp Is Nothing Then
' check its type, if wrong type then delete
If theProp.Type <> msoPropertyTypeString Then
theProp.Delete ' makes it = Nothing
End If
End If

If Not theProp Is Nothing Then
' it does exist, so update it
theProp.Value = strVal
Else
' it does not exist, so create it
ActiveDocument.CustomDocumentProperties.Add _
Name:=strName, LinkToContent:=False, _
Value:=strVal, Type:=msoPropertyTypeString
End If
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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