View Single Post
Old 02-07-2012, 03:59 PM   #445
Gauthier
Enthusiast
Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'Gauthier knows the difference between 'who' and 'whom'
 
Posts: 25
Karma: 10000
Join Date: Jan 2012
Device: kindle
Cleanup of get, set prefix

Add a Class Module Named Param
Code:
Dim ParamName As String
Dim ParamType As Long
Dim ParamValue As Variant
Dim ParamDefault As Variant
Dim ParamLocation As Long
Dim ParamAutoSave As Boolean
Dim ParamControlId As String

Dim isSet As Boolean


Private Sub Class_Initialize()
    isSet = False
End Sub

Public Property Get Default() As Variant
    Default = ParamDefault
End Property

Public Property Let Default(v As Variant)
    ParamDefault = v
End Property

Public Property Get Value() As Variant
    If isSet Then
        Value = ParamValue
    Else
        Value = ParamDefault
    End If
End Property

Public Property Let Value(v As Variant)
    isSet = True
    ParamValue = v
    If ParamAutoSave Then
        Save
    End If
    If Len(ParamControlId) > 0 Then
        UpdateControl ParamControlId
    End If
End Property

Public Sub Define(PropName As String, Optional PropDefaultValue As Variant = "", Optional PropType As Long = msoPropertyTypeString, Optional SaveTo As Long = pLocationVolatile, Optional AutoSave As Boolean = False, Optional RibbonControlId As String = "")
    ParamName = PropName
    Default = PropDefaultValue
    ParamType = PropType
    ParamLocation = SaveTo
    ParamAutoSave = AutoSave
    ParamControlId = RibbonControlId
    Read
End Sub

Public Sub Save()
    
    On Error Resume Next
    
    Select Case ParamLocation
        Case Is = pLocationVolatile
            'do not save
        Case Is = pLocationSystem
            SaveSetting "BookCreator", "Parameters", ParamName, CStr(ParamValue)
        Case Is = pLocationDocument
            ActiveDocument.CustomDocumentProperties(ParamName) = ParamValue
            If Err <> 0 Then
                ActiveDocument.CustomDocumentProperties.Add _
                    Name:=ParamName, LinkToContent:=False, _
                    Value:=ParamValue, _
                    Type:=ParamType
            End If
        Case Is = pLocationTemplate
            ThisDocument.CustomDocumentProperties(ParamName) = ParamValue
            If Err <> 0 Then
                ThisDocument.CustomDocumentProperties.Add _
                    Name:=ParamName, LinkToContent:=False, _
                    Value:=ParamValue, _
                    Type:=ParamType
            End If
    End Select
End Sub

Private Sub Read()

    On Error Resume Next
    
    Select Case ParamLocation
        Case Is = pLocationVolatile
            'Revert to the default value
            isSet = False
        Case Is = pLocationSystem
            'We Only Get a String here
            ParamValue = GetSetting("BookCreator", "Parameters", ParamName)
            If Len(ParamValue) > 0 Then
                'A Value was stored, convert to the defined type.
                isSet = True
                Select Case ParamType
                    Case Is = msoPropertyTypeBoolean
                        ParamValue = CBool(ParamValue)
                    Case Is = msoPropertyTypeNumber
                        ParamValue = CLng(ParamValue)
                    Case Is = msoPropertyTypeString
                        'Nothing to Do
                End Select
            End If
        Case Is = pLocationDocument
            'According to the doc, it is already correctly typed.
            ParamValue = ActiveDocument.CustomDocumentProperties(ParamName)
            If Err = 0 Then 'The CustomDocumentProperty named ParamName existed
                isSet = True
            End If
        Case Is = pLocationTemplate
            'According to the doc, it is already correctly typed.
            ParamValue = ThisDocument.CustomDocumentProperties(ParamName)
            If Err = 0 Then 'The CustomDocumentProperty named ParamName existed
                isSet = True
            End If
    End Select
    'Debug.Print "Param::Read ParamName:=" + ParamName + ", Value:=" + CStr(ParamValue)
End Sub

'Erase the Parameter Value from its storage.
Public Sub Reset()
    Dim docProp As DocumentProperty
    On Error Resume Next
    isSet = False
    Select Case ParamLocation
        Case Is = pLocationVolatile
            'Was Never saved, nothing to do
        Case Is = pLocationSystem
            'Remove from registry
            DeleteSetting "BookCreator", "Parameters", ParamName
        Case Is = pLocationDocument
            'Remove from Custom Document Properties
            docProp = ActiveDocument.CustomDocumentProperties(ParamName)
            docProp.Delete
        Case Is = pLocationTemplate
            'Remove from Custom Document Properties
            docProp = ThisDocument.CustomDocumentProperties(ParamName)
            docProp.Delete
    End Select
End Sub
previous get/set are replaced by:
Code:
'Some constant for Parameter Location
Public Const pLocationVolatile As Long = 0 'Parameter is not saved
Public Const pLocationSystem As Long = 1   'Parameter is saved to the HKCU Registry
Public Const pLocationDocument As Long = 2 'Parameter is saved to The document
Public Const pLocationTemplate As Long = 3 'Parameter is saved to The template

' CONFIGURATION VARIABLES
Public CalibrePath As Param

