PDA

View Full Version : Run-time error '5' : From Excel Macro to AutoCad



phucnguyentk
12-23-2014, 08:45 PM
Hi everyone, I'm new to this.

When I run the macro to draft shaped steel.
The VBA can't not activate AutoCad and show the error:
Run-time error '5': Invalid procedure call or argument

Here is my code:


Sub Macro3()
AppActivate ("AutoCAD")
SendKeys "{ESC}"


For i = 15 To 22


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
......


The error is at line ( AppActivate ("AutoCAD"))
I use Excel 2013 and AutoCad LT2015.
P/s: This macro worked on AutoCad LT2013

Please help me to solve this problem.
Sorry for my poor English.
Thank you.

Kenneth Hobs
12-29-2014, 07:19 AM
Welcome to the forum!

Does the application open have the window's title of "AutoCAD"?

phucnguyentk
12-31-2014, 02:26 AM
Welcome to the forum!

Does the application open have the window's title of "AutoCAD"?

I have found the reason of this problem. Cause the AutoCad LT2015 version doesn't support VBA application.
Anyway, thank you for the response.

Kenneth Hobs
12-31-2014, 06:12 AM
I see no Application specific commands in your code. Any Application that has VBA or vb.net or other programming languages can send keys. That is what your code was doing. To launch an Application if not active from VBA, use the Shell() command.

Sendkeys() methods, to work properly, require two things. (1) Focus, which is where your AppExecute() came in and (2) timing.

phucnguyentk
01-04-2015, 09:35 PM
Thank you very much, Kenneth HObs.

I have try shell() command and it worked perfectly.

And for the application which do not support with VBA.
Do you know how to send data to the application ( like Sendkeys() command) ?
I have try and the error is VBA can not activate application

My code launches AutoCad and then open the standard file and draw shaped steel.
Here is my code:

Sub Macro3()
Dim Pathname As String
Dim Filename As String
Dim RetVal As Integer

Pathname = "C:\Program Files\Autodesk\AutoCAD LT 2015\acadlt.exe "
Filename = "C:\Users\its145\Desktop\Test.dwg"

RetVal = Shell(Pathname & " " & Filename, vbNormalFocus)
AppActivate RetVal

SendKeys "{ESC}"

For I = 15 To 22

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

SendKeys "-Layer"
SendKeys "~"
SendKeys "S"
SendKeys "~"
SendKeys Cells(I * 2, 8).Value
SendKeys "~"
SendKeys "~"

SendKeys "Line"
SendKeys "~"
SendKeys Cells(I * 2, 7).Value
SendKeys "~"
SendKeys Cells(I * 2 + 1, 7).Value
SendKeys "~"
SendKeys "~"

Next I

End Sub

Kenneth Hobs
01-05-2015, 07:01 AM
There are two ways to get the handle. One is if you know the Window's Title and the other is if you know the classname for the application. Spy programs can be used to obtain both but the former is easily observed. To spy, I use PatorJK, 2nd link from bottom at: http://patorjk.com/blog/software/

AppActivate() uses the title. The API method to get that focus handle can be seen in this url: http://stackoverflow.com/questions/18311367/closing-external-application-from-vb6

Another method you could look at are the old Application.DDEInitiate() and Application.DDEExecute() methods: https://groups.google.com/forum/#!topic/autodesk.autocadlt.97general/chrsaTPo7Io