Jump to content

Launching AutoCAD from VB Excel Macro


Bill Tillman

Recommended Posts

I have tried this out along with a couple of other different snippets but all that happens is AutoCAD appears to be running but there is no AutoCAD window on the screen. The acad.exe process is running because I can see that with task manager.

 

Public Graphics As AcadApplication

Public Sub OpnAcad()

   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:\0\Test.dwg"
   Graphics.Documents.Open (strDrawing)

   On Error GoTo 0
End Sub

 

There was at least one instance when I ran this twice and the Acad window appeared , two of them to be exact with the Test.dwg drawing open. But when I closed one of them, both of them closed.

Link to comment
Share on other sites

Have you tried?:

 

Graphics.Visible = True

 

Thank you. Actually, that was a piece of code left over from another test thus the comment mark. But I tried this and viola...it's now working. It opens up in a small window though. I'd like to have it maximized for use when it opens up.

 

Can I also safely assume that once you set the and add the AutoCAD Type Library at each users' computer, this will launch AutoCAD no matter which version they have and no matter how their drive mappings are configured?

Link to comment
Share on other sites

. . . .It opens up in a small window though. I'd like to have it maximized for use when it opens up.

 

 

It has been a while since I’ve used VBA, so I’ll have to confirm when I get a chance, but:

 

Graphics.WindowState= acMax to maximize the window may work.

 

 

Can I also safely assume that once you set the and add the AutoCAD Type Library at each users' computer, this will launch AutoCAD no matter which version they have and no matter how their drive mappings are configured?

 

I believe that is true. I think Windows takes care of that behind the scenes.

Link to comment
Share on other sites

It appears that the window will open with it's last property, that is when AutoCAD was last shutdown. I tried the command suggested but no luck. Will have to keep trying on that one.

 

Now of course there is one other catch. When I was using SHELL I was able to include a command line switch ("/b")to launch a script as well, like this:

 

/b [url="file://\\big_server\SYS2\DWG\STARTUP_SCRIPT\tps.scr"]\\big_server\SYS2\DWG\STARTUP_SCRIPT\tps.scr[/url]

 

This is vital to the automation process I'm trying to accomplish and I need a way to pass this argument into the process. I'm conducting more searches for this information now.

 

Thanks again for the pointers.

Link to comment
Share on other sites

Fixo,

 

You are a gentleman and a scholar. Not to mention a genius at this kind of coding. I leanred more in the first 2 mnutes of looking at your example than I did all last night in reading docs and search articles.

 

I think I have the auto-launch part down. What I need to do now is have the process also auto-launch the VLISP code which I've been working on the past week. This code make various adjustments to the drawing that gets opened.

Link to comment
Share on other sites

Bill, You know I'm just a hobbyist

Better yet to ask about the office engineers,

say SEANT could be explain the matters much better than me

(my sick brain has been remember just acaddoc.lsp way)

CU

 

~'J'~

Link to comment
Share on other sites

As I examine the code that Fixo shared with us, I think I can see that somewhere down where he comments "Create your stuffs....

 

I think this is where I could get the code to carry out this command:

 

(load "\\\\the_big_server\\path_to_VLISP_File.lsp" function_name "")

 

But something tells me that I won't be able to just replace what Fixo's code did with this single line. Am I on the right track here?

Link to comment
Share on other sites

Okay, after countless searches, which by the way many crooks are now using VBA, AutoCAD, etc... searches to highjack you to their websites, I found enough information which got me this line and it works. Now to try and interface the other piece of my macro which writes the text file with it and we're there.

 

acDoc.SendCommand ("(load ""D:/Pathname/My_Lisp.lsp"" ""The load failed"") function_name" & Chr(13))

 

Special thanks to SEANT and fixo on this. You guys are the greatest!

 

.... hey and I really mean that!

Link to comment
Share on other sites

Ouch...! I got burned when trying to put the two parts of the puzzle together. Here is fixo's code all butchered up as I took many of the items out of it I did not need.

 

Option Explicit
'' Require Reference to:
'' Tools--> References --> AutoCAD 2XXX Type Library
'' Tools--> References --> AutoCAD Focus Control for VBA Type Library
'' and also set options here:
'' Tools--> Options --> General --> Error Trapping -> check 'Break on Unhahdled Errors' button
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
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long

Sub runac()
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 = CStr(Cells(1.1).Value) <--- Save for later
strDrawing = ""S:\Path_to_DRAWINGS\TPS.dwg"" ' <-- hard coded

Set acDoc = acApp.Documents.Open(strDrawing)
acDoc.Activate
Set acDoc = acApp.ActiveDocument
acDoc.SendCommand ("(load ""S:/Path_to_Files/MyCode.lsp"" ""The load failed"") Function_Name" & Chr(13))

Err_Control:
End Sub

 

Now I need to put it together with this existing code:

 

Sub Main()
Call makeFile
Call runac
End Sub

