Bill Tillman Posted January 24, 2012 Share Posted January 24, 2012 I'm using this snippet of code which was very generously shared by Fixo to launch AutoCAD, open up a drawing and then load and run a VLISP program. This is running inside of an Excel file and it's all working very well with the exception of this one issue. The users have stated using it as a what-if type of tool meaning that instead of running once and shutting AutoCAD down, they are re-executing the program. No big deal really except that each time they run it, we end up with a new incidence of AutoCAD running on the taskbar. I was wondering if this could be improved to merely use an existing incidence of AutoCAD if it's already running. I went to assist one of the users today and she had five AutoCAD sessions running and was about to launch another one. Sub Main() Dim acApp As AcadApplication Dim AcDocs As AcadDocuments Dim acDoc As AcadDocument Dim acSpace As AcadBlock Dim acdCap As String, ExcCap As String, copyStr As String '*********************************************************************** '* OPEN AUTOCAD THEN OPEN THE DRAWING FILE, LOAD THE VLISP AND EXECUTE IT '*********************************************************************** Dim strDrawing As String On Error Resume Next Set acApp = GetObject(, "AutoCAD.Application") If Err.Number <> 0 Then Err.Clear Set acApp = CreateObject("AutoCAD.Application") End If On Error GoTo Err_Control acApp.Visible = True SetFocus acApp.hwnd Application.WindowState = xlMinimized acApp.WindowState = acMax strDrawing = "path_to_dwg_file\dwg_file.dwg" Set acDoc = acApp.Documents.Open(strDrawing) acDoc.Activate Set acDoc = acApp.ActiveDocument acDoc.SendCommand ("(load "path_to_vlisp_file\\my_vlisp.lsp"" ""The load failed"") function_name" & Chr(13)) Quote Link to comment Share on other sites More sharing options...
BlackBox Posted January 25, 2012 Share Posted January 25, 2012 (edited) Sounds like you need to check for an active process, prior to creating a new instance. Here's a VB.NET snippet from one of my projects, for you to pull from: Imports Excel = Microsoft.Office.Interop.Excel Imports Microsoft.Office.Interop Imports System.Diagnostics Imports System.IO Module FOO Sub Excel_Foo() Dim xlApp As Excel.Application Try [color=seagreen]' Look for an existing process, if available getObject[/color] If Process.GetProcessesByName("Excel").Length > 0 Then xlApp = CType(GetObject(, "Excel.Application"), Excel.Application) [color=seagreen]' Else createObject[/color] Else xlApp = New Excel.Application End If [color=seagreen] ' <-- Do something[/color] Catch ex As Exception Finally xlApp = Nothing End Sub End Module Edited January 25, 2012 by BlackBox Quote Link to comment Share on other sites More sharing options...
Bill Tillman Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks Renderman. We are not yet running VB.NET but I'm selling the idea hard to the accountants. When you consider that AutoDesk has stopped supporting VBA it should be an easy sell. But we're not there quite yet. I will keep this code until we do. I've been looking at this VBA snippet 'The following code works to check if autocad is already running Dim AcadApp As AcadApplication If AcadApp Is Nothing Then Set AcadApp = CreateObject("AutoCAD.Application") Else Set AcadApp = GetObject(, "AutoCAD.Application") End If to see if I can do something with it. What I don't follow is will this allow the existing launch of AutoCAD to load the drawing file or will these codes just check to see if it's already running? And to be completely honest, I really don't know what to do with the return value. I'm still investigating this. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted January 25, 2012 Share Posted January 25, 2012 Thanks Renderman. We are not yet running VB.NET but I'm selling the idea hard to the accountants. When you consider that AutoDesk has stopped supporting VBA it should be an easy sell. But we're not there quite yet. I will keep this code until we do. First - Development with .NET is FREE, so the bean counters really should have nothing to do with it. To reiterate, here's a snippet from this post shared with you previously: Thinking more long-term, consider porting your code to VB.NET - You'll need Visual Studio 2010 (I use Express), the appropriate ObjectARX SDK(s), and I recommend you also use the current Wizard for properly debugging (when using Express). All .NET code I've shared, posted, etc. was written in Visual Studio Express. :wink: As for this.... I've been looking at this VBA snippet 'The following code works to check if autocad is already running Dim AcadApp As AcadApplication If AcadApp Is Nothing Then Set AcadApp = CreateObject("AutoCAD.Application") Else Set AcadApp = GetObject(, "AutoCAD.Application") End If to see if I can do something with it. What I don't follow is will this allow the existing launch of AutoCAD to load the drawing file or will these codes just check to see if it's already running? And to be completely honest, I really don't know what to do with the return value. I'm still investigating this. Again, I am not adept at VBA per-se, but given my learning some VB.NET, it appears that the code you posted returns either the existing AutoCAD.Application Object, or creates one. In any event, the resultant AcadApp, and all of its Properties, Methods and Events should be available for your use. Quote Link to comment Share on other sites More sharing options...
Bill Tillman Posted January 25, 2012 Author Share Posted January 25, 2012 Very sage advice sir. I am going to download VB.NET Express today and start the process of begging the IT guys to let me install it on this machine. I will definetly install it on my home machine tonight and start the learning process. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted January 25, 2012 Share Posted January 25, 2012 Very sage advice sir. I am going to download VB.NET Express today and start the process of begging the IT guys to let me install it on this machine. I will definetly install it on my home machine tonight and start the learning process. That is very kind of you to say, Bill. I am certainly no .NET expert... yet. But I know enough, to know that it's worth my while to learn a whole lot more. Too many good resources to post them all here; feel free to psot any questions you may have - if I don't already know where to point you, I may learn a lot in the process of finding out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.