RefRename.mvba

I'd like to start an open source project on codeplex and develop the  "RefRename.mvba" into a robust solution. There are a number of wrinkles to iron out with this idea, first and foremost is the ownership of the code and if Bentley would allow it. But the real question is if the programmers in the MicroStation Community would be interested in such a project.

So, if you might  be willing to contribute to the solution reply to this post and let’s see if this thing has legs.

Unknown said:

Bentley SELECT-Support ticketno:[8000537120]:

Solution

The example: RefRename.mvba can be downloaded from the following URL:

ftp://ftp.bentley.com/pub/knowledgebase/attachments/RefRename.mvba

This VBA example will change the reference file attachment by using 2 arguments. The 1st argument is the existing reference's full path and file name, the 2nd argument is the new reference's full path and file name.

Steps:

1. Download V BA example "RefRename.mvba"

2. Place application in location pointed by MS_VBASEARCHDIRECTORIES (default is ...\Bentley\Workspace\System\vba\)

3. Start MicroStation (or open command file to be used in Batch processor).

4. Key-in:

vba load RefRename.mvba

vba run [refRename]startapp

Example:

Change the reference name of a file called "testref-01.dgn" to a new name "testref-02.dgn". Both files are in the same folder c:\references\

Key-in:

vba load RefRename.mvba

vba run [refRename]startapp c:\references\testref-01.dgn c:\references\testref-02.dgn

Notes:

-If file contains unresolved (red) references, a runtime error will report. The unresolved references need to be resolved or detached.

-If there is undisplayed references, this will also cause the runtime error, enable "Cache when display off " in reference preferences.

  • Mike,

    I like your idea. Ironically I just found this post as well as the original post and have spent a few hours understanding the original VBA. Being self taught, that might have taken me longer than for others ;-)

    With that being said, I find this VBA to be A) over bloated (unneccesary steps) and B) doesn't work for files within PW. I was able to accomplish the same exact process with these few lines of code below:

    Sub ReplaceRefAttachment()

        Dim oAttachment As Attachment
        Dim oAttachments As attachments
        Dim RefName As String
        Dim OldRefName As String
        Dim NewRefName As String
       
        Set oAttachments = ActiveModelReference.attachments
       
        OldRefName = "PW_WORKDIR:dmsXXXXX\Border-old.dgn"
        NewRefName = "PW_WORKDIR:dmsXXXXX\Border.dgn"
       
        For Each oAttachment In oAttachments
     
            RefName = oAttachment.AttachName
           
            If RefName = OldRefName Then
                Set Attachment = oAttachment.Reattach(NewRefName, Model)
            End If
        Next
      
    End Sub

    Maybe the Reattach Method was introduced after the original VBA was written(2003)? Couple of notes:

    • "PW_WORKDIR: " is absoultely necessary
    • a user would need to know the DMS folder for both the old and new file
    • one could pass the 2 filenames as arguments from the Keyin, as the original VBA was set up to do.

    Hopefully someone else would like to expand on what I just started there, but for now, this gets the job done for me.

  • I made some changes to the above code. Following is the updated code.

    Sub ReplaceRefAttachment()
        Dim oAttachment As Attachment
        Dim oAttachments As Attachments
        Dim RefName As String
        Dim OldRefName As String
        Dim NewRefName As String
        
        Set oAttachments = ActiveModelReference.Attachments
        
        OldRefName = "D:\1000\ABC101\Ref\Border-old.dgn"
        NewRefName = "D:\1000\ABC101\Ref\Border.dgn"
        
        For Each oAttachment In oAttachments
      
            RefName = oAttachment.DesignFile.FullName
                
            If RefName = OldRefName Then
                Debug.Print "Reference File Found..."
                Set Attachment = oAttachment.Reattach(NewRefName, vbNullString)
            End If
        Next
    End Sub

    Regards,
    Leonard Jones.