Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation
  • Product Communities
  • More
MicroStation
MicroStation Wiki Using VBA To Programmatically Import A VBA Projects Components And References [CS]
    • Sign in

    • +MicroStation Wiki
    • +Administration Wiki
    • +Annotations Wiki
    • +Bentley View Wiki
    • +MicroStation PowerDraft
    • -Programming Wiki
      • A class to help create and modify text element
      • A Complete Example
      • A MicroStation VBA Example With Bentley ProjectWise
      • AddIn Development Using VB.NET
      • C# .NET Template with IPrimitiveCommandEvents Class
      • Capturing Graphics in Dynamic Views
      • Compiling MDL Applications
      • Database Manipulation
      • Debugging Native Code MDL Applications
      • Developing Code in VBA
      • Developing MicroStation Applications For DWG Files
      • Drag and Drop in MicroStation
      • Error: "Cannot save changes to VBA project 'Support' because it is read-only"
      • Getting And Setting Elements Using Graphic Groups In VBA [CS]
      • Getting Started with Visual Basic
      • How To Write A Recursive Routine In MicroStation VBA [CS]
      • Introducing Segment3D Methods In MicroStation V8 2004 Edition
      • Known issues in MDL and MicroStationAPI
      • Launching VBA Applications As Initapps Or Dgnapps [CS]
      • Learning MicroStation Addins Step by Step
      • MDL - Getting Started With XAttributes In MicroStation V8 XM Edition
      • MDL - Native Code Application Development
      • MDL Or MicroStation BASIC Choosing The Right Tool [TN]
      • MFC Dialog And Native Window Support
      • Microsoft Office VBA Patch Utility
      • MicroStation BASIC FAQ
      • MicroStation BASIC Limits [FAQ]
      • MicroStation Developer Documentation and Example Code
      • MicroStation Programming Advice
      • MicroStation SDK
      • MicroStation V8 Programming Tools Readme
      • MicroStation V8 VBA Programming Resources [CS]
      • MicroStation V8 XM Edition View Code Updates
      • MicroStation VBA Resources Revisited [CS]
      • Migrating Dimension Code To MicroStation V8
      • Migrating MDL Applications To Native Code
      • Mouse Wheel Events And The Visual Basic 6.0 IDE
      • Porting MDL Applications To MicroStation V8
      • Reading Elements From A Microsoft Access Database With VBA [CS]
      • Running MDL Applications
      • Scanning For MicroStation V8 Elements In VBA [CS]
      • Unleash A Workspace Wizard Automating Workspace Creation With MicroStation V8 And VBA [CS]
      • Using VBA To Detect The Current Or Last Used Command
      • Using VBA To Programmatically Export A VBA Project [CS]
      • Using VBA To Programmatically Import A VBA Projects Components And References [CS]
      • VBA -- Creating A Complex Application
      • VBA Interface Error: failed creating the comp manager - 0x80040583
      • VBA interface error: unable to get IDE
      • vba recording
      • Working With Levels In MicroStation VBA [CS]
      • Writing An MDL Application To Output Custom Placemarks To Google Earth
      • [V8i C++] PointCloud IPointCloudChannelDisplayHandler
    • +Visualization Wiki

     
     Questions about this article, topic, or product? Click here. 

    Using VBA To Programmatically Import A VBA Projects Components And References [CS]


     

    By Bentley Technical Support Group
    05 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.

    Import VBA Projects

    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

    End Sub

     

    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

    End Sub

     

    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.

    End

    End Sub

     

    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

    'Reset counter.

    count = 1

    Exit For

    'If counter reaches max and none match, add the reference.

    ElseIf count = oRefs.count Then

    oProject.References.AddFromFile MyString

    'Reset counter.

    count = 1

     

    End If

    'Add one to counter.

    count = count + 1

    Next

     

    Loop

     

    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

    oProject.VBComponents.Remove oComp

    Next

     

    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

    Case "cls"

    oProject.VBComponents.Import (destPath + currFile) 'Import component.

    Kill (destPath + currFile) 'Delete component(cleanup).

    Case "frm"

    oProject.VBComponents.Import (destPath + currFile) 'Import component.

    Kill (destPath + currFile) 'Delete component(cleanup).

    Case "bas"

    oProject.VBComponents.Import (destPath + currFile) 'Import component.

    Kill (destPath + currFile) 'Delete component(cleanup).

    Case "frx"

    Kill (destPath + currFile) 'Delete component(cleanup).

    End Select

     

    currFile = Dir ' Get next entry.

    Loop

    End If

    Next

     

    End Sub

     

    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.

    See Also

    Client Server Archive

    ProjectWise TechNotes And FAQs

    Comments or Corrections?

    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!

     

     

    • client server 2007
    • Archived Client Server
    • client server
    • Share
    • History
    • More
    • Cancel
    • Elisa McGraw Created by Elisa McGraw
    • When: Wed, Jul 22 2009 3:06 PM
    • Dan Koval Last revision by Bentley Colleague Dan Koval
    • When: Mon, Aug 26 2013 9:47 AM
    • Revisions: 13
    • Comments: 0
    Recommended
    Related
    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2021 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies