我注意到一些Excel加載項從一個用戶維護狀態到另一個用戶。 Solver加載項就是一個例子,即使在關閉並重新打開Excel電子表格後,加載項也會維護表單數據。VBA:保存Excel表單狀態
保存的表單元素在哪裏可以保持狀態?我有興趣在自定義加載項中模擬這種行爲,並且無法弄清楚如何使其工作。
我注意到一些Excel加載項從一個用戶維護狀態到另一個用戶。 Solver加載項就是一個例子,即使在關閉並重新打開Excel電子表格後,加載項也會維護表單數據。VBA:保存Excel表單狀態
保存的表單元素在哪裏可以保持狀態?我有興趣在自定義加載項中模擬這種行爲,並且無法弄清楚如何使其工作。
設置存儲在隱藏名稱的工作表上:
Sub Test()
Dim nName As Name
For Each nName In ActiveWorkbook.Names
Debug.Print nName.Name, nName.RefersTo
Next nName
End Sub
我能夠確認求解器使用隱藏的名字,這正是我之後的。 – 2014-12-02 10:12:31
我最終需要保存超過255個字符,以便隱藏名稱是行不通的爲了我。所以我最終使用了沒有255字符限制的工作表自定義屬性。我的功能保存表單的狀態,然後恢復狀態結束這樣看:
保存狀態:
Private Sub save_form_state()
Dim prop As CustomProperty
For Each Control In Me.Controls
'' only saving state for text boxes and ref edits
If TypeName(Control) = "TextBox" Or TypeName(Control) = "RefEdit" Then
'' only save state for controls with values -- custom properties can't have null values
If Control.Value <> "" Then
For Each prop In ActiveWorkbook.ActiveSheet.CustomProperties
'' if the name already exists in custom properties then delete it
If prop.Name = Control.Name Then prop.Delete
Next prop
'' any dupe has been deleted, so write the key and value
ActiveWorkbook.ActiveSheet.CustomProperties.Add Name:=Control.Name, Value:=Control.Value
End If
End If
Next Control
End Sub
恢復狀態:
Private Sub restore_form_state()
Dim prop As CustomProperty
'' go through every text box and ref edit control then see if there's a custom property
'' with matching name. If so, set the value of the control = custom property value
For Each Control In Me.Controls
If TypeName(Control) = "TextBox" Or TypeName(Control) = "RefEdit" Then
For Each prop In ActiveWorkbook.ActiveSheet.CustomProperties
If Control.Name = prop.Name Then
Control.Value = prop.Value
End If
Next prop
End If
Next Control
End Sub
沒考慮太多此類(也許有更好的方法可以做到這一點),但我想,你正在尋找序列化。看看http://www.ozgrid.com/forum/showthread.php?t=21472和http://www.vb-helper.com/tut5.htm也許它會幫助你開始...... – 2014-12-01 21:10:07
緩存在文檔中:http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.tools.applications.runtime.cachedattribute.aspx – Chrismas007 2014-12-01 21:12:00
這取決於你如何構建加載項。您是使用vba編寫* .xlam加載項,還是使用其他語言編寫COM加載項? – RubberDuck 2014-12-01 21:19:36