PDA

View Full Version : User Form retain input using Custom Document Properties



ReltubP
07-30-2020, 09:00 PM
Relatively new to VBA coding.


I’ve set up a word document with a user form that works fine via custom properties. Example of code below:


Private Sub OK_Click()

' Update the properties values
ThisDocument.CustomDocumentProperties("customTitle").Value = Me.DocumentTitle.Value
ThisDocument.CustomDocumentProperties("customStartDate").Value = Me.CertDate.Value
ThisDocument.CustomDocumentProperties("customProjectNo").Value = Me.ProjectNo.Value
ThisDocument.CustomDocumentProperties("customEqDescription").Value = Me.EqDescription.Value
ThisDocument.CustomDocumentProperties("customClientName").Value = Me.ClientName.Value
ThisDocument.CustomDocumentProperties("customClientContact").Value = Me.ClientContact.Value
ThisDocument.CustomDocumentProperties("customOwnerPhoneNo").Value = Me.OwnerPhoneNo.Value
ThisDocument.CustomDocumentProperties("customOwnerEmail").Value = Me.OwnerEmail.Value
ThisDocument.CustomDocumentProperties("customRevisionNo").Value = Me.RevisionNo.Value
' Show changes of document properties in document
ThisDocument.Fields.Update

' Hide the userform
UserForm1.Hide

End Sub

Problem is when I save and open an existing document the data is not saved in the user form. If I hit enter on the user form it wipes all entries in the document.
Would be good if the user form could retain the old inputs. Is this doable, and does someone have some code that does this?

Appreciate any advice...

gmayor
07-31-2020, 03:00 AM
How are you using this document? What is its relationship to the 'existing document'?
I suspect that the issue might be as simple as confusion between the use of ThisDocument and ActiveDocument, ThisDocument being the document with the macro.

ReltubP
07-31-2020, 03:36 AM
It is a simple word document that is repeated time and again for different clients etc.


I open the document, save as, and fill out the user form.

gmayor
07-31-2020, 05:32 AM
Save it as a template c/w the macro and userform and create new documents from it. Change ThisDocument to ActiveDocument

Frankly I wouldn't use document properties for this task. I would suggest instead using content controls to both receive and display the data in the document. See https://www.gmayor.com/Userform.htm

Paul_Hossler
07-31-2020, 09:04 AM
If you want to use custom properties, try something like this

Seems to work



Option Explicit


Private Sub OK_Click()
ThisDocument.CustomDocumentProperties("SomeProp").Value = Me.custProp.Value

'https://stackoverflow.com/questions/54769648/custom-document-property-not-getting-saved-in-word-document
ThisDocument.Saved = False ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Me.Hide
Unload UserForm1
End Sub


Private Sub UserForm_Initialize()
Dim oCP As DocumentProperty
Dim i As Long

With ThisDocument
For i = 1 To .CustomDocumentProperties.Count
If .CustomDocumentProperties(i).Name = "SomeProp" Then
Set oCP = .CustomDocumentProperties(i)
Exit For
End If
Next i

If oCP Is Nothing Then ' custom prop doesn't exist yet
.CustomDocumentProperties.Add Name:="SomeProp", LinkToContent:=False, Value:="", Type:=msoPropertyTypeString
Else
Me.custProp.Value = oCP.Value
End If
End With
End Sub

gmaxey
07-31-2020, 01:12 PM
Paul,

Why the For ... Next loop? Why not:

Dim oCP As DocumentProperty
With ThisDocument
On Error Resume Next
Set oCP = .CustomDocumentProperties("SomeProp")
If oCP Is Nothing Then ' custom prop doesn't exist yet
.CustomDocumentProperties.Add Name:="SomeProp", LinkToContent:=False, Value:="", Type:=msoPropertyTypeString
Else
custProp.Value = oCP.Value
End If
Set oCP = Nothing
End With

Paul_Hossler
07-31-2020, 01:57 PM
@Greg -- That is a better approach

Actually, I tried something like that but hit some error (my fault, not Word's) and just went brute force

Glad you pointed it out

ReltubP
07-31-2020, 06:38 PM
Thanks gents.

I'm working my way through Graham Mayor's content control method currently.

ReltubP
12-08-2020, 05:16 AM
I could never get this to work. For some reason the userform when opening does not populate the document, and there seems to be no macro to run it again...:(

Paul_Hossler
12-09-2020, 08:07 PM
Without the actual document, it's hard to see what could possibly be wrong

Use [Go Advanced] at botton right of Reply box, and then the paperclip icon to attach the file

Chas Kenyon
12-10-2020, 11:43 AM
Both Graham Mayor and Greg Maxey have pages on this:


Interactive Userforms by Greg Maxey
(https://gregmaxey.com/word_tip_pages/interactive_userforms.html)
Create a Simple Userform by Graham Mayor, MVP (http://www.gmayor.com/Userform.htm)


That is, where the userform populates the document and uses information in the document to populate/initialize the userform.
(https://gregmaxey.com/word_tip_pages/interactive_userforms.html)