Change AttachName

As mentioned in my previous topic I'm working on macro which involves changing the attach paths of the Reference files. I believe its almost there but I can't quite get it to work as desired. I've been using the "Changing the attachname" example from the VBA help as a guide. The purpose of the macro is to save what I consider to be an annoying procedure of attaching reference files via configuration variables (which is my firm's way of doing things) and instead attach files normally but have my macro detect which folder in our standard project structure that the attachment resides and amend the attachname with the correct custom variable. To achieve this I have added around 150 lines of custom variable and constant declarations for each possible folder with the aforementioned folder structure.

I'll post a small amount of the code that show's where I've gotten to:

Option Explicit

'VARIABLE DECLARATIONS
Dim Att As Attachment
Dim BLPROJECT_NO As String
Dim BL3D As String
Dim BLREFS As String

'CONSTANT DECLARATIONS

Const str_BLPROJECT_NO As String = "$(BLPROJECT_NO)"
Const str_BL3D As String = "$(BLPROJECT_NO)Live/3d/"
Const str_BLREFS As String = "$(BLPROJECT_NO)refs/"

Public Sub UpdateRefs()
BLPROJECT_NO = ActiveWorkspace.ExpandConfigurationVariable(str_BLPROJECT_NO)
BL3D = ActiveWorkspace.ExpandConfigurationVariable(str_BL3D)
BLREFS = ActiveWorkspace.ExpandConfigurationVariable(str_BLREFS)

Dim strAttachName As String
Dim strAttachPath As String
Dim position As Long

For Each Att In ActiveModelReference.Attachments
strAttachName = Att.AttachName
strAttachPath = Att.DesignFile.Path & "/"

If strAttachName = BLPROJECT_NO Then
strAttachName = "BLPROJECT_NO:" & strAttachName
Att.SetAttachNameDeferred strAttachName
Att.Rewrite

ElseIf strAttachPath = BL3D Then
strAttachName = "BL3D:" & strAttachName
Att.SetAttachNameDeferred strAttachName
Att.Rewrite

Else strAttachName = BLREFS
strAttachName = "BLREFS:" & strAttachName
Att.SetAttachNameDeferred strAttachName
Att.Rewrite

End If
Next
End Sub

I've tested this with a design file(e.g. test.dgn) that is contained within the folder path location that BL3D represents and attached a single reference from the same folder and because the we use relative paths, the attachname is simply the name of the dgn file. Now, when I run the macro, because strAttachPath expands to something like F:\1234\Live/BL3D/ , I am expecting that the macro to recognise this value is a match for the value of BL3D and will therefore change the attachname to BL3D:test.dgn. However what happens is the macro does not agree that it does match and uses the else condition and renames it and every other attachment to BLREFS:test.dgn. Note that I've added a forward slash on strAttachPath in order to match the value for BL3D.

What I have done is put a breakpoint in the IDE to see the exact values of strAttachPath and BL3D. StrAttachPath has the value F:\1234\Live\BL3D/ whereas BL3D is F:\1234\live\bl3d/. Noticing the case difference I have tried the following but without any success:

strAttachName = UCase(Att.AttachName)
strAttachPath = UCase(Att.DesignFile.Path & "/")

Can anyone highlight what I need to change in my code in order to get the macro to operate as desired?

Thanks

Parents
  • The problem may be that your code dwells in the twilight world between what a configuration file thinks is a directory separator (forward-slash) and what the operating system thinks is a director separator (back-slash), combined with issues of text comparison.

    First, these two strings are different when you use text comparison functions:

    \\server\folder1\folder2\

    \\server/folder1/folder2/

    You need to 'normalise' them, which in this context is what Robert suggested.

    Unknown said:
    strAttachPath = Att.DesignFile.Path & "/"

    Here, you're dealing with operating system paths, not configuration file paths, so append a back-slash rather than a forward-slash.

    Unknown said:
    If strAttachName = BLPROJECT_NO Then

    Prefer the VBA StrComp function. StrComp can perform a case-independant match.  The value for equality is zero:

    If (0 = StrComp("def", "DEF", vbTextCompare)) Then
    ' Strings match
    End If

    Debug.Print is your friend: sprinkle it through your code to be sure that what you think is happening really is what is happening.

     
    Regards, Jon Summers
    LA Solutions

  • Thanks for the explanation Jon. I had forgot to mention in my previous post that I have switched the forward slash for the back slash. I notice in my original code snippet that the first line in each if statement does not always contain the correct variable, it should always read If = not strAttachName. However, I have amended the code to fix the error make use of the StrComp function as suggested but it does not fix the problem and I am now encountering odd behaviour...

    upon starting macro I am finding that it goes through the code for the first 2 No. reference files in my active file and then pops up the following error: "Run Time Error -2147221504 (800400000) This Operation cannot be completed. Possible cause: the associated design file is unavailable."

    Now the problematic file is in the same directory as all the other attachments, its not read only and I can open & edit the file no problem. When the debugger displays the error code its highlighting this line of code: strAttachPath = Att.DesignFile.Path . I have also found something quite odd, running the script with no attachments in the file and only a single design model, the debugger reports that there are 2 attachments though their attachnames are using custom variable prefixes that my atttachments were being named in error when my code was not correct, during tests yesterday! How is this possible if there are no files in the reference manager.?

  • Unknown said:
    the error code its highlighting this line of code: strAttachPath = Att.DesignFile.Path

    Put a trace on those values to see what it's looking at:

    Debug.Print "strAttachPath='" & strAttachPath & "'"

    Debug.Print "Att.DesignFile.Path='" & Att.DesignFile.Path& "'"

    Unknown said:
    How is this possible if there are no files in the reference manager.?

    During code development many things are possible, including corruption of a DGN file.  Does keyin REF DETACH ALL help?  If the problem persists you'll need to start again with a clean DGN file.


     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon, using your key-in suggestion (as opposed to simply using ctrl+A as I had been in the ref manager) has successfully removed the dodgy hidden attachments at slots 9 & 10 and after reattaching the desired attachments, thankfully the macro now appears to be working.

    Thanks again

Reply Children
No Data