if-then-else w/timer

4:06 AM 3/10/2019                            Help Please
Anyone who knows the more complex VBA ?

When I close a file, I have several things I have to do, they are now all done by the VBA. I was able to
figure out. This Timer and the If-then-else seem to be way above my ability.
I now have to remember to detach reference files before sending them in, but when I open and close the .dgn file while making
changes I do not want to detach the reference then. Only upon completion of the .dgn. when I close for the last time
before sending in my work.
This is the simple Macro that detaches the reference file:
Sub XrefDet() 'the () is the ( and ) but it looks funny
' 031019 - Detach Reference file
CadInputQueue.SendCommand "Reference Detach All"

End Sub

So I wanted to have an If-then-else that would time out in say 4-8seconds and it would equal a no
or a strike say space bar and it = no, but you could put Y if you wanted it to detach. I am attaching the
timer from the help file. I could not get to work myself, but this is it.

This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
PauseTime = 5 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
TotalTime = Finish - Start ' Calculate total time.
MsgBox "Paused for " & TotalTime & " seconds"
Else
End
End If

I also looked up the if-then-else and it is also attached.
This example shows both the block and single-line forms of the If...Then...Else statement. It also illustrates the use of If TypeOf...Then...Else.

Dim Number, Digits, MyString
Number = 53 ' Initialize variable.
If Number < 10 Then
Digits = 1
ElseIf Number < 100 Then
' Condition evaluates to True so the next statement is executed.
Digits = 2
Else
Digits = 3
End If

' Assign a value using the single-line form of syntax.
If Digits = 1 Then MyString = "One" Else MyString = "More than one"

Use If TypeOf construct to determine whether the Control passed into a procedure is a text box.

Sub ControlProcessor(MyControl As Control)
If TypeOf MyControl Is CommandButton Then
Debug.Print "You passed in a " & TypeName(MyControl)
ElseIf TypeOf MyControl Is CheckBox Then
Debug.Print "You passed in a " & TypeName(MyControl)
ElseIf TypeOf MyControl Is TextBox Then
Debug.Print "You passed in a " & TypeName(MyControl)
End If
End Sub

These are beyond my ability to under stand, I have tried on my own and spent a number of hours trying to
get something even simple and than build from there. Any Help ? All I can say is thank you and
offer some money for the help.

