Opening an autocad in Visual Basic 6

Opening an autocad in Visual Basic 6

Anonymous
Not applicable
471 Views
13 Replies
Message 1 of 14

Opening an autocad in Visual Basic 6

Anonymous
Not applicable
I am having a little problem trying to open an AutoCAD file in Visual Basic

Autocad Starts, and then errors when it trys to open an existing File
Anyone any Ideas

Thanks

Dim AcadApplication As AcadApplication
Dim ThisDrawing As AcadDocument
Dim AcadSelectedFile as string

AcadSelectedFile ="C:\TEMP\TEST.DWG"

'On Error GoTo ErrorLog 'Catch any errors

Set AcadApplication = CreateObject("Autocad.Application") 'open AutoCAD
Set AcadApplication = GetObject(, "AutoCAD.Application") 'setq reference to
AutoCAD Application

AcadApplication.Visible = True 'Make Autocad Visible
Set ThisDrawing = AcadApplication.ActiveDocument 'set reference to active
document

'Open the new file
ThisDrawing.Open AcadSelectedFile
0 Likes
472 Views
13 Replies
Replies (13)
Message 2 of 14

Anonymous
Not applicable
If it's A2K (and you're not in SDI mode), you need to call the Open method
of the AcadDocuments collection

--
Visit me at http://www2.stonemedia.com/franko

"Graham Steele" wrote in message
news:ef06d74.-1@WebX.SaUCah8kaAW...
> I am having a little problem trying to open an AutoCAD file in Visual
Basic
>
> Autocad Starts, and then errors when it trys to open an existing File
> Anyone any Ideas
>
> Thanks
>
> Dim AcadApplication As AcadApplication
> Dim ThisDrawing As AcadDocument
> Dim AcadSelectedFile as string
>
> AcadSelectedFile ="C:\TEMP\TEST.DWG"
>
> 'On Error GoTo ErrorLog 'Catch any errors
>
> Set AcadApplication = CreateObject("Autocad.Application") 'open AutoCAD
> Set AcadApplication = GetObject(, "AutoCAD.Application") 'setq reference
to
> AutoCAD Application
>
> AcadApplication.Visible = True 'Make Autocad Visible
> Set ThisDrawing = AcadApplication.ActiveDocument 'set reference to active
> document
>
> 'Open the new file
> ThisDrawing.Open AcadSelectedFile
>
0 Likes
Message 3 of 14

Anonymous
Not applicable
Can you explain ??
how do I call the open method ?

Graham Steele
CADD Manager
Whitman Requardt Associates
Baltimore, MD, 21218
gsteele@wrallp.com
Frank Oquendo wrote in message
news:ef06d74.0@WebX.SaUCah8kaAW...
> If it's A2K (and you're not in SDI mode), you need to call the Open method
> of the AcadDocuments collection
>
> --
> Visit me at http://www2.stonemedia.com/franko
>
> "Graham Steele" wrote in message
> news:ef06d74.-1@WebX.SaUCah8kaAW...
> > I am having a little problem trying to open an AutoCAD file in Visual
> Basic
> >
> > Autocad Starts, and then errors when it trys to open an existing File
> > Anyone any Ideas
> >
> > Thanks
> >
> > Dim AcadApplication As AcadApplication
> > Dim ThisDrawing As AcadDocument
> > Dim AcadSelectedFile as string
> >
> > AcadSelectedFile ="C:\TEMP\TEST.DWG"
> >
> > 'On Error GoTo ErrorLog 'Catch any errors
> >
> > Set AcadApplication = CreateObject("Autocad.Application") 'open AutoCAD
> > Set AcadApplication = GetObject(, "AutoCAD.Application") 'setq
reference
> to
> > AutoCAD Application
> >
> > AcadApplication.Visible = True 'Make Autocad Visible
> > Set ThisDrawing = AcadApplication.ActiveDocument 'set reference to
active
> > document
> >
> > 'Open the new file
> > ThisDrawing.Open AcadSelectedFile
> >
>
0 Likes
Message 4 of 14

Anonymous
Not applicable
Assuming your AutoCAD object is named acadApp:

acadApp.Documents.Open ,[ReadOnly (True or False)]

--
Visit me at http://www2.stonemedia.com/franko

"Graham Steele" wrote in message
news:ef06d74.1@WebX.SaUCah8kaAW...
> Can you explain ??
> how do I call the open method ?
0 Likes
Message 5 of 14

Anonymous
Not applicable
Thanks I understand now

--
Graham Steele
CADD Manager
Whitman Requardt Associates
Baltimore, MD, 21218
gsteele@wrallp.com
Frank Oquendo wrote in message
news:ef06d74.2@WebX.SaUCah8kaAW...
> Assuming your AutoCAD object is named acadApp:
>
> acadApp.Documents.Open ,[ReadOnly (True or False)]
>
> --
> Visit me at http://www2.stonemedia.com/franko
>
> "Graham Steele" wrote in message
> news:ef06d74.1@WebX.SaUCah8kaAW...
> > Can you explain ??
> > how do I call the open method ?
>
0 Likes
Message 6 of 14

Anonymous
Not applicable
The method identified is not working

I still have a method open of object acaddocument failed

If a mark the file for read only this is requesting and users defined data
type,
for the read only method
Sorry for the inconvenience

--
Graham Steele
CADD Manager
Whitman Requardt Associates
Baltimore, MD, 21218
gsteele@wrallp.com
Frank Oquendo wrote in message
news:ef06d74.2@WebX.SaUCah8kaAW...
> Assuming your AutoCAD object is named acadApp:
>
> acadApp.Documents.Open ,[ReadOnly (True or False)]
>
> --
> Visit me at http://www2.stonemedia.com/franko
>
> "Graham Steele" wrote in message
> news:ef06d74.1@WebX.SaUCah8kaAW...
> > Can you explain ??
> > how do I call the open method ?
>
0 Likes
Message 7 of 14

Anonymous
Not applicable
Verify the existence of the drawing and that you are specifying the correct
path. If you still have trouble, post the exact you are using. Also, the
*assumption* has been that you're programming A2K. Is this the case?

--
Visit me at http://www2.stonemedia.com/franko

"Graham Steele" wrote in message
news:ef06d74.4@WebX.SaUCah8kaAW...
> The method identified is not working
>
> I still have a method open of object acaddocument failed
>
> If a mark the file for read only this is requesting and users defined data
> type,
> for the read only method
> Sorry for the inconvenience
0 Likes
Message 8 of 14

Anonymous
Not applicable
Yes this is a2k, It works fine in AutoCAD 14
acadselect file is defined by dialog box
even if I specify the path I error
Thanks

''Define AutoCAD Variables System
Dim AcaDApplication As AcaDApplication
Dim ThisDrawing As AcadDocument

Dim AcadSelectedFile As String 'Define the selected file for Autocad

Dim objectLayer As AcadLayer 'Variable For Layers

Dim LayerTmp As String'Temporay Locator for Print the Layers of a file

'On Error GoTo ErrorLog 'Catch any errors

Set AcaDApplication = CreateObject("Autocad.Application") 'open AutoCAD
Set AcaDApplication = GetObject(, "AutoCAD.Application") 'setq reference to
AutoCAD Application

AcaDApplication.Visible = True 'Make Autocad Visible
Set ThisDrawing = AcaDApplication.ActiveDocument 'set reference to active
document

'Open the new file
ThisDrawing.Open AcadSelectedFile.......................

--
Graham Steele
CADD Manager
Whitman Requardt Associates
Baltimore, MD, 21218
gsteele@wrallp.com
Frank Oquendo wrote in message
news:ef06d74.5@WebX.SaUCah8kaAW...
> Verify the existence of the drawing and that you are specifying the
correct
> path. If you still have trouble, post the exact you are using. Also, the
> *assumption* has been that you're programming A2K. Is this the case?
>
> --
> Visit me at http://www2.stonemedia.com/franko
>
> "Graham Steele" wrote in message
> news:ef06d74.4@WebX.SaUCah8kaAW...
> > The method identified is not working
> >
> > I still have a method open of object acaddocument failed
> >
> > If a mark the file for read only this is requesting and users defined
data
> > type,
> > for the read only method
> > Sorry for the inconvenience
>
0 Likes
Message 9 of 14

Anonymous
Not applicable
You don't need to use both CreateObject and GetObject. If the CreateObject
worked, the call to GetObject is unnecessary. Actually, most folks do it
just the other way around: Try GetObject to attach to a running instance and
if there is none use CreateObject to start one. Anywau, assuming you have a
valid reference to an instance of AutoCAD, you're using the wrong Open
method.

You have this:
ThisDrawing.Open AcadSelectedFile

What you need is this:
AcadApplication.Documents.Open AcadSelectedFile

One other note. I don't know if it's at the root of your problem (or even if
it is a problem) but I would suggest you not name a variable after a class.
Try something like objAcad or acadAppp.

--
Visit me at http://www2.stonemedia.com/franko

"Graham Steele" wrote in message
news:ef06d74.6@WebX.SaUCah8kaAW...
> Yes this is a2k, It works fine in AutoCAD 14
> acadselect file is defined by dialog box
> even if I specify the path I error
> Thanks
>
> ''Define AutoCAD Variables System
> Dim AcaDApplication As AcaDApplication
> Dim ThisDrawing As AcadDocument
>
> Dim AcadSelectedFile As String 'Define the selected file for Autocad
>
> Dim objectLayer As AcadLayer 'Variable For Layers
>
> Dim LayerTmp As String'Temporay Locator for Print the Layers of a file
>
> 'On Error GoTo ErrorLog 'Catch any errors
>
> Set AcaDApplication = CreateObject("Autocad.Application") 'open AutoCAD
> Set AcaDApplication = GetObject(, "AutoCAD.Application") 'setq reference
to
> AutoCAD Application
>
> AcaDApplication.Visible = True 'Make Autocad Visible
> Set ThisDrawing = AcaDApplication.ActiveDocument 'set reference to active
> document
>
> 'Open the new file
> ThisDrawing.Open AcadSelectedFile.......................
>
> --
> Graham Steele
> CADD Manager
> Whitman Requardt Associates
> Baltimore, MD, 21218
> gsteele@wrallp.com
> Frank Oquendo wrote in message
> news:ef06d74.5@WebX.SaUCah8kaAW...
> > Verify the existence of the drawing and that you are specifying the
> correct
> > path. If you still have trouble, post the exact you are using. Also, the
> > *assumption* has been that you're programming A2K. Is this the case?
> >
> > --
> > Visit me at http://www2.stonemedia.com/franko
> >
> > "Graham Steele" wrote in message
> > news:ef06d74.4@WebX.SaUCah8kaAW...
> > > The method identified is not working
> > >
> > > I still have a method open of object acaddocument failed
> > >
> > > If a mark the file for read only this is requesting and users defined
> data
> > > type,
> > > for the read only method
> > > Sorry for the inconvenience
> >
>
0 Likes
Message 10 of 14

Anonymous
Not applicable
Graham,

Here is your answer.

