Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation
  • Product Communities
  • More
MicroStation
MicroStation Wiki Replacing Text with VBA - Part 2: Complex Structures
    • Sign in

    • -MicroStation Wiki
      • +Learning Tips
      • -MicroStation
        • +3D Printing - MicroStation
        • +Animation - MicroStation
        • +Archive / Backup - MicroStation
        • +Base Geometry - MicroStation
        • +Batch Processing - MicroStation
        • +CONNECT Advisor - MicroStation
        • CONNECT Integration - MicroStation
        • +CONNECTION Client
        • +Cells - MicroStation
        • +Change Tracking - MicroStation
        • +Configuration - MicroStation
        • +Coordinate Systems - MicroStation
        • +Curves - MicroStation
        • +Custom Linestyles - MicroStation
        • +DGN - MicroStation
        • +DWG - MicroStation
        • +Database - MicroStation
        • +Detailing Symbols - MicroStation
        • +Dimensions - MicroStation
        • +Documentation & Help - MicroStation
        • +Drawing Aids - MicroStation
        • +Exception - MicroStation
        • +FAQ - MicroStation
        • +Feature Based Solids Modeling - MicroStation
        • +File Access - MicroStation
        • +GUI - MicroStation
        • +General - MicroStation
        • +Graphics Display - MicroStation
        • +Groups - MicroStation
        • +Hardware / Input Devices - MicroStation
        • +Import/Export - MicroStation
        • +Installation - MicroStation
        • +Interchange - MicroStation
        • +Interference - MicroStation
        • +Internationalization - MicroStation
        • +Levels - MicroStation
        • +Licensing - MicroStation
        • +Manipulation - MicroStation
        • +Markup / Redline - MicroStation
        • +Measure - MicroStation
        • +Mesh - MicroStation
        • +Models - MicroStation
        • +Multi-Lines - MicroStation
        • +OLE - MicroStation
        • +Other - MicroStation
        • +Parametrics - MicroStation
        • +Patterning - MicroStation
        • +Point Cloud - MicroStation
        • +Printing - MicroStation
        • -Programming - MicroStation
          • +General - Programming - MicroStation
          • +Macro Recorder - Programming - MicroStation
          • +MDL - Programming - MicroStation
          • -VBA - Programming - MicroStation
            • Automatic execution when opening or closing drawings
            • Automatic Subroutine Loading
            • Automatically generate plots with VBA
            • Avoiding Inaccuracies in VBA Methods - .FacetSolidAsShapes for SmartSolids
            • Bring All Text to a New Level with VBA
            • Browsing References for Specific Items
            • Calculating the range of Rotated Cells
            • Changing Colors of Levels in VBA
            • Changing display priority for references
            • Changing Element Colors from RGB to Indexed Color
            • Changing Layer Colors from RGB to an Indexed Color
            • Changing the Alignment of All Texts with VBA
            • Changing the display order of elements
            • Changing the presentation order of Layers
            • Changing the Transparency Settings of Levels with VBA
            • Cleaning Property Data with VBA
            • COM Server error
            • Create Engineering (HTML) Links in VBA
            • Creating Coordinates as Latitude and Longitude using VBA Lines
            • Creating VBA Levels - 'Level name is duplicate'
            • Deleting Lines of Length 0 Using VBA
            • Errors Attempting to Load VBA Projects
            • Errors while attempting to load VBA
            • Exporting the RGB Values of the Color Table to a Text File in VBA
            • Forms in an .mvba do not open some pc's
            • How to read the RGB values of the Attached Color Table
            • Keyin to load the VBA Project Manager in MicroStation Connect
            • Leaving a VBA Tools Dialog Box Open
            • Linking to VBA Elements with User Attributes
            • New Text Font Cannot be Assigned
            • Placing annotation cells with VBA
            • Print Organizer Control with VBA
            • Printing all Sheet Models to PDF Using VBA
            • Reading the Length of All Arcs
            • Removing All Object Data from Selected Cells in VBA
            • Replacing Points with Circles Using VBA
            • Replacing Text with VBA - Part 1: Introduction
            • Replacing Text with VBA - Part 2: Complex Structures
            • Running a VBA Routine by Keyin
            • +Searching and Evaluating Data in a drawing with VBA
            • Searching and Selecting Text Fields with VBA
            • Updating sequence control with VBA
            • Using a VBA Macro
            • Using the VBA object PropertyHandler to change the Element Information
            • VBA - Interface Error: 0x80040502
            • VBA Error in Execution: Project or Library Not Found
            • What are these "Default.mvba" files?
        • +Project Navigation - MicroStation
        • +Properties - MicroStation
        • +RSS Feeds - MicroStation
        • +Reference - MicroStation
        • +Security - MicroStation
        • +Selection - MicroStation
        • +Settings - MicroStation
        • +Sheet Composition - MicroStation
        • +Solids - MicroStation
        • +Standards - MicroStation
        • +Surfaces - MicroStation
        • +Tables - MicroStation
        • +Text - MicroStation
        • +UI Customization - MicroStation
        • +Units - MicroStation
        • +View - MicroStation
        • +Visualization - MicroStation
        • Welcome Page - MicroStation
        • +Accreditation - MicroStation
        • +i.Models - MicroStation
        • +ProjectWise Integration
        • +Raster - MicroStation
    • +Administration Wiki
    • +Annotations Wiki
    • +Bentley View Wiki
    • +MicroStation PowerDraft
    • +Programming Wiki
    • +Visualization Wiki

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

    Replacing Text with VBA - Part 2: Complex Structures

       
      Applies To 
       
      Product(s): MicroStation
      Version(s): 08.11.09.578
      Environment:  Windows 7 32 bit,Windows 7 64 bit
      Area:  Programming
      Subarea:  VBA
      Original Author: Tristan Anderson, Bentley Technical Support Group
       

     Background Information

    In this second part, the functionality will be expanded. The text elements that have been placed directly in the active model and those who are in text nodes and cells of any depth will be checked.

    Steps

    To carry out the investigation of nested cells, I have created a subroutine called Complex Search. This is a recursive subroutine used for the iteration of the cells.

    In addition, further call parameters are supported, where you can indicate whether or not the replcaement should be performed in the complex elements. With no parameter, this subroutine simply functions the same as the example in the first part with one exception. Text nodes are no processed without the option used.

    Here is an example of how to do this:

    Option Explicit
        ' Searches in text strings and nested cells
     
        Private sToFind As String       ' Find text
        Private sToReplace As String    ' Replace with this text
        Private isComplex As Boolean
     
    Sub TxtRep_complex()
        Dim CmdLine() As String
        Dim Ee As ElementEnumerator
        Dim Sc As New ElementScanCriteria
     
        ' As a separator between parameters, "|" is used.
        CmdLine = Split(KeyinArguments, "|")
        ' Cancel if incorrect parameters are given:
        If UBound(CmdLine) < 1 Then  ' no parameter given
            MessageCenter.AddMessage "Replace Text: Missing Parameters, see details:", "Call was made with: " + KeyinArguments, msdMessageCenterPriorityError
            Exit Sub
        End If
         
        sToFind = Trim(CmdLine(0))      '1. Parameter for search text
        sToReplace = Trim(CmdLine(1))   '2. Parameter for new text     
        ' Check if optional parameter = yes complex was given
        isComplex = False
        If UBound(CmdLine) > 1 Then
            If InStr(CmdLine(2), "complex") > 0 And InStr(CmdLine(2), "yes") > 0 Then isComplex = True
        End If
         
        ' If not looking at cells, only filter texts and text nodes:
        If isComplex = False Then
            Sc.ExcludeAllTypes
            Sc.IncludeType msdElementTypeText
            Sc.IncludeType msdElementTypeTextNode   ' Also text node always search
        Else
            Sc.ExcludeNonGraphical
        End If
         
        Set Ee = ActiveModelReference.Scan(Sc)
        ' Browse current model and start the test routine:
        Do While Ee.MoveNext
            Call complexSearch(Ee.Current)
        Loop
    End Sub
    ' Subroutine for recursively browsing nested complex elements
    Sub complexSearch(oEle As Element)
        Dim EeSub As ElementEnumerator
        ' If a complex element or text node is found, check all sub-elements:
        If (oEle.IsComplexElement And isComplex) Or (oEle.Type = msdElementTypeTextNode) Then
            Set EeSub = oEle.AsComplexElement.GetSubElements
            Do While EeSub.MoveNext
                Call complexSearch(EeSub.Current)
            Loop
        ' Otherwise string compare, if a text is present:
        Else
            If oEle.Type = msdElementTypeText Then
                If oEle.AsTextElement.Text = sToFind Then
                   oEle.AsTextElement.Text = sToReplace
                   oEle.Rewrite
                End If
            End If
        End If
    End Sub

    The call is made in this example about the subroutine TxtRep_complex, which can be called as in the previous example with 2 parameters:

    vba run TxtRep_complex Findtext

    The optional parameter Complex = yes specifies whether you want to search for text in the complex structures or not:

    vba run TxtRep_complex Findtext | Replacetext | [complex=yes]

    These 3 variables:

    Private sToFind As String ' Find Text to replace
    Private sToReplace As String ' Replace found text with this
    Private isComplex As Boolean

    have been declared outside the routine as private, so they are valid in all routines of this module and do not need to be entered when calling routines as parameters.

    << RETURN TO PART 1 <<

    • VBAWindows 7 32 bit
    • MicroStation
    • Windows 7 64 bit
    • Programming
    • en
    • 08.11.09.578
    • SELECTsupport
    • Share
    • History
    • More
    • Cancel
    • Tristan Anderson Created by Bentley Colleague Tristan Anderson
    • When: Fri, Jul 10 2015 10:46 AM
    • Carl Myhill Last revision by Bentley Colleague Carl Myhill
    • When: Tue, Feb 16 2016 4:28 AM
    • Revisions: 3
    • 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

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