PDA

View Full Version : Importing a Module That I've Exported



Cyberdude
05-07-2006, 10:13 AM
I have no difficulty using VBA to export modules for backup. I do it regularly.
What are the statements I must use to import a module back into the workbook?

Norie
05-07-2006, 10:40 AM
Isn't it simply something like this?


Workbooks("book1").VBProject.VBComponents.Import "C:\Module1.bas"

Cyberdude
05-07-2006, 01:36 PM
Hey, Norie, thanks a load! I can always count on you.
I looked for it in help, but it eluded me. :ole:
Sid

johnske
05-07-2006, 05:36 PM
If it's not a 'one-off' and you import regularly, try
Option Explicit
'
Sub ImportCodeModule()
'
Dim Filt$, Title$, FileName$
Dim Message As VbMsgBoxResult
'
Do
'the "double" (*.bas; *.frm; *.cls) below is only required for office 2000
'for other versions you should be able to safely delete one (*.bas; *.frm; *.cls)
Filt = "VB Files (*.bas; *.frm; *.cls)(*.bas; *.frm; *.cls),*.bas;*.frm;*.cls"
Title = "SELECT A FOLDER - CLICK OPEN TO IMPORT - CANCEL TO QUIT"
FileName = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=5, Title:=Title)
'
On Error GoTo Finish
'the actual module import
Application.VBE.ActiveVBProject.VBComponents.Import (FileName)
'
'import more modules or exit loop?
Message = MsgBox(FileName & vbNewLine & " has been imported - more imports?", vbYesNo, "More Imports?")
'
Loop Until Message = vbNo
'
Finish:
Message = vbYes
End Sub

Cyberdude
05-07-2006, 07:37 PM
Looks good, John. Thanks for the suggestion. I'll give it a try. I put together something less sophisticated using Norie's suggestion, and while it works, I'm thinking about doing something more elegant.