×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How do I open an AutoCAD file from Excel?

How do I open an AutoCAD file from Excel?

How do I open an AutoCAD file from Excel?

(OP)
From Excel I want to use the GetOpenFilename method to open an Autocad file. Below is what I have but it certainly doesn't work. any ideas?

Dim NewFile As String
Dim ACAD As Object
Dim MyPath As String
On Error Resume Next
Set ACAD = GetObject(, "ACAD.Application")
If Err <> 0 Then
    Err.Clear
    Set ACAD = CreateObject("autocad.Application")
    If Err <> 0 Then
        MsgBox "Could Not Load AutoCAD!", vbExclamation
        End
    End If
End If
On Error GoTo ErrorMessage
'     Section 2. Get Excel ready to make the data available.
''MyPath = "C:\Documents and Settings\HP_Administrator\Desktop\SampleSpreadsheet.xls"
''MyPath = "G:\Jerico stuff\marketing\Form Letter\Local Designers.xls"
MyPath = "G:\Programming\Ares PDQ\A0631621.dwg"
NewFile = ACAD.Application.GetOpenFilename("Drawing File (*.dwg), *.dwg", , "Select Drawing File", , False)
MsgBox NewFile
'Set ExcelSheet = Excel.workbooks.Open(NewFile)
GoTo PastErrorMessage
ErrorMessage:
If NewFile = "False" Then End
MsgBox "Could not find the required spreadsheet that should be located at" & vbCr & MyPath & vbCr & "Please rename or relocate the specified file as needed."
End
PastErrorMessage:

RE: How do I open an AutoCAD file from Excel?

Required changes:
  • Dim NewFile As Object
  • Dim bReadOnly As Boolean
  • bReadOnly = True 'Or could be False
  • Set NewFile = ACAD.Documents.Open(MyAcadPath, bReadOnly)

CODE

Sub main()

Dim ACAD As Object
Dim NewFile As Object
Dim MyAcadPath As String
Dim bReadOnly As Boolean

    On Error Resume Next
    Set ACAD = GetObject(, "ACAD.Application")
    If (Err <> 0) Then
        Err.Clear
        Set ACAD = CreateObject("autocad.Application")
        If (Err <> 0) Then
            MsgBox "Could Not Load AutoCAD!", vbExclamation
            End
        End If
    End If
    
    'If you want to see AutoCAD on screen
    'ACAD.Visible = True
    
    MyAcadPath = "c:\Temp\Drawing2.dwg"
    bReadOnly = True
    Set NewFile = ACAD.Documents.Open(MyAcadPath, bReadOnly)
    
    If (NewFile Is Nothing) Then
ErrorMessage:
        If NewFile = "False" Then End
            MsgBox "Could not find the required spreadsheet that should be located at" & vbCr & MyAcadPath & vbCr & "Please rename or relocate the specified file as needed."
        End
    End If
    
    'Close AutoCAD Process
    'ACAD.Quit
    
    Set ACAD = Nothing
    Set NewFile = Nothing

End Sub

Also...
Dim ACAD As Object
-->Will work fine. But a better choice would be:
   Dim ACAD As AcadApplication
   But you first need to go to Tools/References and add
   the 'AutoCAD #### Type Library' for this to work.

Just my $0.02

Enjoy
 

RE: How do I open an AutoCAD file from Excel?

(OP)
It worked perfectly. Thanks so much.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources