Consulting

Results 1 to 10 of 10

Thread: Solved: Scan for a block in AutoCAD

  1. #1

    Solved: Scan for a block in AutoCAD

    Hi all,

    I am trying to scan each drawing and read certain blocks such as the sheet #, page title, etc. so I can put them into an excel sheet.

    This is what I have so far...
      Option Explicit
    Private Sub ScanBlock()
        Dim strString As String
        Dim obj As Object
        Dim obj2 As Object
        'On Error Resume Next
        For Each obj In ThisDrawing.PaperSpace
            If obj.ObjectName = "AcDbBlockReference" Then
                If obj.Name = "tbD2" Then
                    For Each obj2 In obj
                        If obj2.ObjectName = ?AcDbMtext? Then
                            strString = obj2.Value
                            MsgBox strString
                            'Return strString
                        End If
                    Next obj2
                End If
            End If
        Next obj
    End Sub
    It breaks at the red section (?AcDbMtext?) saying "variable not defined"
    Any ideas on why this is happening and how to fix it? I do have Mtext in the drawing. Please let me know if you need more info on this also.

    Thanks for the help,
    S_H

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Instead of dimming the variable as an object try AcadObject.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I know nil about autocad, but it looks here
    [VBA]For Each obj2 In obj
    [/VBA]
    that you are saying
    For Each Object in Object
    as opposed to
    For Each Object in Objects
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    md,
    The obj is "supposed" to be a block, which can have multiple items. But you may be correct, it may need to be dimmed as an AcadBlock.

    seeking_help,
    md has brought up a point that I originally missed. You are looking for a block when you may need to be looking for a blockreference. The difference is a block is inserted once, whereas the blockreference is inserted multiple times. But knowing this is a title block it should only be "referenced" once.

  5. #5
    VBAX Regular
    Joined
    Nov 2006
    Posts
    16
    Location
    try this
    Dim objtext as AcadText
    Dim objBlock as AcadBlockReference
     
    For Each obj In ThisDrawing.PaperSpace
            If TypeOf obj Is AcDbBlockReference And obj.Name = "tbD2" Then
               Set objBlock = obj
                For Each obj2 In objBlock
                      If TypeOf obj2 Is AcadText Then
                          Set objtext = obj2
                          strString = objtext.TextString
                          MsgBox strString

  6. #6
    Thanks for all the replies,

    Ok thats weird, I sent this code to a co-worker yesterday and it worked on his computer so I just re-typed that line this morning and no more error there.

    md, the obj2 is trying to look for Mtext inside the title block.

    Tommy, yes the block ("tbD2") is only "referenced" once in a drawing and it will now read that block.

    sca67can, I changed AcDbBlockReference to AcadBlockReference because it gave me a compile error "user-defined type not defined" on that TypeOf. It now tells me a run time error "object doesn't support this property or method." My code gives me the same error on the >>> For Each obj2 in obj <<<< line (I tried to change obj to --> obj.entity still nothing)



    It will scan through the blocks and it will find "tbD2" but I guess it does not like how I am looking for the Mtext inside that block. Any suggestions to fix these errors and to get the Mtext inside the Block?

    Thanks for all the help so far and for future help, I really appreciate it!

    Regards,
    S_H

  7. #7
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    [VBA]Option Explicit
    Private Sub ScanBlock()
    Dim strString As String
    Dim obj As AcadBlock
    Dim obj2 As AcadEntity
    For Each obj In ThisDrawing.Blocks
    If obj.Name = "tbD2" Then
    For Each obj2 In obj
    If obj2.ObjectName = "AcDbMText" Then 'this was the problem, notice the capital T
    strString = obj2.TextString
    MsgBox strString
    'Return strString
    End If
    Next obj2
    End If
    Next obj
    End Sub
    [/VBA]

  8. #8
    Ahh, got to love the minor details. Thanks for the catch Tommy, and now it works great.

    Next step is now to figure out how to send that string to an Excel Spreadsheet. Any ideas? I am just now to this step so have yet to research on it but thought i would ask.

    Thanks again with the help thus far!!

    Regards;
    S_H

  9. #9
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    [VBA]Sub PlaceInExcel(iStrInfo As String)
    Dim xL As Excel.Application
    Dim xLWrkBk As Workbook
    Dim xLSheet As Worksheet
    ' this will create a new instance every time you may want to check for an existing instance
    Set xL = CreateObject("Excel.Application")
    xL.Visible = True
    Set xLWrkBk = xL.Workbooks.Add
    Set xLSheet = xLWrkBk.Sheets("Sheet1")
    xLSheet.Cells(1, 1).Value = iStrInfo
    ' this leaves excel open and active if you need to do something else let me know
    Set xLSheet = Nothing
    Set xLWrkBk = Nothing
    Set xL = Nothing
    End Sub
    [/VBA]

  10. #10
    Wow, I was figuring a few lines at most

    Once again...perfect!! Gave me a compile error at first but that was because I did not have the correct reference.

    Few notes thought if anyone else needs help on this subject and reads this.

    1.) Make sure you have the "Microsoft Excel x.x Object Library" active
    2.) Have to add a simple line of code to get into the PlaceInExcel Sub. I simply used a vbYesNo + vbQuestion Msgbox to get into it.

    Thanks again!! This works great! SOLVED!

    Extreme gratitude,
    S_H

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •