Programmatically export/import forms and modules?

  • Thread starter Thread starter Gustaf
  • Start date Start date
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
 
"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" ------
 
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
 
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.
 
Back
Top