Results 1 to 6 of 6

Thread: From Excel to Autocad - using VBA

  1. #1
    Member
    Join Date
    2016-12
    Posts
    2
    Login to Give a bone
    0

    Default From Excel to Autocad - using VBA

    Hy,

    I am new to all of this(using VBA)but I am determined to make my life easier.
    What I did :
    -i wrote in excel the script of what i need to draw in autocad,if i copy paste in the command line in autocad it all works perfectly.
    -i found the vba code that opens autocad and this part works.
    What I need :
    -to make the VBA code that copies the script from excel in the command line in autocad(a copy paste from excel to autocad)event if it seems simple or maybe it is, i don't know how to do this.

    Many thanks for your help!

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,396
    Login to Give a bone
    0

    Default Re: From Excel to Autocad - using VBA

    Hi, welcome to augi.

    Your question is a little confusing. Are you saying you want the code to copy/paste entities or are you trying to copy/paste code? What is the overall problem you are trying to solve with code? You can also post your code.
    C:> ED WORKING....

  3. #3
    Member
    Join Date
    2016-12
    Posts
    2
    Login to Give a bone
    0

    Default Re: From Excel to Autocad - using VBA

    Thank you for your interest in helping me,

    I wrote in excel the following in column (A1:A350) and i want to make a vba excel ( if possible ) that copy/paste the range in the autocad command line.

    If I copy this range now in autocad it works.

    I m new to all of this, and this is why probably I am not very clear.

    RANGE :
    Code:
    -LAYER SET Axes
    _ltscale 50
    
    PLINE
    0,-744.204496985428
    1289,-744.204496985428
    0,1488.40899397086
    -1289,-744.204496985428
    0,-744.204496985428
    LINE
    0,1488.40899397086
    0,-744.204496985428
    1289,-744.204496985428
    -644.5,372.102248492714
    -1289,-744.204496985428
    644.5,372.102248492714
    
    -LAYER SET Concrete
    
    PLINE
    -250,1738.40899397086
    250,1738.40899397086
    250,1238.40899397086
    -250,1238.40899397086
    CLOSE
    *
    -ARRAY
    -250,1738.40899397086
    
    Polar
    0,0
    2
    -120
    y
    *
    I started with opening autocad like this :

    Code:
    Sub Acad()
    
        Dim strDrawing As String
        On Error Resume Next
        Set Graphics = GetObject(, "AutoCAD.Application")
        If Err.Description > vbNullString Then
            Err.Clear
            Set Graphics = CreateObject("AutoCAD.Application")
        End If
        ' ACAD.Visible = True
        strDrawing = "C:\Users\s\Desktop\FD\tte.dwg"
        Graphics.Documents.Open (strDrawing)
        Graphics.Visible = True
        On Error GoTo 0
        
    End Sub
    Last edited by Ed Jobe; 2016-12-14 at 04:18 PM.

  4. #4
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,396
    Login to Give a bone
    0

    Default Re: From Excel to Autocad - using VBA

    Try this. It assumes the code is in the module for Sheet1.
    Code:
    Option Explicit
    
    Sub Acad()
    
        Dim strDrawing As String
        Dim Graphics As AcadApplication
        Dim dwg As AcadDocument
        On Error Resume Next
        Set Graphics = GetObject(, "AutoCAD.Application")
        If Err.Description > vbNullString Then
        Err.Clear
        Set Graphics = CreateObject("AutoCAD.Application")
        End If
        strDrawing = "C:\Users\s\Desktop\FD\tte.dwg"
        Set dwg = Graphics.Documents.Open(strDrawing)
        Graphics.Visible = True
        On Error GoTo 0
        
        dwg.Utility.Prompt Sheet1.Range("A1", "A350")
    
    End Sub
    Notice that I added "Option Explicit". You didn't dimension a var for the Graphics object. With Option Explicit, the compiler will let you know of such errors. I assume you added a reference to the acad tlb to your project.
    C:> ED WORKING....

  5. #5
    Member
    Join Date
    2005-10
    Posts
    4
    Login to Give a bone
    0

    Default Re: From Excel to Autocad - using VBA

    I am working on this very same issue. Though, in my case, the application is BricsCAD, which is largely compatible with AutoCAD. First, I will mention that my BricsCAD has to be set to "run as administrator", or the security settings in my Windows 7 Pro system will not permit BricsCAD to change and remember how it is configured. This means that any other program that accesses BricsCAD, must also be set to "run as administrator". I don't know if this is true in AutoCAD.

    Now, for the program. It simply takes a series of cells in Excel, and then pastes them into the command line of BricsCAD. The benefit of using this method is that you can see the progress of your script on the command line. Otherwise it gets extremely difficult to debug your script, since you can't just look at the command line history to see where things went wrong.

    Firsts, note that I have tried to format my code, but even in the advanced message window, I cannot see any way to format. Even my attempt to format the code by adding a few spaces before each line, got edited out by the forum.

    In my program the script is built up into a string variable called ScriptText. I know there are probably some redundant things here, as I am experimenting and trying to make sure focus is in the right spot. At times I have had my routine paste the strip into the Excel VBA program itself! The issue of focus has been an ongoing battle for me, and I am still very much trying to figure it out.
    Code:
    -----Start Code Snip------
         'Variables
         Public acadApp As AcadApplication
         Public acadDoc As AcadDocument
         Dim ScriptText As String
    -----------------------
    
    There are two steps to get the scrip into the clipboard.
    ----Start Code Snip-------
         'Put the command string into the clipboard
         MyClipboard.SetText ScriptText 'step one use the settext
         MyClipboard.PutInClipboard    'step two, use  putinclipboard
    
         'Make sure BricsCAD has focus
         acadApp.Visible = True
         acadApp.ActiveDocument.Activate
         Set acadDoc = acadApp.ActiveDocument
    
         'Paste the commands.
         acadDoc.SendCommand ("_pasteclip" & Chr(13))
    -----------------------
    
    The script then creates a new drawing, draws the objects, converts them into a block, copies the block to the clipboard, and finally closes that temporary new drawing.
    ----Start Code Snip-------
         'Give BricsCAD focus, so user can paste block <-THIS PART IS NOT WORKING. FOCUS ALWAYS GOES TO EXCEL
         acadApp.Visible = True
         acadApp.ActiveDocument.Activate
    -----------------------
    Any ideas why I can't get the CAD program to end up with focus, so the user can simply hit Cntl-V to past the block I just created? As it is now, Excel has focus, so the user has to manually switch to the CAD application.
    Last edited by Ed Jobe; 2017-06-30 at 02:32 PM.

  6. #6
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,396
    Login to Give a bone
    0

    Default Re: From Excel to Autocad - using VBA

    Quote Originally Posted by cadcoke3 View Post
    Firsts, note that I have tried to format my code, but even in the advanced message window, I cannot see any way to format. Even my attempt to format the code by adding a few spaces before each line, got edited out by the forum.
    In the advanced editor, use the Code Tags button.
    Code Tags.png

    For your app, instead of
    Code:
    acadApp.Visible = True 'the app can be visible without being the active app
    try this line
    Code:
    AppActivate acadDoc.Application.Caption
    C:> ED WORKING....

Similar Threads

  1. Replies: 11
    Last Post: 2022-11-30, 03:18 PM
  2. Replies: 0
    Last Post: 2015-12-23, 06:01 AM
  3. Replies: 5
    Last Post: 2013-10-16, 05:39 PM
  4. Replies: 1
    Last Post: 2012-08-08, 05:50 PM
  5. Replies: 1
    Last Post: 2009-04-29, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •