This Client Server article is republished in its entirety from 2004 for reference purposes.
Download the Code: The download contains a full working example, but please note it is a programming example and not a utility.
There are times when you may need to export your VBA project’s components. Perhaps your project has become corrupted, or you wish to re-use the components (Userform, Module, or Class) for other VBA projects so you do not have to rewrite the code. This article will illustrate how to use the Windows dialog to select a VBA project, how to extract the components, and how to write the project’s references to a text file.
The first step is to create a main routine, and define the variables needed to write this code. You will also want to initialize a flag variable to determine if the project you wish to export is already loaded or not:
Dim fileName As String
Dim filePath As Variant
Dim oProj As VBProject
Dim oComponent As VBComponent
Dim index As Integer
isLoadedFlag = False
Next we want to call up the Windows FileOpenDialog box to allow the user to select the VBA project:
fileName = FileOpenDialog("Load VBA Project", "BLOCKED SCRIPTvoid(null);", "*.mvba", "VBA Project")
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
lStructSize As Long
hInstance As Long
lpstrCustomFilter As String
nFilterIndex As Long
nMaxFile As Long
nMaxFileTitle As Long
lpstrTitle As String
nFileOffset As Integer
lpstrDefExt As String
lpfnHook As Long
End Type
Public Const OFN_EXPLORER = &H80000
Public Const OFN_HIDEREADONLY = &H4&
'==========================================================
'When user selects project, application will export components
'to C:\VBARefs.txt datafile. Then it deletes the project.
'-----------------------------------------------------------------------+
' name FileOpenDialog
' author BSI-DEVSPT 7/2001
strFileExtDescription As String) As String
'
' ...
' MsgBox (filename)
Dim OpenFile As OPENFILENAME
Dim pos As Long ' Used for trimming C style string.
Dim strFilter As String
' Populate the OPENFILENAME data type
OpenFile.hwndOwner = 0
strFilter = strFileExtDescription & " (" & strFileExt & ")" & Chr(0) & strFileExt & Chr(0)
OpenFile.nFilterIndex = 1
OpenFile.lpstrTitle = strTitle
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
status = GetOpenFileName(OpenFile)
' Return the file selected
FileOpenDialog = ""
' Trim C style string for compatibility with VB strings.
pos = InStr(OpenFile.lpstrFile, strTmp)
End If
End Function
For Each oProject In oVBE.VBProjects
'If project name matches search criteria.
isLoadedFlag = True
After setting the flag to “1” we can call our function, which will parse out the path from our filename variable. Just write the following inside the “If…Then” statement, after isLoadedFlag = True:
fPath = fileStripPath(fileName)
' name fileStripPath
' author BSI 07/2002
Function fileStripPath(filePath As String) As String
Dim Path As Variant
Dim count As Integer
count = 0
Path = Split(filePath, "\") ' strip out the full path
'While array is not last item.
'Concatenate path together.
'Physically putting slashes in between folder names.
'Add one to counter.
Wend
fileStripPath = pathName
'Export the project components to the project's path.
The NamedProjectExport function will do the following: Find the project you are going to export, create a text file called VBARefs.txt, and write out all the reference information that was being used by your VBA project to the path of the project. It will then loop through all of the project’s components, and export them all out to the path as well.
Dim oProject As VBProject
Dim oRef As Reference
Set oVBE = Application.VBE
'Loop through each project
If UCase(name) = UCase(oProject.fileName) Then
Open dir + "VBARefs.txt" For Output As #1
For Each oRef In oProject.References
Write #1, oRef.FullPath
For Each oComp In oProject.VBComponents
Select Case oComp.Type
suffix = ".cls"
suffix = ".frm"
suffix = ".bas"
fileName = dir & oComp.name & suffix
Next
End Sub
You will notice that different types of modules have different extensions. Each extension represents the following:
.frm = Userform
The last two lines you want to include inside the “If…Then” statement will unload the VBA Project, and then delete it using the Kill function:
CadInputQueue.SendKeyin "vba unload " + fileName
Kill filename
If isLoadedFlag <> True Then
CadInputQueue.SendKeyin "vba load " + fileName
'Get the path of the project.
NamedProjectExport fileName, fPath
'Unload the project from MicroStation.
Kill fileName
Client Server Archive
Bentley's Technical Support Group requests that you please confine any comments you have on this Wiki entry to this "Comments or Corrections?" section. THANK YOU!