Public BaseFont As Param
Public BaseFontEpub As Param
Public BaseFontLIT As Param
Public WordSpaceLRF As Param
Public HeaderFormat As Param
Public Margin_Top As Param
Public Margin_Bottom As Param
Public Margin_Left As Param
Public Margin_Right As Param
Public LinkLevel As Param
Public ImpDeviceType As Param
Public BC_FIRST_RUN As Param
Public BookCreatorVersion As Param
Public AdditionalParam As Param
Public IMPAdditionalParam As Param
Public BookCreatorBuildVersion As Param
Public LocationPATH As Param
Public LocationSaveOption As Param
Public AutoSaveTemplate As Param
Public eBookReaderSaveOption As Param
Public eBookReaderPath As Param
Public DebugON_OFF As Param

Public InputProfile As Param
Public OutputProfile As Param
Public DisableJustify As Param
Public CoverImage As Param

Sub InitParams()
    Set DebugON_OFF = New Param
    DebugON_OFF.Define "DebugON_OFF", False, msoPropertyTypeBoolean, pLocationSystem, True
    Set CalibrePath = New Param
    CalibrePath.Define "CalibrePath", DefaultCalibrePath, msoPropertyTypeString, pLocationSystem, True
    Set BaseFont = New Param
    BaseFont.Define "BaseFont", "8", msoPropertyTypeString, pLocationDocument
    Set BaseFontEpub = New Param
    BaseFontEpub.Define "BaseFontEpub", "80", msoPropertyTypeString, pLocationDocument
    Set BaseFontLIT = New Param
    BaseFontLIT.Define "BaseFontLIT", "8", msoPropertyTypeString, pLocationDocument
    Set WordSpaceLRF = New Param
    WordSpaceLRF.Define "WordSpaceLRF", "2.0", msoPropertyTypeString, pLocationDocument
    Set HeaderFormat = New Param
    HeaderFormat.Define "HeaderFormat", """%t""", msoPropertyTypeString, pLocationDocument
    Set Margin_Top = New Param
    Margin_Top.Define "Margin_Top", "10", msoPropertyTypeString, pLocationDocument
    Set Margin_Bottom = New Param
    Margin_Bottom.Define "Margin_Bottom", "0", msoPropertyTypeString, pLocationDocument
    Set Margin_Left = New Param
    Margin_Left.Define "Margin_Left", "10", msoPropertyTypeString, pLocationDocument
    Set Margin_Right = New Param
    Margin_Right.Define "Margin_Right", "10", msoPropertyTypeString, pLocationDocument
    Set LinkLevel = New Param
    LinkLevel.Define "LinkLevel", 1, msoPropertyTypeNumber, pLocationDocument
    Set ImpDeviceType = New Param
    ImpDeviceType.Define "ImpDeviceType", 2, msoPropertyTypeNumber, pLocationDocument
    Set BC_FIRST_RUN = New Param
    BC_FIRST_RUN.Define "BC_FIRST_RUN", True, msoPropertyTypeBoolean, pLocationDocument
    Set BookCreatorVersion = New Param
    BookCreatorVersion.Define "BookCreatorVersion", "1.5", msoPropertyTypeString, pLocationDocument
    Set AdditionalParam = New Param
    AdditionalParam.Define "AdditionalParam", "", msoPropertyTypeString, pLocationDocument
    Set IMPAdditionalParam = New Param
    IMPAdditionalParam.Define "IMPAdditionalParam", "", msoPropertyTypeString, pLocationDocument
    Set BookCreatorBuildVersion = New Param
    BookCreatorBuildVersion.Define "BookCreatorBuildVersion", 15, msoPropertyTypeNumber, pLocationDocument
    Set LocationPATH = New Param
    LocationPATH.Define "LocationPATH", "", msoPropertyTypeString, pLocationDocument
    Set LocationSaveOption = New Param
    LocationSaveOption.Define "LocationSaveOption", "", msoPropertyTypeString, pLocationDocument
    Set AutoSaveTemplate = New Param
    AutoSaveTemplate.Define "AutoSaveTemplate", True, msoPropertyTypeBoolean, pLocationDocument
    Set eBookReaderSaveOption = New Param
    eBookReaderSaveOption.Define "eBookReaderSaveOption", False, msoPropertyTypeBoolean, pLocationDocument
    Set eBookReaderPath = New Param
    eBookReaderPath.Define "eBookReaderPath", "", msoPropertyTypeString, pLocationDocument
    
    Set InputProfile = New Param
    InputProfile.Define "InputProfile", cDEFAULT, msoPropertyTypeNumber, pLocationDocument, True
    Set OutputProfile = New Param
    OutputProfile.Define "OutputProfile", cDEFAULT, msoPropertyTypeNumber, pLocationDocument, True
    Set DisableJustify = New Param
    DisableJustify.Define "DisableJustify", False, msoPropertyTypeBoolean, pLocationDocument, True
    Set CoverImage = New Param
End Sub
Then replace usage:
getSomeParam -> SomeParam.Value
setSomeParam X -> SomeParam.Value = X

That will make form data persistence more easy to manage and also save about 40KB from the file.

The Default Values also clean up the INITIALIZE:
Code:
Sub INITALIZE()
'________________________________________________
' NAME: INITALIZE
' AUTHOR: =X=
' DESCRIPTION: Initalizes BookCreator application
'   for the very first run.
' HISTORY:
' DATE: 09/08/2008
'________________________________________________
    InitParams
    If BC_FIRST_RUN.Value = True Then
        CreateToolBox True
        BC_FIRST_RUN.Value = False
    End If
End Sub

Last edited by Gauthier; 02-08-2012 at 06:45 PM.
Gauthier is offline   Reply With Quote