vb6

Discussion in 'AutoCAD' started by john m, Jan 13, 2005.

  1. john m

    john m Guest

    Hello,

    Can someone direct me to an example of the proper way to access autocad
    from vb6?

    i just want to see if there is an open session of acad and then if there is
    use it and if not then start autocad.

    some of my programs have lately started acting whacky by saying that no
    autocad session was found when there is one open.

    do i need to set a reference to something? scripting runtime?

    thanks in advance for any reply

    jm
     
    john m, Jan 13, 2005
    #1
  2. john m

    Jackrabbit Guest

    Here's what I use to connect to Excel from VBA. You should be able to connect to AutoCAD the same way.
    [pre]
    On Error Resume Next
    Set objExcel = GetObject(, "Excel.Application")
    If objExcel Is Nothing Then
    Err.Clear
    blnExcelWasRunning = False
    Set objExcel = GetObject("", "Excel.Application")
    If Err.Number <> 0 Then
    Err.Clear
    MsgBox "Error: You must have Excel installed on your" & vbCrLf & _
    "computer to use the Import BDIF function."
    ImportDieData_BDIF = 1
    Set objExcel = Nothing
    Exit Function
    End If
    Else
    blnExcelWasRunning = True
    End If
    [/pre]
     
    Jackrabbit, Jan 13, 2005
    #2
  3. john m

    john m Guest

    thanks JR
    does anyone know why this code throws an error in VB6?
    this is the same way i was doing it before..
    what i get when i run this code or my own is "Compiler Error: Invalid
    Outside Procedure"


    thanks

    jm

    connect to AutoCAD the same way.
     
    john m, Jan 13, 2005
    #3
  4. john m

    hwalker Guest

    It probably doesn't work because JR says his program runs in VBA and you say your programs run in VB6. They use slightly different command sets. try adapting the following which I used for a program I wrote in VB6 for Autocad LT. It uses DDE and you need to put a text box on a form and call it txtbID. Also be careful of the word wrapping on the stuff I've pasted below

    txtbID.LinkMode = 0 '0 = reset
    txtbID.LinkTopic = "AutoCAD LT.DDE|system" 'establish DDE link

    txtbID.LinkMode = 2
    txtbID.LinkTimeout = -1

    If Dir("C:\plotlogfile\*.log") > "" Then
    FileName$ = Dir("C:\plotlogfile\*.log")
    Do Until FileName$ = ""
    If UCase$(FileName$) <> "HARDCOPY.LOG" Then Kill "c:\plotlogfile\" + FileName$
    FileName$ = Dir
    Loop
    End If

    'Call Command1_Click
    AppActivate "AutoCAD LT", True
     
    hwalker, Jan 14, 2005
    #4
  5. john m

    john m Guest

    Thank You

    I still get the same error regardless of which way i do it.

    For some reason every time i try to use appactivate or shell or createobject
    or get object its always the same

    "Invalid Outside Procedure"

    Programs which have worked before no longer work


    say your programs run in VB6. They use slightly different command sets. try
    adapting the following which I used for a program I wrote in VB6 for Autocad
    LT. It uses DDE and you need to put a text box on a form and call it txtbID.
    Also be careful of the word wrapping on the stuff I've pasted below
     
    john m, Jan 14, 2005
    #5
  6. john m

    Jackrabbit Guest

    Is your code actually inside a procedure??

    Public Sub()
    Your code here?
    End Sub
     
    Jackrabbit, Jan 14, 2005
    #6
  7. john m

    john m Guest

    thanks JR
    i was trying it in the immediate window
    finally got this to work

    Private Sub Form_Load()
    Dim oa As Object
    On Error Resume Next
    Set oa = GetObject(, "Autocad.application.16")
    If Err <> 0 Then
    Err.Clear
    Set oa = CreateObject("Autocad.application.16")
    End If
    End Sub

    apparently the ".16" is required for 2005
     
    john m, Jan 14, 2005
    #7
  8. | Hello,
    |
    | Can someone direct me to an example of the proper way to access autocad
    | from vb6?
    |
    | i just want to see if there is an open session of acad and then if there is
    | use it and if not then start autocad.
    |
    | some of my programs have lately started acting whacky by saying that no
    | autocad session was found when there is one open.
    |
    | do i need to set a reference to something? scripting runtime?
    |
    | thanks in advance for any reply

    You just need to add a reference to the "AutoCAD 2005 Type Library"
    for example (whatever your AutoCAD version is). Then you can just
    do something like the following:

    Private Sub Form_Load()
    '
    Dim AutoCAD As New AcadApplication
    AutoCAD.Documents.Open "C:\MyDWG.dwg"

    Dim Buffer As String
    Dim Layer As Object

    Dim ActiveDocument As AcadDocument
    Set ActiveDocument = AutoCAD.ActiveDocument

    Buffer = "This drawing contains the following layers: " & vbNewLine & vbNewLine

    For Each Layer In ActiveDocument.Layers

    If Layer.Name = "0" Then
    Buffer = "Default Layer: " & Layer.Name & vbNewLine & vbNewLine
    Else
    Buffer = Buffer + Layer.Name & vbNewLine
    End If

    Next Layer

    MsgBox Buffer, vbInformation, "LAYERS LIST INFORMATION"

    AutoCAD.Documents.Close
    AutoCAD.Quit

    Set AutoCAD = Nothing

    Unload Me
    End
    '
    End Sub

    Cheers.
    Chris Val
     
    Chris \( Val \), Jan 16, 2005
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.