Variable Values dissapears

J

jab

Hi all

I have made a custom Dialog in which users will enter information.
This information will then be downloaded into the document via
docvariables. Some of the values shall also be saved as custom
properties with the document

I want the dialog to have preset values if no values are present in
the document i.e NOT in the document show the message "Error, No
document variable supplied" but a predefiend value via VBA instead. If
entries priviously have been made those should be loeded into the
dialog text fields when it is opened.

The code so far works and displays everything as stated if ALL the
fields of the dialog are prefilled in the dot file or if the user
fills them all in. But if one field is left empty when clicking ok in
the dialog, the variable entry in the document shows the error
statement described above in the docuument variable field (I want the
default value to be there). AND, here's the weirdest part, when
opening the dialog again for reentry of information, not only the left
out field is empty but a few of the others as well, i.e the code does
not pick up the values of the variables in the document and presents
them as the're supposed to. It only does so if ALLthe fields are
filled in and the values transferred to the document

Could som1 please tell me what is going on?

Here is the code:

Option Explicit
Dim oVar As Variables
Dim objCustomProperties As DocumentProperties
Private Sub UserForm_Initialize()

On Error GoTo Handler
Set oVar = ActiveDocument.Variables
' ## JAB Added ref to custom doc properties
Set objCustomProperties = ActiveDocument.CustomDocumentProperties

Me.txtprojname.Text = oVar("Project").Value
Me.Txtclientname.Text = oVar("Client").Value
Me.txtDocutype.Text = oVar("DocType").Value
Me.txtdate.Text = oVar("PrefDate").Value
Me.txtcontactp.Text = oVar("Contact").Value
Me.txtdirphonenumber.Text = oVar("DirPhone").Value
Me.Txtmobil.Text = oVar("Mobil").Value
Me.txtemail.Text = oVar("email").Value
Me.Txtnameofauthor.Text = oVar("DocAuthor").Value
Me.txtfax.Text = oVar("fax").Value
Me.txtstad.Text = oVar("City").Value
Me.txtpostal.Text = oVar("Postalnr").Value
Me.txtstreet.Text = oVar("street").Value

Exit Sub

With ActiveDocument
txtprojname = .Variables("Project").Value
Txtclientname = .Variables("Client").Value
txtDocutype = .Variables("Doctype").Value
txtdate = .Variables("Prefdate").Value
txtcontactp = .Variables("Contact").Value
txtdirphonenumber = .Variables("Dirphone").Value
Txtmobil = .Variables("Mobil").Value
txtemail = .Variables("email").Value
Txtnameofauthor = .Variables("DocAuthor").Value
txtfax = .Variables("fax").Value
txtstad = .Variables("City").Value
txtpostal = .Variables("Postalnr").Value
txtstreet = .Variables("Street").Value

End With

txtprojname.SetFocus
Txtclientname.SetFocus
txtDocutype.SetFocus
txtdate.SetFocus
txtcontactp.SetFocus
txtdirphonenumber.SetFocus
Txtmobil.SetFocus
txtemail.SetFocus
Txtnameofauthor.SetFocus
txtfax.SetFocus
txtstad.SetFocus
txtpostal.SetFocus
txtstreet.SetFocus

Handler:
oVar("Project").Value = "Project name"
oVar("Client").Value = "Client Name"
oVar("DocType").Value = "Type of Document"
oVar("PrefDate").Value = "Last editing date of document"
oVar("Contact").Value = "Contact person name at "
oVar("dirphone").Value = " "
oVar("mobil").Value = " "
oVar("email").Value = "...@"
oVar("DocAuthor").Value = "Name of document responsible"
oVar("fax").Value = " "
oVar("city").Value = " "
oVar("postalnr").Value = " "
oVar("street").Value = " "

End Sub

Private Sub cmdCancel_Click()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:=""

End If

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields = True Then
ActiveDocument.Unprotect Password:=""

Else

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True, Password:=""
Else
ActiveDocument.Unprotect Password:=""

End If
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:=""
Unload Me
End

End Sub

Private Function CheckExistsCDP(CDPName As String, propcoll As Object)
As Boolean
Dim PrCust As Object
For Each PrCust In propcoll
If PrCust.Name = CDPName Then
CheckExistsCDP = True
Exit For
Else
CheckExistsCDP = False
End If
Next
End Function

Private Sub CmdOK_Click()

Set oVar = ActiveDocument.Variables
Set objCustomProperties = ActiveDocument.CustomDocumentProperties

If Not CheckExistsCDP("Project", objCustomProperties) Then
objCustomProperties.Add Name:="Project", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtprojname.Text
Else
objCustomProperties("Project").Value = Me.txtprojname.Text
End If

If Not CheckExistsCDP("Client", objCustomProperties) Then
objCustomProperties.Add Name:="Client", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.Txtclientname.Text
Else
objCustomProperties("Client").Value = Me.Txtclientname.Text
End If

'If Not CheckExistsCDP("Contact", objCustomProperties) Then
' objCustomProperties.Add Name:="Contact", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtcontactp.Text
'Else
' objCustomProperties("Contact").Value = Me.txtcontactp.Text
'End If

If Not CheckExistsCDP("DocType", objCustomProperties) Then
objCustomProperties.Add Name:="DocType", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtDocutype.Text
Else
objCustomProperties("DocType").Value = Me.txtDocutype.Text
End If

If Not CheckExistsCDP("DocAuthor", objCustomProperties) Then
objCustomProperties.Add Name:="DocAuthor", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.Txtnameofauthor.Text
Else
objCustomProperties("DocAuthor").Value = Me.Txtnameofauthor.Text
End If

'If Not CheckExistsCDP("fax", objCustomProperties) Then
' objCustomProperties.Add Name:="fax", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtfax.Text
'Else
' objCustomProperties("fax").Value = Me.txtfax.Text
'End If

'If Not CheckExistsCDP("city", objCustomProperties) Then
' objCustomProperties.Add Name:="city", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtstad.Text
'Else
' objCustomProperties("city").Value = Me.txtstad.Text
'End If

'If Not CheckExistsCDP("postalnr", objCustomProperties) Then
' objCustomProperties.Add Name:="postalnr", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtpostal.Text
'Else
' objCustomProperties("postalnr").Value = Me.txtpostal.Text
'End If

'If Not CheckExistsCDP("street", objCustomProperties) Then
' objCustomProperties.Add Name:="street", LinkToContent:=False,
Type:=msoPropertyTypeString, Value:=Me.txtstreet.Text
'Else
' objCustomProperties("street").Value = Me.txtstreet.Text
'End If


oVar("DocType").Value = Me.txtDocutype.Text
oVar("Project").Value = Me.txtprojname.Text
oVar("Client").Value = Me.Txtclientname.Text
oVar("PrefDate").Value = Me.txtdate.Text
oVar("Contact").Value = Me.txtcontactp.Text
oVar("Dirphone").Value = Me.txtdirphonenumber.Text
oVar("mobil").Value = Me.Txtmobil.Text
oVar("email").Value = Me.txtemail.Text
oVar("DocAuthor").Value = Me.Txtnameofauthor.Text
oVar("fax").Value = Me.txtfax.Text
oVar("City").Value = Me.txtstad.Text
oVar("postalnr").Value = Me.txtpostal.Text
oVar("street").Value = Me.txtstreet.Text
 

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