Programmatically export/import forms and modules?

G

Gustaf

I made a VB6 app to extract forms and modules from Excel workbooks. Now I'd like to expand this app to also handle Access files (.mdb). Here's the critical parts of the code for Excel:

Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim VBComp As VBIDE.VBComponent

' Load workbook
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlWb = xlApp.Workbooks.Open(sWorkbook)

' Loop through all files (components) in the workbook
For Each VBComp In xlWb.VBProject.VBComponents

' Export the file
VBComp.Export sFile

Next VBComp

I'm trying to do the same thing with an Access file now, but can't find a corresponding Export method. Can it be done? Later, I will need to import (merge) the component files back into the Access file too.

Best regards,

Gustaf
 
D

Dirk Goldgar

"Gustaf" wrote in message news:[email protected]...
I made a VB6 app to extract forms and modules from Excel workbooks. Now I'd
like to expand this app to also handle Access files (.mdb). Here's the
critical parts of the code for Excel:

Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim VBComp As VBIDE.VBComponent
' Load workbook
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlWb = xlApp.Workbooks.Open(sWorkbook)
' Loop through all files (components) in the workbook
For Each VBComp In xlWb.VBProject.VBComponents

' Export the file
VBComp.Export sFile
Next VBComp

I'm trying to do the same thing with an Access file now, but can't find a
corresponding Export method. Can it be done? Later, I will need to import
(merge) the component files back into the Access file too.


What format do you have it in mind to export into? My guess is that you
will want to use the hidden method SaveAsText. For example,

'----- start of "air code" ------
' Note: as written, requires a reference to the
' Microsoft Access <version> Object Library.
' Could easily be written to use late binding instead.

Dim accApp As Access.Application
Dim ao As AccessObject

Set accApp = New Access.Application
With accApp

.OpenCurrentDatabase "YourDatabasePath"

' Export forms.
For Each ao In .CurrentProject.AllForms
.SaveAsText acForm, ao.Name, ao.Name & ".txt"
Next ao

' Export reports.
For Each ao In .CurrentProject.AllReports
.SaveAsText acReport, ao.Name, ao.Name & ".txt"
Next ao

' Export modules.
For Each ao In .CurrentProject.AllModules
.SaveAsText acModule, ao.Name, ao.Name & ".txt"
Next ao

.Quit

End With
'----- end of "air code" ------
 
G

Gustaf

Dirk said:
"Gustaf" wrote in message news:[email protected]...


What format do you have it in mind to export into? My guess is that you
will want to use the hidden method SaveAsText. For example,

'----- start of "air code" ------
' Note: as written, requires a reference to the
' Microsoft Access <version> Object Library.
' Could easily be written to use late binding instead.

Dim accApp As Access.Application
Dim ao As AccessObject

Set accApp = New Access.Application
With accApp

.OpenCurrentDatabase "YourDatabasePath"

' Export forms.
For Each ao In .CurrentProject.AllForms
.SaveAsText acForm, ao.Name, ao.Name & ".txt"
Next ao

' Export reports.
For Each ao In .CurrentProject.AllReports
.SaveAsText acReport, ao.Name, ao.Name & ".txt"
Next ao

' Export modules.
For Each ao In .CurrentProject.AllModules
.SaveAsText acModule, ao.Name, ao.Name & ".txt"
Next ao

.Quit

End With
'----- end of "air code" ------

Hi Dirk,

Hidden method! No wonder I didn't see it! How do you see hidden methods? And don't say "you don't"... :)

I was greatly helped by your code. What I had in mind was to export to the VB formats, I thought Forms in Access where actual VB forms, but apparently they are not. If I rename them to .frm and try to open them in VB6, it complains about bad syntax.

I wonder if it's possible to suppress the Security Warning triggered by this line:

accApp.OpenCurrentDatabase sAccessFile

That line brings up a "This file may not be safe..." dialog where I need to click an Open button.

Gustaf
 
D

Dirk Goldgar

Gustaf said:
Hidden method! No wonder I didn't see it! How do you see hidden methods?
And don't say "you don't"... :)

In the Object Browser (in the VBA Editor environment), you can right-click
almost anywhere in the object-browser window and choose "Show Hidden
Members" from the popup menu.
I was greatly helped by your code. What I had in mind was to export to the
VB formats, I thought Forms in Access where actual VB forms, but
apparently they are not. If I rename them to .frm and try to open them in
VB6, it complains about bad syntax.

No, Access forms are totally different from VB forms.
I wonder if it's possible to suppress the Security Warning triggered by
this line:

accApp.OpenCurrentDatabase sAccessFile

That line brings up a "This file may not be safe..." dialog where I need
to click an Open button.

If the database you're opening is in a trusted location, as designated in
the Trust Center, I don't think you'll get that message.
 

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