If... Then... Else to check for a reference file attachment

I have a VBA function that runs when a design file is opened and closed. However, I need to add a IF... Then... Else statement that checks to see if there is a reference file attached when the design file is opened. If the reference file is attached, then turn the reference file display off. Else attach the reference file and turn the display off.

I have a VBA function that attaches the reference file that works perfectly. And the VBA function that runs when a design file is open or closed works great. I just need to get them to work perfectly together.

I cannot seem to get this IF.. THEN... ELSE statement right. Can anyone help?

  • We could help more if you were to post the two bits of code that you want to merge.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • OK. Here is what I have:

    Option Explicit

    Private WithEvents m_OpenCloseHooks As MicroStationDGN.Application

    Private Sub Class_Initialize()

       Set m_OpenCloseHooks = MicroStationDGN.Application

    End Sub

    Private Sub Class_Terminate()

       Set m_OpenCloseHooks = Nothing

    End Sub

    Private Sub m_OpenCloseHooks_OnDesignFileOpened(ByVal DesignFileName As String)

       CadInputQueue.SendCommand "vba run [AttRef_v8]AttRef;REFERENCE DISPLAY YellowPlot OFF"      

    End Sub

    Private Sub m_OpenCloseHooks_OnDesignFileClosed(ByVal DesignFileName As String)

       CadInputQueue.SendCommand "REFERENCE DISPLAY YellowPlot ON"

       CadInputQueue.SendCommand "save design"

    End Sub

    AttRef_v8 is a routine that our customer gave us to use but it is password protected and I don't have access to the code. AttRef_v8 attaches a reference file called YellowPlot to the design file.  I want to insert an IF THEN ELSE statement before AttRef_v8 routine in called. because some design files already have the YellowPlot reference attached and running the AttRef_v8 function attaches another YellowPlot reference. So everytime that design file is opened another reference is attached, which can get out of hand quickly.

    I want the above routine to check if YellowPlot is attached. If it is, just turn off the display of the YellowPlot reference. If it is not attached, then run AttRef_v8 and turn off the display. Can you Help?

  • Try something like this (untested) code …

     ' ---------------------------------------------------------------------
    Public Sub AttachReferenceIfNotAlreadyAttached (ByVal name As String) As Boolean
    Dim oAttachment As Attachment
    Set oAttachment = IsAttached (name)
    If (oAttachment Is Nothing) Then
       '  Attach named file and turn display off
      CadInputQueue.SendCommand "vba run [AttRef_v8]AttRef;REFERENCE DISPLAY " & name & " OFF"
    Else
       '  Turn display off
      oAttachment.DisplayFlag = False
    End Sub
     ' ---------------------------------------------------------------------
    Public Function IsAttached (ByVal name As String) As Attachment
      Set IsAttached = Nothing
      Dim oAttachment As Attachment
      For Each oAttachment in ActiveModelReference.Attachments
        If (0 = StrComp (oAttachment.AttachName, name)) Then
        	Set IsAttached = oAttachment
        	Exit Function
        EndIf
      Next oAttachment
    End Function
    

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions