Add drawings references.

Hi, everyone,

I know there are similar post about this,  But I cant find a Finally solution.


This time I´m gona need your great knowledge about programmig.

I´m triying to make a "Macro" I´ve never done one before. So I Used to reading post  and them copy paste, and  normally woks, but this time dont.

I think That What I only need is add refference into a file.

By copy pasting, I get this

Sub Attach()

Dim myDGN As DesignFile
Dim myTag As TagElement
Dim myExcel As Excel.Application
Dim myWS As Excel.Worksheet
Dim CurRow As Long
Dim FileRow As Long


CurRow = 2

Set myExcel = GetObject(, "Excel.Application")
Set myWS = myExcel.ActiveSheet
CurRow = myExcel.ActiveCell.Row
FileRow = CurRow

While myWS.Cells(CurRow, 1) <> ""
Set myDGN = Application.OpenDesignFile( _
myWS.Cells(CurRow, 1), False)
Set oModel = ActiveDesignFile


CadInputQueue.SendKeyin "MODEL ACTIVE Model"
'CadInputQueue.SendKeyin "@C:\Autodesk\Path1.txt"


myDGN.Save
myDGN.Close

CurRow = CurRow + 1

Wend

End Sub

But only works if the reference files are storage in my computer  C:

I dont know if this is because the long of the path or because a specific configuration on the server we have. Something like  \\ES\.........

The second try, Is this

Sub Attach2 ()

Dim myDGN As DesignFile
Dim myTag As TagElement
Dim myExcel As Excel.Application
Dim myWS As Excel.Worksheet
Dim CurRow As Long
Dim CurCol As Long
Dim FileRow As Long
Dim Path1 As String

CurRow = 2

Set myExcel = GetObject(, "Excel.Application")
Set myWS = myExcel.ActiveSheet
CurRow = myExcel.ActiveCell.Row
FileRow = CurRow

While myWS.Cells(CurRow, 1) <> ""
Set myDGN = Application.OpenDesignFile( _
myWS.Cells(CurRow, 1), False)
Set oModel = ActiveDesignFile


CadInputQueue.SendKeyin "MODEL ACTIVE Model"


Dim oReference As Attachment


Set oReference = ActiveModelReference.Attachments.Add(myWS.Cells(CurRow, 2), Model, , , , , True, True)


myDGN.Save
myDGN.Close

Wend

End Sub

But  when i try to run it. A message appear. "COMPILE ERROR, ARGUMENT NOT OPTIONAL.


Any Idea about what happen?
Or any other solution ?

Any comment wil be very helpfull.
thanks.

Lucas ;)

  • Hi Lucas,

    Unknown said:
    A message appear. "COMPILE ERROR, ARGUMENT NOT OPTIONAL.

    For what line this error is reported? It's important information.

    Unknown said:
    Any Idea about what happen?

    I guess the error is reported for

    Set oReference = ActiveModelReference.Attachments.Add(myWS.Cells(CurRow, 2), Model, , , , , True, True)

    It's not allowed to sckip arguments you don't want to take care about. If in the method specification a particular argument is not defined as optional (using square brackets [ ] ), you have always to pass such argument into the method. Declaration of .Add method (from MicroStation VBA help file) is:

    Set Attachment = object.Add (FileSpecification, ModelName, LogicalName, Description, MasterOrigin, ReferenceOrigin [, TrueScale [, DisplayImmediately]]) 

    It means you have to pass LogicalName, Description, MasterOrigin and ReferenceOrigin, not to use commas only.

    Unknown said:
    Or any other solution ?

    It's hard to say because you have not provided any description what you want to do and what worklow should be used. To extract it from the code is risky and not good idea in general, because you wrote you are beginner and created the code as a copy/paste, so it's not clear how close the code is to your requirements.

    I also recommend to read and follow MicroStation Programming Forum best practices (specify version and language in a post subject, use Syntax Highlighter when any code is posted...). It will make your question better readable, which will increase a chance of quick answers.

    To be frank, the code is not very clear and I don't understand some parts, e.g. why

    CadInputQueue.SendKeyin "MODEL ACTIVE Model"

    is used.

    With regards,

     Jan

  • :)

    Thanks  very much Jan Slerg.

    Yes the error is in that line.

    Set oReference = ActiveModelReference.Attachments.Add(myWS.Cells(CurRow, 2), Model, , , , , True, True)

    I

    wrote this

    CadInputQueue.SendKeyin "MODEL ACTIVE Model"

    Because, y need to attach the reference in this model, (there are dwg files, and the default name is Model)

    so to change from sheet space to model space put this line (CadInputQueue.SendKeyin "MODEL ACTIVE Model")

    I know that is not the Best, and only work if the model is named "Model".

    Now it works, thanks to you.

    I change the line, and add pt1 and Opt1 ( I dont know if I had to set this 3dpoints to 0,0,0. But as a wrote the reference goes to the correct place )

    Sub ref()

       Dim myDGN As DesignFile

       Dim myExcel As Excel.Application

       Dim myWS As Excel.Worksheet

       Dim CurRow As Long

       Dim CurCol As Long

       Dim FileRow As Long

       Dim Path1 As String

       Dim pt1 As Point3d

       Dim Opt1 As Point3d

       CurRow = 2

       Set myExcel = GetObject(, "Excel.Application")

       Set myWS = myExcel.ActiveSheet

       CurRow = myExcel.ActiveCell.Row

       FileRow = CurRow

    While myWS.Cells(CurRow, 1) <> ""

       Set myDGN = Application.OpenDesignFile( _

                                       myWS.Cells(CurRow, 1), False)

                                       Set oModel = ActiveDesignFile

       CadInputQueue.SendKeyin "MODEL ACTIVE Model"

    Dim oReference As Attachment

    Set oReference = ActiveModelReference.Attachments.Add(myWS.Cells(CurRow, 2), Model, (myWS.Cells(CurRow, 3)), 1, pt1, Opt1, True, True)

    myDGN.Save

    myDGN.Close

    CurRow = CurRow + 1

    Wend

    End Sub

    The exel I use have this simple structure.

    Sheet Reference Reference Name
    Path of the file to open The path of the reference i want to add The lfile name (logical name)

    Y put the logical name, because i dont know other way.

    Now I can say that this is solved :)

    here the mbva
    AttachFromExel.mvba

  • Hi Lucas,

    Unknown said:
    Now I can say that this is solved :)

    With all respect, regardless you treated as solved, I don't think the code is workable and I have to say it's not good in my opinion.

    I strongly recommend to start any VBA Module code with Option Explicit that will ensure all variables have to be defined explicitely, which decrease a threat of wrongly used variables.

    I also see not used variables, personaly I hate to have all variables defined at the macro beginning, and also there are some not required commands (like Save and Close).

    I guess it can be simplified to this form (with a little help of VBA wrapper of MDL function ;-).

    Option Explicit
    
    Declare Function mdlSystem_newDesignFileAndModel Lib "stdmdlbltin.dll" (ByVal fileName As String, ByVal modelName As Long) As Long
    
    Sub ref()
        ' Get access to Excel
        Dim myExcel As Excel.Application
        Set myExcel = GetObject(, "Excel.Application")
        
        ' Get access to Excel active sheet
        Dim myWS As Excel.Worksheet
        Set myWS = myExcel.ActiveSheet
        
        ' Get active cell row number
        Dim CurRow As Long
        CurRow = myExcel.ActiveCell.Row
        
        ' Iterate through row from the active row
        While myWS.Cells(CurRow, 1) <> ""
        
            ' Open design file and specified model in one step
            OpenDesignFile myWS.Cells(CurRow, 1)
            mdlSystem_newDesignFileAndModel myWS.Cells(CurRow, 1), StrPtr("Model")
            
            ' Build full reference name (path + file name) from Excel cells
            Dim fullRefName As String
            fullRefName = myWS.Cells(CurRow, 2) & "\" & myWS.Cells(CurRow, 3)
            
            ' Define reference origin
            Dim referenceOrign As Point3d
            referenceOrign = Point3dZero
            
            ' Attach the reference to active model
            ActiveModelReference.Attachments.Add fullRefName, vbNullString, vbNullString, vbNullString, referenceOrign, referenceOrign, True, True
            
            CurRow = CurRow + 1
        Wend
    End Sub

    In fact this code is still quite poor, because there are no tests that operations (e.g. file opening) were finished without error and also no check for possible already existing reference attachment of the same file.

    With regards,

      Jan

  • Thanks Jan Slegr.

    It is really helpfull and didactic.
    I appreciate your comments in green. Now I can understand better what the code do and the meaning of that.

    Im using this code now :)