Snapshot of data structures, VBA excel [duplicate]

2019-08-14 10:52发布

问题:

Possible Duplicate:
What's the fastest way to export all excel add-in modules to .bas files?

Is it possible to do snapshot of all used modules and data structures in VBA Excel and recover it some time after?

The reason is reproducing error after crashes

ADDED. Yes, in essence I want to serialize classes and types.

回答1:

I feel like what you kind of want is to serialize all your classes as they are to an XML file? I know this function exists in other languages but VBA might not have it as it is. (I'm sure you could force something) – Brad

@Brad, yes, exactly, as one of possible ways – nikaan

Assuming that this method does not exist for VBA (although it is available for VB.NET or VB 2005: http://support.microsoft.com/kb/315703), I would probably try to make a simulation: Write the state of each object / variable (of those needed) to a txt file by using FSO.
I don't know if this is feasible for your project though and I probably wouldn't like to do this with pleasure for a huge amount of code.

dim sFile               As string 
Dim FSO                 As FileSystemObject     
Dim FSOFile             As TextStream 


sFile = "U:/Log.txt"

Set FSO = New FileSystemObject
Set FSOFile = FSO.OpenTextFile(sFile, 2, True) 
FSOFile.writeline (<Descriptions+Property/variable value>) 
FSOFile.Close
set FSO = nothing 

Depending on how well you input descriptions (eg. inlcuding boolean values for successful instantiation of objects, etc...), you may be able to see what has happened behind the screen up to the point where your application crashed... I don't know if this answers your question, as I'm a little bit doubting about what you refer to with "fields" (comments).



回答2:

When being the owner of a VBA project, you can create a sub routine that exports all the modules in your currenct project. This link provides the code:

http://edndoc.esri.com/arcobjects/9.0/Samples/Application_Framework/ExportVBACode.htm

For the purpose as you describe it, I don't see a reason to do this.
Personally I would use a lame trick like this one:

Option Explicit

Sub Save_File()

Dim sCur_Path           As String
Dim sCopied_Path        As String

Application.DisplayAlerts = False
Application.ScreenUpdating = False


sCur_Path = ThisWorkbook.FullName
sCopied_Path = "U:/Copied_New.xlsm"

ThisWorkbook.SaveAs sCopied_Path
ThisWorkbook.SaveAs sCur_Path

End Sub