Hi all,
I have a macro to copy selected cells and paste it into specific format, it’s basically a coordinate(easting and northing) list with semicolon delimiter, and then save it into notepad.
After that my macro open autocad and call a lisp that I wrote in autocad to import the notepad into point with description.
I use excel and autocad 2007.
My macro works as expected. However I have small irritating problem. Which is after I run the macro and the point is plotted on the autocad, if i deleted columns in excel by selecting by highlighting in the “ A B C D….” bar (For example I deleted column F to column I by highlighting the I to F bar), and then I click the “File” or “Edit” in the autocad toolbar. The excel will crash. It will close itself and then opened again in recovered mode.
When I click the “file” or “edit” the mouse cursor is showing the “thinking” animation for a second. And then the excel just crashed.
After several testing, the workaround is to perform an action in excel. For example if I copy and paste a random cell after I deleted columns, when I click the “File” or “Edit” it will not crash the excel. I’m guessing it’s a problem with the way I wrote my code and something related to selection.
It’s just that I want this code to be also used by my friends and I want it to be free of bugs.
I wonder if there’s something that I can improve in my vba code? I tried of many ways that I can think of but to no avail.
I hope that there’s someone with similar problem that know the solution or someone that can enlighten me with the problem
Thank you very much.
Here is my vba (how to attach it?)
I have a macro to copy selected cells and paste it into specific format, it’s basically a coordinate(easting and northing) list with semicolon delimiter, and then save it into notepad.
After that my macro open autocad and call a lisp that I wrote in autocad to import the notepad into point with description.
I use excel and autocad 2007.
My macro works as expected. However I have small irritating problem. Which is after I run the macro and the point is plotted on the autocad, if i deleted columns in excel by selecting by highlighting in the “ A B C D….” bar (For example I deleted column F to column I by highlighting the I to F bar), and then I click the “File” or “Edit” in the autocad toolbar. The excel will crash. It will close itself and then opened again in recovered mode.
When I click the “file” or “edit” the mouse cursor is showing the “thinking” animation for a second. And then the excel just crashed.
After several testing, the workaround is to perform an action in excel. For example if I copy and paste a random cell after I deleted columns, when I click the “File” or “Edit” it will not crash the excel. I’m guessing it’s a problem with the way I wrote my code and something related to selection.
It’s just that I want this code to be also used by my friends and I want it to be free of bugs.
I wonder if there’s something that I can improve in my vba code? I tried of many ways that I can think of but to no avail.
I hope that there’s someone with similar problem that know the solution or someone that can enlighten me with the problem
Thank you very much.
Here is my vba (how to attach it?)
Code:
'This script is to draw point with description in the current active autocad drawing
'Selected cells will be concatenated to Navipac wp2 format and then saved in L:\Plot to CAD\XLtoCAD.wp2
'And then it will open autocad or if autocad already opened will tell the cad to type command "wew", which is custom LISP to draw point from wp2 format file
'Point will be saved in layer "Point", and description will be saved in layer "Description" with magenta color
'Agung Hutomo 2016
'Public ACAD As Object
'sub to work with cad is from howtoautocad.com/excel-autocad--a-match-made-in-heaven-again/
Sub open_Cad()
Dim warning As Integer
'ENSURES USER KNOWS OPEN DRAWING WILL BE EDITED, YES NO TO PROCEED
warning = MsgBox("Selected Coordinates will be plotted into currently opened drawing." & vbCrLf & "Would you like to continue?", vbYesNo, "Data Loss Warning")
Select Case warning
'if they select yes to proceed
Case 6
Set ACAD = AcadApplication 'Create ACAD variable of type AcadApplication
On Error Resume Next 'This tells VBA to ignore errors
Set ACAD = GetObject(, "AutoCAD.Application") 'Get a running instance of the class AutoCAD.Application
On Error GoTo 0 'This tells VBA to go back to NOT ignoring errors
If ACAD Is Nothing Then 'Check to see if the above worked
Set ACAD = New AcadApplication 'Set the ACAD variable to equal a new instance of AutoCAD
ACAD.Visible = True 'Once loaded, set AutoCAD® to be visible
End If
Case 7
Exit Sub
End Select
ACAD.ActiveDocument.SendCommand ("wew ") 'Print a message to the AutoCAD® command line
ACAD.ActiveDocument.SendCommand ("regen ")
'Inform the user that the drawing was created.
MsgBox "The coordinates was successfully exported to Autocad!", vbInformation, "Finished"
End Sub
'Sub Talk_CAD()
'End Sub
Sub concatwptocad()
'concatenate selected desc, e, n into wp2 format
Dim ActSheet As Worksheet
Dim SelRange As Range
Dim warn As Integer
Set ActSheet = ActiveSheet
Set SelRange = Selection
' Turn off screen updating.
Application.ScreenUpdating = False
ActSheet.Select
SelRange.Select
Selection.Copy
Sheets.Add After:=ActSheet
Range("A1").Select
ActiveSheet.Paste
Range("D1").Select
Application.CutCopyMode = False
If Range("A2") = vbNullString Then
ActiveCell.FormulaR1C1 = "=CONCATENATE(char(34),RC[-3],char(34),char(59),RC[-2],char(59),RC[-1],char(59),""0.000"",char(59),14.1,char(59),4.1,char(59),14.1,char(59),char(34),""Arial"",char(34),char(59),""0.00"",char(59),-2.1,char(59),char(34),char(34),char(59),""0.00"",char(59),char(34),char(34),char(59),1,char(59),""0.000"",char(59),""0.000"",char(59),""0.000"",char(59),0,char(59),0.05)"
Else
ActiveCell.FormulaR1C1 = "=CONCATENATE(char(34),RC[-3],char(34),char(59),RC[-2],char(59),RC[-1],char(59),""0.000"",char(59),14.1,char(59),4.1,char(59),14.1,char(59),char(34),""Arial"",char(34),char(59),""0.00"",char(59),-2.1,char(59),char(34),char(34),char(59),""0.00"",char(59),char(34),char(34),char(59),1,char(59),""0.000"",char(59),""0.000"",char(59),""0.000"",char(59),0,char(59),0.05)"
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D" & Range("A" & Rows.Count).End(xlUp).Row)
End If
With Application
If Range("A2") = vbNullString Then
Range("D1").Select
Selection.Copy
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Application.CutCopyMode = False
End If
Call Copytonotepad
End With
Call delsh
ActSheet.Activate
Call open_Cad
'Call Talk_CAD
' Turn off screen updating.
Application.ScreenUpdating = True
Set SelRange = Nothing
Set ActSheet = Nothing
End Sub
Private Sub Copytonotepad()
Dim f As Integer, c As Range
f = FreeFile
Open "L:\Plot to CAD\XLtoCAD.wp2" For Output As #f
For Each c In Selection
Print #f, Replace(c.Value, vbLf, vbCrLf)
Next c
Close #f
Exit Sub
End Sub