By Bentley Technical Support Group05 December 2007 Modified: 07 December 2007
This Client Server article is republished in its entirety from 2007 for reference purposes.
This article is a continuation of the December 2, 2004, Client Server article entitled "Using VBA To Programmatically Export A VBA Project."
After exporting all the VBA components and references, you can also create a VBA that will import them into a new VBA project. This article explains how to use the VBA UserForm to design an interface that will allow the end-user to specify the path in which the components and references are located. It will also allow you to specify a name of the new project into which you want to import them.
The first step is to design a UserForm so that the end-user will be able to enter this information:
Image of UserForm
First, right-click the Project in the Project dialog box and select Insert > UserForm. You can rename the UserForm in the Properties dialog of the UserForm and name it ProjectCreate (or whatever name you like). Then add two textboxes and two pushbuttons. You can also add two labels above the textboxes explaining what you expect the end-user to use them for input. The next step is to select one of the pushbuttons and rename it "Create," then select the other one and name it "Cancel." After these steps, your UserForm should be finished.
The next step is to double-click the first textbox that you placed and enter the following code to assign what the end-user enters into it to the global variable destPath that you defined in your module:
Private Sub TextBox1_Change()
'Assign path of components.
destPath = TextBox1.Text
Do the same for the second textbox as well. The only difference is that you assign the value to the variable projName:
Private Sub TextBox2_Change()
'Assign name of new project.
projName = TextBox2.Text
After you finish setting the textbox events, put code behind the two pushbuttons. Start with the easy one. Double -click the pushbutton that you named Cancel. Basically, you want to put code behind it to end the program:
Private Sub CommandButton2_Click()
'If hit Cancel, end program.
The Create button is the meat of the code you are implementing. First define all the needed variables for writing the code (i.e. - VBE object, objects for the components and references, etc.) and set the VBE object:
Private Sub CommandButton1_Click()
Dim oVBE As VBE
Dim oProject As VBProject
Dim oComps As VBComponents
Dim oComp As VBComponent
Dim oRefs As References
Dim oRef As Reference
Dim currFile As String
Dim Cut As Variant
Dim Ext As String
Dim MyString As String
Dim count As Integer
Set oVBE = Application.VBE
Next, create the new VBA project using the name that the end-user entered into the textbox. Then loop through each project that is loaded until the one just created is found:
'Create the new project.
CadInputQueue.SendKeyin "vba create " + destPath + projName + ".mvba"
'Loop through projects until find the new one.
For Each oProject In VBE.VBProjects
When the project is finished, open the text file that contains all the reference information (this was created in the earlier article) and loop through all the references to add them to the new project. Also, perform a safety check to make sure the current reference is not already there. At the end of the process, close and delete the text file:
If oProject.Name = projName Then
'Open data file to read names of references.
Open "BLOCKED SCRIPTvoid(null);" For Input As #1
'Set reference counter.
count = 1
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString ' Read data into variables.
'Get current attached references to see if already attached.
Set oRefs = oProject.References
For Each oRef In oRefs
'If ref attached matches ref from datafile, move onto next ref in datafile.
If oRef.FullPath = MyString Then
'If counter reaches max and none match, add the reference.
ElseIf count = oRefs.count Then
'Add one to counter.
count = count + 1
Close #1 ' Close file.
Kill ("BLOCKED SCRIPTvoid(null);") 'Delete datafile.
After adding the references needed to run the VBA project, delete the Default module that comes with the newly created VBA, since it is not needed:
'Small routine to delete the default Module1 that gets added to a new project.
Set oComps = oProject.VBComponents
For Each oComp In oComps
Finally, import the VBA components (.bas, .cls, .frm) that are located in the path that the end-user entered into the textbox. The following routine loops through all the components and imports them. After importing each component, it deletes the component in the specified path so the end-user will not have to manually delete it. Also, don't forget to end the subroutine:
'Loop until done list.
Do While currFile <> "" ' Start the loop.
'Parse path and file into array.
Cut = Split(currFile, ".")
'Get extension of file.
Ext = Cut(UBound(Cut)) 'the value
'If matches any of the case statement, insert as that component.
Select Case Ext
oProject.VBComponents.Import (destPath + currFile) 'Import component.
Kill (destPath + currFile) 'Delete component(cleanup).
currFile = Dir ' Get next entry.
Now you have a VBA application that will create a new VBA project and import the components and references that were exported from the utility explained in the previous article. A completed version of this VBA is available here.
Client Server Archive
ProjectWise TechNotes And FAQs
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!