How to keep editbox's value even when file is closed and reopened? (in word 2016)

62 Views Asked by At

I'm a beginner who just started learning macros through a Google search. I have succeeded in running the macro with the value entered in the edit box. I've been working on the normal.dotm file. For example, a macro to set the font size.

The custom UI is:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad = "onLoad">
~~~~
    <editBox id="editBox11" label="size" getText="editBox11_getText" sizeString="W" maxLength="4" onChange="editBox11_onChange" screentip="Font size in Lv1 format" />
~~~~

The vba of "ThisDocument" is:

Private myRibUI As IRibbonUI
Private v_Lv1size As String

Public Property Let ribbonUI(irib As IRibbonUI)
    Set myRibUI = irib
End Property

Public Property Get ribbonUI() As IRibbonUI
    Set ribbonUI = myRibUI
End Property

Public Property Let Lv1size(xVal As String)
     v_Lv1size = xVal
End Property

Public Property Get Lv1size() As String
     Lv1size = v_Lv1size
End Property

The vba of "Module1" is:

Sub Lv1()
    Selection.font.Size = ThisDocument.Lv1size
End Sub

Sub onLoad(ribbon As IRibbonUI)
    ThisDocument.ribbonUI = ribbon
    ThisDocument.Lv1size = 14
End Sub

Sub editBox11_getText(control As IRibbonControl, ByRef returnedVal)
    If control.ID = "editBox11" Then returnedVal = ThisDocument.Lv1size
End Sub

Sub editBox11_onChange(control As IRibbonControl, text As String)
    If Not IsNumeric(text) Then
        MsgBox ("Please enter a number. ")
        ThisDocument.ribbonUI.InvalidateControl "editBox11"
    ElseIf text < 1 Or text > 1638 Then
        MsgBox ("Please enter an appropriate font size..")
        ThisDocument.ribbonUI.InvalidateControl "editBox11"
    Else
        ThisDocument.Lv1size = text
    End If
End Sub

If the Lv1 macro of module 1 is executed, the font size is changed to the value of the editbox. However, if I use an editbox with a value of 12 and close and reopen Word, the editbox resets to a value of 14. Can I start over with the last used value? (12 in this case). I think I need to do something in the onLoad macro, but I'm a beginner so I'm not sure. It may be a little strange because it's a macro I made while searching around. Because it is not Excel, I could not follow the method of saving to a cell, and the command to save to another file or registry did not work well either. Thank you in advance.

1

There are 1 best solutions below

0
Raymond Wu On

You can try saving to the CustomDocumentProperties collection, something like:

ThisDocument.CustomDocumentProperties.Add "TestVar", False, msoPropertyTypeNumber, 14

Or use msoPropertyTypeString since your variable v_Lv1size is string.

Then when you need to modify:

ThisDocument.CustomDocumentProperties("TestVar") = v_Lv1size

And in your OnLoad sub, you can assign v_Lv1size = ThisDocument.CustomDocumentProperties("TestVar") or just throw away the variable, use the value in the document property directly.