Export/Import: How do I delete Userform copies?

D

davidm

The code below which mimics standard CODE CLEANERS (1) exports al
(non-sheet based) modules in WORKBOOK("TEST.XLA") as text files etc.
(2) deletes the modules and (3) then imports back the files as ne
modules (The text files in folder are also meant to be deleted). Th
whole idea is to clean up the codes in the WORKBOOK so to reduce th
file size which gets bloated over time. The problem I face is that th
code works fine except that when there are Userforms, the copies create
in the directory folder fail to be removed by the *Kill filename
command. Notice that, for any one Userform, two (2) FRX files ar
created even where one file extension (.frm) is used. I woul
appreciate any help.

Here is the code (adapted from C.Pearson's):

Sub ExportImportModulesToClean()

Dim x As VBComponent
Dim VBComps As VBComponents

Set VBComps = WORKBOOKS("TEST.XLA").VBProject.VBComponents
For Each x In VBComps

If x.Type = vbext_ct_MSForm Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".frm"
End If

If x.Type = vbext_ct_stdmodule Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".std"
End if


If x.Type = vbext_ct_classmodule Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".cls"
End if

x.Export FName

'delete old modules
VBComps.Remove x

VBComps.Import FName

Kill FName

Next

End Sub

[PS: I have gone to this trouble of trying to fashion a code cleane
that can be incorporated into my project and distibuted as a componen
part.
 
T

Tom Ogilvy

Sub ExportImportModulesToClean()

Dim x As VBComponent
Dim VBComps As VBComponents

Set VBComps = WORKBOOKS("TEST.XLA").VBProject.VBComponents
For Each x In VBComps

If x.Type = vbext_ct_MSForm Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".frm"
FName1 = Replace(FName,".frm",".frx")
End If

If x.Type = vbext_ct_stdmodule Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".std"
End if


If x.Type = vbext_ct_classmodule Then
FName = ActiveWorkbook.Path & "\" & x.Name & ".cls"
End if

x.Export FName


'delete old modules
VBComps.Remove x

VBComps.Import FName

Kill FName
If x.Type = vbext_ct_MSForm Then Kill FName1

Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top