VBA to select and copy specific reference files (MicroStation 2004)

In respect to Set oAttachments = ActiveModelReference.Attachments, how do you select a specific reference file with this command? I want to copy/use the clipping properties of an existing reference file. Also at a later date, I would like to pick specific reference files to turn on or off levels. Looking through all the threads, I can only find mass reference file manipulation. The code for clipping to a shape looks quite complicated.

 Thank you

Parents
  • Hi,

    I'm in v8i, so I can't verify stuff for 2004, but here's a good start:

    Dim oAttachment as Attachement
    ...
    For Each Attachment in ActiveModelReference.Attachments
        you can get: Attachment.Name, Attachment.Number, Attachment.Description, Attachment.Path (verify term with help)

    Then you can use this information to choose: ActiveModelReference.Attachments(Name or Number) to manipulate.

    --Robert

  • Hello Robert,

    I'm somewhat confused. I know what reference file I want to copy. Do I need to run the For Each statement so the code will recognize the file given or is it just for information?

    When I:

    Set oAttachments = ActiveModelReference.Attachments(copyRefFileName) 'copyRefFileName is referenced from an Excel cell

    it gives me an error ActiveModelRefen...=<This operation cannot be completed. Possible cause:...

     

    This is the code i'm trying 

    Dim oAttachments As Attachments
    Set oAttachments = ActiveModelReference.Attachments(copyRefFileName)
    Copy oAttachments
    CadInputMessage.Keyin , dx = 0, 0
    CadInputMessage.Keyin , dx = 0, 0
    ' Reference a model from another file
    Dim oAttachment As Attachment
    Set oAttachment = oAttachments.AddCoincident(refFileName, modelName, LogicalName, Description)

  • One Attachment — Many Attachments

    klauden:
    
    Dim oAttachments As Attachments
    Set oAttachments = ActiveModelReference.Attachments(copyRefFileName)
    
    

    ActiveModelReference.Attachments is plural — there may be many attachments. You want just one attachment. Try this …

    
    Dim oAttachments As Attachments
    Set oAttachments = ActiveModelReference.Attachments(copyRefFileName)
    Dim oAttachment As Attachment
    Set oAttachment = Attachments (copyRefFileName)
    
    
    klauden:
    Copy oAttachments

    It's not clear what you expect that line to do. When you copy something, you need both a source and a destination.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Hi,

    In the vba editor when I type in ActiveModelReference.Attachments(, it gives me the hint that I need to enter the name or number of the attachment.  Please note that the name is not the file name or the logical name.  So if you are trying to use the reference file name to find your attachment you may need to loop through the reference files until you find the correct attachement (attachment.filename = xxx).  Once you get the attachment number, then you can continue doing what you need to.

    (Again, please note that I'm in v8i SS1, not 2004.  I don't know what has and has not changed.)

    --Robert

  • Okay guys,

    Robert must be correct in that the name being asked for by ActiveModelReference.Attachments is not the file name. I found that using:

    Set oAttachments = ActiveModelReference.Attachments.FindByLogicalName("copyRefFileName") works until I get to trying to reattach it.

    The new code as is follows:

    Dim oAttachments As Attachments
    Set oAttachments = ActiveModelReference.Attachments.FindByLogicalName("copyRefFileName")
    Dim oAttachment As Attachment
    Set oAttachment = oAttachments
    Set oAttachment = oAttachments.AddCoincident(refFileName, modelName, LogicalName, Description)

    I tried Set oAttachment = oAttachments.FindByLogicalName("copyRefFileName") with no luck...

    It's failing at the last line of code included.

    Run-time error '91'  Objct variable or With block variable not set

    Any ideas?

    I think I am so close to getting this to run I can smell it.

    Thanks!

  • Hi,

    Remember that the command is "Find By Logical Name".  So when you add your new attachment, make sure you specify a logical name.  (if you're worried about conflicts, you could put the date or time into the logical name.)  Then you can use this logical name to find the correct attachment.  

    --Robert

  • Hello Robert,

    Yes I have a cell with a new logical name and a cell for the description for the reattached file. It seems to drop the link for the given file name or number when I set:

    Set oAttachment = oAttachments

    It seems I need a wildcard statement or something to retain the value of the given file name or number in:

    Set oAttachments = ActiveModelReference.Attachments.FindByLogicalName("copyRefFileName")

    Is there such animal?

    Thank you!

  • Hi,

    I would expect to do something like this:

    Sub TestCopy()
         Dim NewAttachment As Attachment
         Set NewAttachment = CopyRefFile("aa", "bb")
         MsgBox "logical name = " & NewAttachment.LogicalName & " file name = " & NewAttachment.DesignFile.FullName
    End Sub

    Function CopyRefFile(OriginalLogicalName As String, NewLogicalName As String) As Attachment
       Dim oAttachment As Attachment
       Dim oAttachment2 As Attachment
       Dim oFullName As String
       Dim oDescription As String
       Set oAttachment = ActiveModelReference.Attachments.FindByLogicalName(OriginalLogicalName)
       oFullName = oAttachment.DesignFile.FullName
       oDescription = "New " & oAttachment.Description
       Set oAttachment2 = ActiveModelReference.Attachments.AddCoincident(oFullName, vbNullString, NewLogicalName, oDescription)
       Set CopyRefFile = oAttachment2
    End Function

    --Robert
  • Thanks Robert!

    Still having issues mainly because the code is in a loop, I condensed it down to this:

    Dim oAttachment As Attachment

    Set oAttachment = ActiveModelReference.Attachments.FindByLogicalName(copyRefFileName).Copy

    oAttachment.Reattach refFileName, refModelName

    Now I'm just down to changing the reattached file's (refFileName) logicalname and description. Can't this be done in the last string while reattaching?

    Thank you!

Reply
  • Thanks Robert!

    Still having issues mainly because the code is in a loop, I condensed it down to this:

    Dim oAttachment As Attachment

    Set oAttachment = ActiveModelReference.Attachments.FindByLogicalName(copyRefFileName).Copy

    oAttachment.Reattach refFileName, refModelName

    Now I'm just down to changing the reattached file's (refFileName) logicalname and description. Can't this be done in the last string while reattaching?

    Thank you!

Children