[V8i VBA] Passing more than one KeyinArgument?

I found this discussion: https://communities.bentley.com/products/programming/microstation_programming/f/microstation-programming---forum/120244/v8i-vba-pass-command-arguments-to-macro which led me to the KeyinArguments property. But I'd like a little clarification or help with this.

I am looking into an MVBA that I can run through the batch process to replace a certain cell in a number of files. I'd like to pass OldCellName and NewCellName as KeyinArguments but I'm not sure I'm getting the syntax right, and I'm not sure how to pass "verbose" information

My command string for the batch process would look like

vba run RepCellVBA OldCellName NewCellName

From the Help file, it tells me I can use the Split function to break the argument "OldCellName NewCellName" into two separate strings. That will be fine if the cell names are a single word. But some of our cell names are phrases, instead of words (like "IN RW Left"). How does KeyinArguments handle phrases? Can I just use quote marks to delineate the different phrases:

vba run RepCellVBA "Old Cell Name" "New Cell Name"

and will KeyinArguments respect that those are two phrases (not six words)?

Of course, if someone has a macro that works already, or can point me to the keyin sequence to allow me to run replace cell through the batch process, it might save me the extra effort, and would be appreciated mightily!

Thank you for your assistance!
MaryB
MSTN 08.11.09.829

  • Hi Mary,

    but I'm not sure I'm getting the syntax right

    Whatever syntax you will use in your macro is up to you. But it's always wise to follow common patterns and practices, so to use a space as arguments delimiter is the best option.

    That will be fine if the cell names are a single word. But some of our cell names are phrases

    Yes, the space(s) in arguments (e.g. cell names) always brings extra complexity ;-)

    To use quotations to "merge" more words to one argument is generally used approach.

    How does KeyinArguments handle phrases? Can I just use quote marks to delineate the different phrases:

    When you check KeyinArguments property description in VBA help, you will see that it's String, so whatever you will enter as the macro arguments, will be passed as one string. MicroStation do not care about the string syntax and content.

    and will KeyinArguments respect that those are two phrases (not six words)?

    No, it will be one string. What is inside the string is the macro responsibility, not MicroStation or VBA engine.

    or can point me to the keyin sequence to allow me to run replace cell through the batch process

    You have to implement own parser, which in VBA is not great thing, but it can be done. Especially when you can fix the syntax by yourself, so it's not about to implement general parser, which can be pretty huge task.

    You can simplify the implementation when you will define that both arguments have to be enclosed in quotations always, despite of they contain spaces or not. In such case the algorithm can be something:

    1. Iterate the string char by char
    2. When there will be first quotation, start to record 1st argument
    3. Next quotation mark ends the recording
    4. 3rd quotation mark starts recording of 2nd argument
    5. 4th quotation mark ends the recording

    Another benefit of such strict simple syntax is you don't have to take care about spaces (sometimes a delimiter can be one space, sometimes more etc.).

    With regards,

      Jan

  • Nice - that answers my question very concisely.
    Now to refresh my memory on string functions.

    Thank you!

    MaryB

    Power GeoPak 08.11.09.918
    Power InRoads 08.11.09.918
    OpenRoads Designer 2021 R2

        

  • Now to refresh my memory on string functions

    The string functions in VBA are pretty basic.

    This suggestion might be a step too far, but have you considered Regular Expressions (RegEx)?  You can use a RegEx in VBA by  referencing Microsoft VBScript Regular Expressions 5.5This article tells you more.

    This example from StackOverflow provides a RegEx for parsing quoted strings.  And here's an online regex tester and debugger, which I've found very handy.

     
    Regards, Jon Summers
    LA Solutions

  • For the split function I have used a "," delimeter in MVBAs in the past which would allow you to have strings with spaces in your keyin argument.
    The syntax for the split function is Split( Expression, [Delimiter], [Limit], [Compare] )

    docs.microsoft.com/.../split-function

    For example MACRO VBA RUN [VBA_Project_Name]<MacroName>.<Procedure> Keyin
    Where Keyin might be something like "Test Arg1,Test Arg2"
    Sample code which may help you is below;

    Sub TestKeyinCommand()

        Dim sKeyinString() As String
        Dim sArgument1 As String
        Dim sArgument2 As String

        'For testing purposes the following line is uncommented
        'Comment it out if running the MACRO VBA RUN [VBA_Project_Name]<MacroName>.<Procedure> Keyin
        KeyinArguments = "Old Cell Name,New Cell Name"

        sKeyinString = Split(KeyinArguments, ",", -1, vbTextCompare)
        sArgument1 = sKeyinString(0)
        Debug.Print "1st Keyin Argument is '" & sArgument1 & "'"
        sArgument2 = sKeyinString(1)
        Debug.Print "2nd Keyin Argument is '" & sArgument2 & "'"

    End Sub

  • Yep, that's it! The delimiter makes all the difference

    MaryB

    Power GeoPak 08.11.09.918
    Power InRoads 08.11.09.918
    OpenRoads Designer 2021 R2

        

  • KeyinArguments = "Old Cell Name,New Cell Name"

        sKeyinString = Split(KeyinArguments, ",", -1, vbTextCompare)
        sArgument1 = sKeyinString(0)
        Debug.Print "1st Keyin Argument is '" & sArgument1 & "'"
        sArgument2 = sKeyinString(1)
        Debug.Print "2nd Keyin Argument is '" & sArgument2 & "'"

    Similar to what I do, but I add  a Trim$() function to get rid of any leading or trailing spaces between arguments in case someone puts in a space after the comma delimiter.

    KeyinArguments = "Old Cell Name, New Cell Name"

    The Trim$() function will take care of that.

    sArgument1 = Trim$(sKeyinString(0))
    sArgument2 = Trim$(sKeyinString(1))

    Rod Wing
    Senior Systems Analyst