I am not sure of the point of reading the values twice e.g.
PropVal = ReadProp("Subject")
Subject = ReadProp("Subject")
and if Subject has no value, what's the point of reading any further values? The following reads the docproperties almost instantaneously (when present)
I don't like to rely on error conditions in order to progress. The following does not use error handling to read or write values.
Option Explicit
'''READ VARIABLES TO FILE PROPERTIES (ON LOAD)
Private Sub UserForm_Initialize()
Dim PropVal As String
PropVal = ReadProp("Subject")
If PropVal = "" Then Exit Sub
Subject = PropVal
DateComplete = ReadProp("Date Completed")
Company = ReadProp("Company")
NoOfStages = ReadProp("NoOfStages")
Stage1Process = ReadProp("Stage1Process")
TrackSpeed = ReadProp("TrackSpeed")
Temperature_Controller_Type = ReadProp("Temperature_Controller_Type")
Temperature_Indicator = ReadProp("Temperature_Indicator")
Product_Height = ReadProp("Product_Height")
Product_Width = ReadProp("Product_Width")
Product_Length = ReadProp("Product_Length")
Customer_Address1 = ReadProp("Customer Address1")
Customer_Address2 = ReadProp("Customer Address2")
Customer_Address3 = ReadProp("Customer Address3")
Customer_Address4 = ReadProp("Customer Address4")
Customer_Address5 = ReadProp("Customer Address5")
Customer_Address6 = ReadProp("Customer Address6")
End Sub
'''WRITE VARIABLES TO FILE PROPERTIES
Private Sub OK_Click()
Dim PlantList As String
'Changes document properties to values of userform. Values are linked in documents and will update automatically
' FOUND HERE: https://wordmvp.com/FAQs/MacrosVBA/MixedDocProps.htm
Call WriteProp(sPropName:="Subject", sValue:=Subject)
Call WriteProp(sPropName:="Company", sValue:=Company)
Call WriteProp(sPropName:="Date completed", sValue:=DateComplete)
Call WriteProp(sPropName:="NoOfStages", sValue:=NoOfStages)
Call WriteProp(sPropName:="Stage1Process", sValue:=Stage1Process)
Call WriteProp(sPropName:="TrackSpeed", sValue:=TrackSpeed)
Call WriteProp(sPropName:="Temperature_Controller_Type", sValue:=Temperature_Controller_Type)
Call WriteProp(sPropName:="Temperature_Indicator", sValue:=Temperature_Indicator)
Call WriteProp(sPropName:="Product_Height", sValue:=Product_Height)
Call WriteProp(sPropName:="Product_Width", sValue:=Product_Width)
Call WriteProp(sPropName:="Product_Length", sValue:=Product_Length)
Call WriteProp(sPropName:="Customer Address1", sValue:=Customer_Address1)
Call WriteProp(sPropName:="Customer Address2", sValue:=Customer_Address2)
Call WriteProp(sPropName:="Customer Address3", sValue:=Customer_Address3)
Call WriteProp(sPropName:="Customer Address4", sValue:=Customer_Address4)
Call WriteProp(sPropName:="Customer Address5", sValue:=Customer_Address5)
Call WriteProp(sPropName:="Customer Address6", sValue:=Customer_Address6)
ActiveDocument.Fields.Update
Unload DocProperties
End Sub
Private Sub WriteProp(sPropName As String, sValue As String, Optional lType As Long = msoPropertyTypeString)
Dim oProp As DocumentProperty
Dim bBuilt As Boolean, bCust As Boolean
For Each oProp In ActiveDocument.BuiltInDocumentProperties
If oProp.Name = sPropName Then
oProp.Value = sValue
bBuilt = True
Exit For
End If
DoEvents
Next oProp
If Not bBuilt Then
For Each oProp In ActiveDocument.CustomDocumentProperties
If oProp.Name = sPropName Then
oProp.Value = sValue
bCust = True
Exit For
End If
DoEvents
Next oProp
If Not bCust Then
ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, _
LinkToContent:=False, _
Type:=lType, _
Value:=sValue
End If
End If
lbl_Exit:
Set oProp = Nothing
Exit Sub
End Sub
Private Function ReadProp(strName As String) As Variant
Dim oProp As DocumentProperty
Dim bBuilt As Boolean, bCust As Boolean
For Each oProp In ActiveDocument.BuiltInDocumentProperties
If oProp.Name = strName Then
ReadProp = oProp.Value
bBuilt = True
Exit For
End If
DoEvents
Next oProp
If Not bBuilt Then
For Each oProp In ActiveDocument.CustomDocumentProperties
If oProp.Name = strName Then
ReadProp = oProp.Value
bCust = True
Exit For
End If
DoEvents
Next oProp
If Not bCust Then ReadProp = ""
End If
lbl_Exit:
Exit Function
End Function