Consulting

Results 1 to 3 of 3

Thread: Creating CAD files using Excel.

  1. #1

    Creating CAD files using Excel.

    I am having a template drawings in C Drive. And a Excel file with list of say 50 Names. The task is using Excel-VBA, I have to create 50 files (using that template file.
    Summary of steps is as follows:

    1. Open Excel files having list of file names
    2. Run the script in excel VBA which opens AutoCAD template file from C drive and start saving with 50 names
    3. Exit ACAD.Application

    The Code I am using is mentioned below. Struck point is CAD Application is not saving the file with a new name or desired name and CAD application is not exiting with VBA
    I am a rookie level in VBA in Excel and have zero knowledge of VBA in AutoCAD.
    Your suggestions, help will be of great help..


    Sub Run_CAD()
    
    Dim ACAD AsObject
    'Dim ACAD As AcadApplication
    Dim NewFile AsObject
    Dim MyAcadPath AsString
    Dim bReadOnly AsBoolean
    Dim st AsString
    Dim MyPath AsString
    Dim CurrentDwg AsObject
    Dim foldername AsString
    Dim Filename AsString
    
    
        OnErrorResumeNext
        Set ACAD = GetObject(,"AutoCAD.Application")
        Set CurrentDwg = CurrentApp.ActiveDocument
        If(Err <>0)Then
            Err.Clear
            Set ACAD = CreateObject("autocad.Application")
            If(Err <>0)Then
                MsgBox "Could Not Load AutoCAD!", vbExclamation
                End
            EndIf
        EndIf
    
        'If you want to see AutoCAD on screen
        ACAD.Visible =True
    
        MyPath ="C:\"
    
         'Add a slash at the end if the user forget it
        If Right(MyPath,1)<>"\"Then
            MyPath = MyPath &"\"
        EndIf
    
        'Create folder for the new files
        foldername = MyPath & Format(Now,"yyyy-mm-dd hh-mm-ss")&"\"
        foldername ="C:\All CAD Files at "& Format(Now,"yyyy-mm-dd hh-mm-ss")&"\"
        MkDir foldername
    
    
        MyAcadPath ="c:\CAD Templates\DAE Template.dwg"
        bReadOnly =True
        Set NewFile = ACAD.Documents.Open(MyAcadPath, bReadOnly)
    
        If saveit =TrueThen
            'saveFolder = "F:\MHJ\"
            Filename ="Hey"
            filepath = MyPath & Filename
            CurrentDwg.SaveAs filepath
        Else
            ACAD.Close False
    
        EndIf
    
        '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
    
    
    
        '****
    
        MsgBox "Look in "& foldername &" for the files"
    
        stFolder ="C:\"
    
        If Len(Dir(stFolder, vbDirectory))<>0Then
            Shell "Explorer.exe /n,/e,"& stFolder, vbNormalFocus
        Else
            MsgBox "The directory does not exist!", vbCritical
        EndIf
    
        'Close AutoCAD Process
    
        CurrentDwg.Close False
        ACAD.Close False
        Set ACAD =Nothing
        Set NewFile =Nothing
    EndSub
    


    Many Thanks in advance.
    Last edited by Mushtaq86; 08-08-2018 at 02:20 AM. Reason: Code Ethics

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.mrexcel.com/forum/excel-...ing-excel.html
    and at: https://www.ozgrid.com/forum/forum/h...sing-excel-vba
    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3

    Kindly also notify Ozgrid of your cross-posting.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Help for the same query is also requested at the below mentioned links

    Stackoverflow: https://stackoverflow.com/questions/...44929_51709310

    I will keep on working on the code with the help received from anyone. Once, the query is solved... threads on all the above links will be updated with the code.
    Hope I am not breaking any rule or no one is getting offended.

    Sincere Thanks.

Tags for this Thread

Posting Permissions

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