Sub makeFile()
Dim ce As Range
Dim RetVal
Open "H:\tps.txt" For Output As #1
For Each ce In Worksheets("TPSBILL").Range("D5:D6,K6,M6,D8,F12:F19,F21,F23:F27,F29:F30,F32:F33,F35,F38:F39,I17")
   Write #1, ce.Value
Next ce
Close #1
End Sub

 

The Main sub calls the two other subs, both of which work fine on their own. If I put the code for runac sub in the IDE complains and will not compile. But runac will run just fine if I put it in all by itself. Can someone help me with how to get these in synch? I'm just not that familiar with the VB syntax yet.

Link to comment
Share on other sites

The routines are finally all married together as one, although I may get a not so passing grade for coding ettiquitte. Regardless, it's working now but I was hoping for just a few more bits of information.

 

1. I load two AutoCAD Type Libraries in References for this to work. And it does work well. But when I shutdown and come back at it later, the references are unload from Excel. I was of the opinion that once you loaded these, they stayed put unless you unloaded them manually. So what's the trick to having them stay static with this macro?

 

2. AutoCAD loads, opens the drawing file, loads and runs the VLISP and in the blink of an eye the drawing is modified and ready for printing, etc... But with the latest in-tar-nation of my code, AutoCAD looses focus and the Excel screen comes back into play. You will notice that I removed the code from Fixo's contribution which exited AutoCAD. AutoCAD does indeed remain on the task bar but I don't want to disappear until the user closes out of it.

 

Any suggestions would be appreciated.

 

UPDATE: Both of the above points have been resolved. The libraries were saved with the Excel file once I entered some properties for the Project and resaved it. The other issue was if you run the macro from the IDE it will jump back there once AutoCAD is completed it's thing. Run it from the spreadsheet using the button and AutoCAD remains where we want it for the users.

Edited by Bill Tillman
Link to comment
Share on other sites

Slowly but surely the process is continuing. The boss ran it on his computer which has Office 2010 and AutoCAD 2009. The computer I developed all this on has Office 2003 and AutoCAD 2009. It worked perfect, right out of the box. But alas, we went to one of the other users running AutoCAD 2005 and tried it. The first thing which happend was Excel reported one of the libraries was missing. We knew that would happen so we unchecked the missing one and checked the AutoCAD 2005 Type Library. Simple enough so it should now work. NOT!....it does write the text file but AutoCAD 2005 is a no-show. Nothing beyond that happens the computer just sits there, no debuig information whatsoever. I stepped through the code and it executes the line to run AutoCAD....and just buzzes right past it.

 

My luck didn't hold out on this with the older versions of AutoCAD.

 

UPDATE: I don't know if I wasn't facing the right direction when I launched the code or it I just wasn't holding my tongue in the right position but I restarted Excel and everything worked.....this is a real relief but did someone say "Murphy's Law"?

Edited by Bill Tillman
Link to comment
Share on other sites

Work had me in the field throughout, and I see quite a bit of Excel/AutoCAD integration happened while I was away. Nice progress.

 

It doesn’t sound like the progress went without a hitch, necessarily, though is reminiscent of most of my programming endeavors. Successful conclusions to particularly sticky problem tend to be all the more satisfying. Your other post, Bill, suggests that you may shift to .NET sometime soon: Plenty of opportunity to find more of that type of satisfaction there.

 

In addition to this forum, theswamp.org is another great resource. Fixo, and numerous other programmers here are also swampers. . . . you’ll feel right at home.

Link to comment
Share on other sites

Thanks Seant. I've been giving it the old college try as we say.

 

My trouble today is this. We have some users with AutoCAD 2005 and some with 2009. The Active X portion of the script won't function unless the proper AutoCAD Type Libraries are enabled in Excel References. And since the boss only wants one Excel file to deal with I've had to come up with a way to determine what version of AutoCAD the user is running so I can set the GUID's properly. I came up with just such a method and that part works fine. And the other part of the process which opens up AutoCAD and runs the VLISP code is also working flawlessly.

 

The problem is that I cannot successfully marry these two codes together. The second part has some declarations which must come up front. So I created a second module and placed the code for the second part in it. I can run the code in the first module and it will very accurately setup the correct Reference Type Libraries. These of course are required before the second part can run. Then I can run the code in the second module and all is well.

 

So I think cool, I'll just place a Call statement at the end of the first module and call the Main sub in the second module. You'd think that would do it? NOT! When I do this the code in the first module seems to be skipped over. The crash happens at the first Dim statement which needs the References set. With this method the References do not get set, it's like all the code in the first module is skipped over except the call statement.

 

I will figure this out but like you I haven't written code in years and that was when we used QuickBasic...the DOS version mind you.

 

And yes, the boss here and I have been talking about .NET...of which neither of us know the first thing. But at one time I didn't know how to have great sex with beautiful dames and I figured that out on my own too!

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...