Parents
  • So if I understand the above correctly, all you want to do is give a user that choice to detach all reference files upon exiting a drawing?

  • Yes, Sir I do not always want the Reference files detached. Had hoped to have the program ask Detach Reference Files and I could say Y or N

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • Special big thank you I finally figured out how to get Autorun.mvba to go out and run you file It always ask's now when closing a .dgn I commented out the one in

    autorun and have yours loading must be running because when I close the files if they have a reference it asks

    I so appreciate the help.

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • This super file you shared will it do something when opening a .dgn also? If so perhaps I could use this instead of the Autorun.mvba because the only thing I added to it was the lines Jon Summers was kind enough to provide. I have never done anything in VBA except macros. So forgive me if I get confused on the bigger programs like the Autorun and the one you gave me. I thought I could save loading an extra  file. this is how I am getting it to work now making all three auto-load 

    '------------------------------------------------------------
    'OnDesignFileOpened(ByVal DesignFileName As String)
    ' This method will be called each time a design file is opened.
    ' The DesignFileName is the name of the file that was just opened,
    '  this is useful if you perform some sort of filtering based on file
    '  name or extension.
    Private Sub m_OpenCloseHooks_OnDesignFileOpened(ByVal DesignFileName As String)
       
        
    ' 011819 - Added Key-in command
        ' Place code you wish to execute each time a file is opened here.
        ' You could do all sorts of things here, but use caution as this is run
        ' for every file you open as long as this class is active.
        ' For this example, we will close all views except for View 1
        ' and perform a Fit All. We will also use familiar MBE style commands
        ' for those trying to transition from MBE.
        
       '[A] - Personal Section Added Commands
        'CadInputQueue.SendCommand "View Off All"    'Turn off all views
        'CadInputQueue.SendCommand "View On 1"       'Turn View 1 on
        'CadInputQueue.SendCommand "Window Tile"     'Resize View 1 to fit screen
        'CadInputQueue.SendCommand "Fit All"         'Start Fit All command
        'CadInputQueue.SendCommand "Selview 1"       'Apply to View 1
        ' 011619 - The VBA RUN ViewAttributON also runs the AttribOFF section subroutine
        '{Note} Changing to comment for testing
        'CadInputQueue.SendKeyin "vba run ViewAttribON"      ' Turns on Attributes Off Attributes and Runs AA= & As= settings
        ' {Note} Changing to comment for testing
        
        '[B] - 052619 Modified Colors are messing up ________________________________________________________ RJB
         Dim KeyIn As String
        '[AA] 052219 - Runs macro in Module6 Set Settings Design File Preferences
        KeyIn = "VBA RUN [Default]Module6.SetSDFpreferences"         ' 051319 - Testing adding a dim command"
        CadInputQueue.SendKeyin KeyIn
        
        ' RemoveModalDialogEventsHandler modalHandler
        ' As stated before, you can perform all kinds of things here.
    End Sub
    

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • I do not know what is contained in your Autorun.mvba but the answer is yes any code you wish to run when opening a file can be copied into the MVBA file which is giving you the option for detaching references. I do not have an AutoRun.mvba in my VBA Project Manager though I have seen it in images before but I don't know it its a supplied Bentley file or not.

  • I was given a link to it in Bentley, but I believe If I can figure how to put something in yours that runs when I open a .dgn

    I will just rename it AutoRun.MVBA after I am sure how to put the commands in it. I am going to attach the code I added to the Autorun and perhaps you can tell me where to place it in your program file. I sure do appreciate it and it works perfect. I can press Y or N I can click on OK or cancel and it does what I was so wanting to learn. At 74 running out of time to learn alot mind is going faster that I want ((((c;

    this is the section I put the command in or code that Jon Summers put me in the right direction. I have modules inside my RJBP.MVBA Module 6 is the files I wanted run at opening and they all tested and worked except the one on the color Settings>Design File>Color . If they come to me with both top and bottom colors the same the VBA commands do not change them to different. They stay locked. If it was Yel-Yel, and you ask blue red it becomes blu-blu, or if you want green-magenta you get green-green

    if you manually change either color to anything different

    then all the color change commands work fine. So I have resolved to do manual when I get one. I even tried splitting it into 2 macros 1 command in each Haha Nope. Ok code coming up, got to get to Dr's

    '------------------------------------------------------------
    'Private Sub Class_Terminate()
    ' This is executed when an instance of this class is destroyed
    Private Sub Class_Terminate()
      'Remove the existing reference, if any, to the current MicroStation session
      Set m_OpenCloseHooks = Nothing
    End Sub
    
    '------------------------------------------------------------
    'OnDesignFileOpened(ByVal DesignFileName As String)
    ' This method will be called each time a design file is opened.
    ' The DesignFileName is the name of the file that was just opened,
    '  this is useful if you perform some sort of filtering based on file
    '  name or extension.
    Private Sub m_OpenCloseHooks_OnDesignFileOpened(ByVal DesignFileName As String)
       
        
    ' 011819 - Added Key-in command
        ' Place code you wish to execute each time a file is opened here.
        ' You could do all sorts of things here, but use caution as this is run
        ' for every file you open as long as this class is active.
        ' For this example, we will close all views except for View 1
        ' and perform a Fit All. We will also use familiar MBE style commands
        ' for those trying to transition from MBE.
        
       '[A] - Personal Section Added Commands
        'CadInputQueue.SendCommand "View Off All"    'Turn off all views
        'CadInputQueue.SendCommand "View On 1"       'Turn View 1 on
        'CadInputQueue.SendCommand "Window Tile"     'Resize View 1 to fit screen
        'CadInputQueue.SendCommand "Fit All"         'Start Fit All command
        'CadInputQueue.SendCommand "Selview 1"       'Apply to View 1
        ' 011619 - The VBA RUN ViewAttributON also runs the AttribOFF section subroutine
        '{Note} Changing to comment for testing
        'CadInputQueue.SendKeyin "vba run ViewAttribON"      ' Turns on Attributes Off Attributes and Runs AA= & As= settings
        ' {Note} Changing to comment for testing
        
        '[B] - 052619 Modified Colors are messing up ________________________________________________________ RJB
         Dim KeyIn As String
        '[AA] 052219 - Runs macro in Module6 Set Settings Design File Preferences
        KeyIn = "VBA RUN [Default]Module6.SetSDFpreferences"         ' 051319 - Testing adding a dim command"
        CadInputQueue.SendKeyin KeyIn
        
        ' RemoveModalDialogEventsHandler modalHandler
        ' As stated before, you can perform all kinds of things here.
    End Sub
    
    060319 - I had commented out all the previous one's when I found I could 
    just run them direct from the ohter file. I hope this is not to confusing. 
    Gotta Run
    

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • I don't know if [AutoRun] is a supplied Bentley file

    The name AutoRun may seem apposite, but in fact any VBA project will run automatically given the right conditions.  The conditions are very simple: the project must contain a module having subroutines OnProjectLoad and OnProjectUnload.  See Overview of MicroStation Events in VBA help.

    AFAIK a file named AutoRun.mvba is not supplied with MicroStation.

     
    Regards, Jon Summers
    LA Solutions

  • I do want to say thank you so much for the help. With what jon told me I was able to call the macro from within the Autorun and then Barry when I printed out your helpful program and followed the two AutoRun and Your file Detach ref's I was able to copy the section I needed to complete the task. Thank you so much. 

    PS: Thanks Jon Summers, have already been reading some goggle's on the Excel VBA so far they are not showing the Dim commands, something about Excel has something built in, But thank you I am also checking on the MS Word same thing IF-THEN-ELSE. And a thank you to Jan who has given me so many tips in the past.

    Private Sub m_OpenCloseHooks_OnDesignFileClosed(ByVal DesignFileName As String)
        Detach_Refs_Query
    End Sub
    Private Sub Detach_Refs_Query()
    
    Dim RefQuery As Integer
    Dim nAttachments As Integer
    
        'Determine if there are any references attached,
        nAttachments = ActiveModelReference.Attachments.Count
    
        'Proceed with messagebox if no at least 1 reference files is attached, otherwise bypass and exit without prompt
        If Not nAttachments = 0 Then
    
            RefQuery = MsgBox("Now closing file" & ActiveDesignFile.Name & vbNewLine & "Do you wish to detach all Reference Files?", vbYesNo + vbQuestion, "RP Detach Refs Query")
    
        'Detach all references if the user presses Yes
            If RefQuery = vbYes Then
    
                Dim i As Integer
                For i = nAttachments To 1 Step -1
                    ActiveModelReference.Attachments.Remove i
                Next i
                RedrawAllViews
                Exit Sub
            Else
        'Do nothing and exit without detaching reference attachments
            End If
        End If
    End Sub
    
    

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • FWIW, the AutoRun.mvba being mentioned is the example provided on Ask Inga that I posted, wow, over 10 years ago now. Still perfectly relevant though.

    https://communities.bentley.com/communities/other_communities/askinga/w/askinga/1109/run-a-vba-macro-when-opening-or-closing-design-files

    Hopefully that clarifies some of the confusion for future readers of this thread.

    Gerald

  • Barry 2 questions, I have been told do not keep closing a file, just load another. New draw back if I fail to close the file it never asks about the cross ref files. If I close it does fine

    any change that would correct that?

    My second question on this same subject will keep so as to  only be dealing with 1 at a time. I am going to look at the code and see if I can see a way (Hahaha) I am going to try

    I so appreciate your major assistance on this . I am close to a complete winner.

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

  • Not sure I fully understand the what you are saying, Richard.

    "do not keep closing a file, just load another" sounds strange and could mean either of 2 things

    1. Keep the existing session/instance of MicroStation with a specific drawing open, and open a new session/instance of MicroStation for another drawing, or
    2. Alternatively it could mean do not use the close command on the File menu (to return back to the MicroStationManager) and instead use the Open command to load another DGN file.

    The latter is logical and the way I would expect everyone to use MicroStation (unless there is a need to change Projects) however programatically (in relation to the MVBA file I uploaded) there is no difference using File>Close>Open DGN file from MicroStation Manager compared to File>Open i.e. the OnDesignFileClosed events are triggered in both instances so you should be seeing the dialog box if you have any references attached to the currently open DGN file.

    If I've misunderstood, you will need to provide a clearer example of what the issue is.

  • Barry, It works just perfect. Every file I put a xref in it will ask. I think where I got confused(easy for me) was it did not always seem to ask. I was trying to check between file open and or File Close. I use the close a lot to check that all the settings to see if everything is still correct.

    Version: MicroStation V8i SS 10

    RJB Phillips III (Richard) Praise the Lord for His Mercy and grace in Christ Jesus

Reply Children
No Data