Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I open an AutoCAD file from Excel?

Status
Not open for further replies.

jrice174

Civil/Environmental
Nov 8, 2004
129
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:
 
Replies continue below

Recommended for you

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

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
 
It worked perfectly. Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor