bug in Autodesk example file

Discussion in 'AutoCAD' started by perry, Jan 21, 2004.

  1. perry

    perry Guest

    Whilst trying to learn VBA for Acad and Inventor I was working on a routine
    (or other peoples routines I should say) for plotting from Acad.
    In trying to batch plot layouts I stepped though the code in the
    "SetLayoutsToPlot" example from the VBA help file.
    Therein I found 2 bugs one right after the other, in the block of code that
    reads thus:

    ' Prompt user for Layouts to plot
    For Each Layout In ThisDrawing.Layouts
    If MsgBox("Do you wish to plot the layout: " & Layout.name, vbYesNo
    & vbQuestion) = vbYes Then
    ArraySize = ArraySize + 1
    ReDim AddedLayouts(1 To ArraySize)
    AddedLayouts(ArraySize) = Layout.name
    End If
    Next

    The question mark between "vbYesNo" and "vbQuestion" should be an addition
    sign, as it is now it will cause an "okOnly" to appear and
    so you can never get a "yes" answer to the dialog and nothing will be
    plotted.
    Even worse than this simple typo is the "ReDim" statement, calling this will
    clear out the contents of the array so that no layout names
    will be in it, just empty strings, except for the last layout name added
    when the for statement ends without doing the Redim.
     
    perry, Jan 21, 2004
    #1
  2. That's not surprising, quite a few are incorrect. To fix the ReDim,
    incase you or someone else reading this doesn't know how, do something
    like this:

    ReDim AddedLayouts(1 To 2)
    ' Prompt user for Layouts to plot
    For Each Layout In ThisDrawing.Layouts
    If MsgBox("Do you wish to plot the layout: " & Layout.Name, vbYesNo +
    vbQuestion) = vbYes Then
    ArraySize = ArraySize + 1
    If ArraySize > 2 Then
    ReDim Preserve AddedLayouts(1 To ArraySize)
    End If
    AddedLayouts(ArraySize) = Layout.Name
    End If
    Next

    You first need to ReDim the original variable so it has a limit, then
    use the Preserve option of ReDim to retain any entries already within
    the array. Not pretty but I'm sure someone else will post if they have
    aa better solution:)
    ___________________________
    Mike Tuersley
    CADalyst's AutoCAD Clinic
    Rand IMAGINiT Technologies
     
    Mike Tuersley, Jan 22, 2004
    #2
  3. perry

    perry Guest

    Ah yes, that "preserve" thing, I remember reading something about it now ;)
     
    perry, Jan 22, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.