First, a function to open AutoCAD (put in a standard .bas module

Public Function StartAutoCAD(AcadApp As AcadApplication) As Boolean
On Error Resume Next

'assume success
StartAutoCAD = True

Set AcadApp = GetObject(, "AutoCAD.Application")

If Err Then
Err.Clear

Set AcadApp = CreateObject("AutoCAD.Application")

If Err Then
StartAutoCAD = False
End If
End If
End Function

To use it, place a call to the function in say your Form_Load event as
follows

Private Sub Form_Load()
If StartAutoCAD(objAcadApp) Then
objAcadApp.Visible = True
'change this to match your drawing filename
objAcadApp.Documents.Open "C:\Program Files\Acad2000\Drawing1.dwg"

Else
MsgBox "Unable to start AutoCAD 2000"
Unload Me
End If
End Sub

That's it.
--
Joe Sutphin
Author of "AutoCAD 2000 VBA Programmers Reference"
ISBN #1861002564

Checkout Sources - The magazine dedicated to AutoCAD customization
http://vbdesign.hypermart.net/sources/sources.htm

Visit our website http://vbdesign.hypermart.net/sources/sources.htm and find
out how you can reserve your
copy of the book "The Complete AutoCAD R14.01 and 2000 VBA Reference Guide"

****************************************************************************
*****************************************

Graham Steele wrote in message
news:ef06d74.-1@WebX.SaUCah8kaAW...
> I am having a little problem trying to open an AutoCAD file in Visual
Basic
>
> Autocad Starts, and then errors when it trys to open an existing File
> Anyone any Ideas
>
> Thanks
>
> Dim AcadApplication As AcadApplication
> Dim ThisDrawing As AcadDocument
> Dim AcadSelectedFile as string
>
> AcadSelectedFile ="C:\TEMP\TEST.DWG"
>
> 'On Error GoTo ErrorLog 'Catch any errors
>
> Set AcadApplication = CreateObject("Autocad.Application") 'open AutoCAD
> Set AcadApplication = GetObject(, "AutoCAD.Application") 'setq reference
to
> AutoCAD Application
>
> AcadApplication.Visible = True 'Make Autocad Visible
> Set ThisDrawing = AcadApplication.ActiveDocument 'set reference to active
> document
>
> 'Open the new file
> ThisDrawing.Open AcadSelectedFile
>
0 Likes
Message 11 of 14

Anonymous
Not applicable
One thing that I forgot to mention in the previous post is that when you are
using standalone VB you must set a reference [Project|References] to the
AutoCAD Type Library. This step is NOT required if you are using the VBAIDE
inside of AutoCAD. Sorry for the omission.

Joe

Joe Sutphin wrote in message
news:ef06d74.8@WebX.SaUCah8kaAW...
> Graham,
>
> Here is your answer.
>
> First, a function to open AutoCAD (put in a standard .bas module
>
> Public Function StartAutoCAD(AcadApp As AcadApplication) As Boolean
> On Error Resume Next
>
> 'assume success
> StartAutoCAD = True
>
> Set AcadApp = GetObject(, "AutoCAD.Application")
>
> If Err Then
> Err.Clear
>
> Set AcadApp = CreateObject("AutoCAD.Application")
>
> If Err Then
> StartAutoCAD = False
> End If
> End If
> End Function
>
> To use it, place a call to the function in say your Form_Load event as
> follows
>
> Private Sub Form_Load()
> If StartAutoCAD(objAcadApp) Then
> objAcadApp.Visible = True
> 'change this to match your drawing filename
> objAcadApp.Documents.Open "C:\Program Files\Acad2000\Drawing1.dwg"
>
> Else
> MsgBox "Unable to start AutoCAD 2000"
> Unload Me
> End If
> End Sub
>
> That's it.
> --
> Joe Sutphin
> Author of "AutoCAD 2000 VBA Programmers Reference"
> ISBN #1861002564
>
> Checkout Sources - The magazine dedicated to AutoCAD customization
> http://vbdesign.hypermart.net/sources/sources.htm
>
> Visit our website http://vbdesign.hypermart.net/sources/sources.htm and
find
> out how you can reserve your
> copy of the book "The Complete AutoCAD R14.01 and 2000 VBA Reference
Guide"
>
>
****************************************************************************
> *****************************************
>
> Graham Steele wrote in message
> news:ef06d74.-1@WebX.SaUCah8kaAW...
> > I am having a little problem trying to open an AutoCAD file in Visual
> Basic
> >
> > Autocad Starts, and then errors when it trys to open an existing File
> > Anyone any Ideas
> >
> > Thanks
> >
> > Dim AcadApplication As AcadApplication
> > Dim ThisDrawing As AcadDocument
> > Dim AcadSelectedFile as string
> >
> > AcadSelectedFile ="C:\TEMP\TEST.DWG"
> >
> > 'On Error GoTo ErrorLog 'Catch any errors
> >
> > Set AcadApplication = CreateObject("Autocad.Application") 'open AutoCAD
> > Set AcadApplication = GetObject(, "AutoCAD.Application") 'setq
reference
> to
> > AutoCAD Application
> >
> > AcadApplication.Visible = True 'Make Autocad Visible
> > Set ThisDrawing = AcadApplication.ActiveDocument 'set reference to
active
> > document
> >
> > 'Open the new file
> > ThisDrawing.Open AcadSelectedFile
> >
>
0 Likes
Message 12 of 14

Anonymous
Not applicable
Excuse me for saying this, but everything that everyone
has told you to this point, is wrong. Most people seem
to overlook the fact that they can use GetObject to get
an AcadDocument object, as well as AcadApplication.

If the objective is to start a running instance of
AutoCAD and open an existing file, then what you
should be doing, is this:

Dim Doc As AcadDocument
Set Doc = GetObject("C:\TEMP\TEST.DWG")

You can then get the Application object from the
Document object returned by GetObject().

This will not only start AutoCAD and make it load the
specified file, but if the file is already open in a
running instance of AutoCAD, it will return the open
document object, rather than start another instance of
AutoCAD and load the file again.

Not only using this method eliminate the need to check
whether an instance of AutoCAD is already running, it
eliminates the need to call the Open() method, and it
does something important that few VB programmers ever
bother to do, which is to check to see if the existing
file they are about to open, is already open in a
running instance of AutoCAD.

Graham Steele wrote:
>
> Yes this is a2k, It works fine in AutoCAD 14
> acadselect file is defined by dialog box
> even if I specify the path I error
> Thanks
>
> ''Define AutoCAD Variables System
> Dim AcaDApplication As AcaDApplication
> Dim ThisDrawing As AcadDocument
>
> Dim AcadSelectedFile As String 'Define the selected file for Autocad
>
> Dim objectLayer As AcadLayer 'Variable For Layers
>
> Dim LayerTmp As String'Temporay Locator for Print the Layers of a file
>
> 'On Error GoTo ErrorLog 'Catch any errors
>
> Set AcaDApplication = CreateObject("Autocad.Application") 'open AutoCAD
> Set AcaDApplication = GetObject(, "AutoCAD.Application") 'setq reference to
> AutoCAD Application
>
> AcaDApplication.Visible = True 'Make Autocad Visible
> Set ThisDrawing = AcaDApplication.ActiveDocument 'set reference to active
> document
>
> 'Open the new file
> ThisDrawing.Open AcadSelectedFile.......................
>
> --
> Graham Steele
> CADD Manager
> Whitman Requardt Associates
> Baltimore, MD, 21218
> gsteele@wrallp.com
> Frank Oquendo wrote in message
> news:ef06d74.5@WebX.SaUCah8kaAW...
> > Verify the existence of the drawing and that you are specifying the
> correct
> > path. If you still have trouble, post the exact you are using. Also, the
> > *assumption* has been that you're programming A2K. Is this the case?
> >
> > --
> > Visit me at http://www2.stonemedia.com/franko
> >
> > "Graham Steele" wrote in message
> > news:ef06d74.4@WebX.SaUCah8kaAW...
> > > The method identified is not working
> > >
> > > I still have a method open of object acaddocument failed
> > >
> > > If a mark the file for read only this is requesting and users defined
> data
> > > type,
> > > for the read only method
> > > Sorry for the inconvenience
> >

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 13 of 14

Anonymous
Not applicable
You love that word don't you. There is nothing "wrong" with first gaining a
reference to the application object and then the drawing. You believe that
using GetObject using a drawing path is more *expedient* and it very well
may be depending on your overall objective. But that does not invalidate any
other method that yields the same end result. Joe's code is just as valid as
what you have shown. I can see the advantage of the method you demonstrated
and appreciate your input. But sooner or later Graham is going have to learn
how to use the Open method, wouldn't you agree?

--
Visit AcadXtreme for a chance to win a copy of AutoCAD LT 2000
Contest ends 5/26/00
http://www2.stonemedia.com/franko

"Tony Tanzillo" wrote in message
news:391D0EF0.18A3B8A5@worldnet.att.net...
> Excuse me for saying this, but everything that everyone
> has told you to this point, is wrong. Most people seem
> to overlook the fact that they can use GetObject to get
> an AcadDocument object, as well as AcadApplication.
0 Likes
Message 14 of 14

Anonymous
Not applicable
Hi Tony

This is great this is exactly what I need for the developemt of my application, however is this all the code that is required the two lines above, I program in visual basic but I do not have any experience in programming visual basic for Autocad I would appreciate any assistance
0 Likes