Open DWG dialog window from excel

Open DWG dialog window from excel

chrisjspower
Enthusiast Enthusiast
1,587 Views
3 Replies
Message 1 of 4

Open DWG dialog window from excel

chrisjspower
Enthusiast
Enthusiast

Is it possible?

I have got this far:

 

Open autocad application

Check if app is open

if not - MsgBox

Set the open document to the active document

if that returns "nothing" then

open file dialog box <<< this where I fall over 😞

 

Sub OpenFileCAD()

    Dim AcadApp As Object
    Dim acadDoc As Object
    Dim acadAppVar As Integer
acadAppVar = 0
'--------------------------------------------------------------------
 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
        Set AcadApp = GetObject(, "AutoCAD.Application")
If AcadApp Is Nothing Then
        Set AcadApp = CreateObject("AutoCAD.Application")
        AcadApp.Visible = True
End If
'--------------------------------------------------------------------
    'Check (again) if there is an AutoCAD object.
If AcadApp Is Nothing Then
        MsgBox "Message", vbCritical, "AutoCAD Error"
        Exit Sub

        Else:  acadAppVar = 1 '<<< Used later on

End If
    On Error GoTo 0
'--------------------------------------------------------------------
    'Check if there is an active drawing.
On Error Resume Next
Set acadDoc = AcadApp.ActiveDocument
On Error GoTo 0

'No active drawing found. Create a new one.
If acadDoc Is Nothing Then


' ¯\_(?)_/¯

'acadApp.Visible = True
End If
End Sub

I have spent most of today and all of yesterday trying to figure this out, I have done many a search so please forgive me if I missed something obvious.

 

Thanks for you time! 🙂

0 Likes
1,588 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor

@chrisjspower wrote:

 

Sub OpenFileCAD()

    Dim AcadApp As Object
    Dim acadDoc As Object
    Dim acadAppVar As Integer
acadAppVar = 0
'--------------------------------------------------------------------
 'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
        Set AcadApp = GetObject(, "AutoCAD.Application")
If AcadApp Is Nothing Then
        Set AcadApp = CreateObject("AutoCAD.Application")
        AcadApp.Visible = True
End If
'--------------------------------------------------------------------
    'Check (again) if there is an AutoCAD object.
If AcadApp Is Nothing Then
        MsgBox "Message", vbCritical, "AutoCAD Error"
        Exit Sub

        Else:  acadAppVar = 1 '<<< Used later on

End If
    On Error GoTo 0
'--------------------------------------------------------------------
    'Check if there is an active drawing.    
If AcadApp.Documents.Count > 0 Then
Set acadDoc = AcadApp.ActiveDocument
Else
Set acadDoc = AcadApp.Documents.Add("acad.dwt")
End If End If End Sub

I have spent most of today and all of yesterday trying to figure this out, I have done many a search so please forgive me if I missed something obvious.

 

Thanks for you time! 🙂


 

Try making the changes in red.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 4

norman.yuan
Mentor
Mentor

There is no direct, built-in way in AutoCAD COM API to show open file dialog, other than using Win32 API's Common Dialogs. Since your code runs from Excel side, you do not have to bother using Win32 API, because Excel VBA provides built-in way to show open/save file dialog with Application.FileDialog.

 

Go to Excel VBA editor, open "Object Browser" window, locate and select "Application" on left, fine "FileDialog" on the right and select it; then click "?" for help documentation/sample code.

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 4

chrisjspower
Enthusiast
Enthusiast

I couldn't figure out how to apply either of these suggestions, though I tried and am very grateful for the assistance.

 

I ended up programming the function to start a new ACAD document,

write to it's command line to open a file there,

and then close the "new" document once the opened document was the application focus.

Works pretty well and with the rest of the function without throwing any errors....yet!

0 Likes