For the quick test add this code into ThisWorkBook module in VBAIDE (Alt+F11)etc
Insert module in your Excel fileCode:Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) MsgBox "Cell double clicked: """ & Target.Row & ":" & Target.Column & """" & vbCr & _ "Value: " & Target.Value & vbCr & _ "Add your code after this line" Cancel = True End Sub
paste the following code in there
this will get you started I hope
Code:Option Explicit ' Requires: ' AutoCAD 200X Object Library (tested on A2009 only) 'Tools->Options->General tab->Error trapping ->Break on Unhandled Errors Const folderPath As String = "C:\Users\...\TempVBA\" '<-- change the folder name here (add backslash at the end) 'by Randall Rath Function IsExcelRunning() As Boolean Dim objACAD As Object On Error Resume Next Set objACAD = GetObject(, "AutoCAD.Application") IsExcelRunning = (Err.Number = 0) Set objACAD = Nothing Err.Clear End Function Public Sub GotoAcad() Application.Cursor = xlWait Application.WindowState = xlMinimized Dim dwgName As String Dim rng As Range Dim handle As String Dim cel As Range Dim i As Long Dim j As Long Dim n As Long Dim k As Long Dim attArr As Variant Dim acApp As AcadApplication Dim acDocs As AcadDocuments Dim oLayout As AcadLayout Dim oEnt As AcadEntity Dim blkRefObj As AcadBlockReference Dim adoc As AcadDocument Dim attObj As AcadAttributeReference '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' On Error GoTo Error_Control Set rng = ActiveSheet.Range("C2") ' <--Zeichnung3.dwg2119022168 must be a handle instead of ObjectID dwgName = Trim(CStr(rng.Value)) n = InStr(1, dwgName, "dwg", 1) + 3 handle = Trim(Mid(dwgName, n)) dwgName = Left(dwgName, n - 1) 'MsgBox folderPath & dwgName & vbCr & "Handle: " & handle '<-- debug only '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' On Error Resume Next 'late binding to Acad Dim oACAD As Object Dim blnAcadRunning As Boolean blnAcadRunning = IsExcelRunning() If blnAcadRunning Then Set oACAD = GetObject(, "AutoCAD.Application") Else Set oACAD = CreateObject("AutoCAD.Application") oACAD.Visible = True End If ' acApp.ActiveDocument.SetVariable "SDI", 0 'optional Set acDocs = oACAD.Documents Set adoc = acDocs.Open(folderPath & dwgName, False) adoc.Activate Dim oAtt As AcadAttributeReference Dim oBref As AcadBlockReference Dim p1 As Variant Dim p2 As Variant Dim layoutname As String Dim layout As AcadLayout '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' Set oEnt = adoc.HandleToObject(handle) If oEnt Is Nothing Then MsgBox "Invalid handle, exit..." Exit Sub End If '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' If TypeOf oEnt Is AcadAttributeReference Then Set oAtt = oEnt Set oBref = adoc.ObjectIdToObject(oAtt.OwnerID) oBref.GetBoundingBox p1, p2 layoutname = adoc.ObjectIdToObject(oBref.OwnerID).layout.Name Set layout = adoc.Layouts.Item(layoutname) adoc.ActiveLayout = layout adoc.Application.ZoomWindow p1, p2 Dim attStr As String attStr = oAtt.TextString 'MsgBox attStr'<-- debug only oAtt.TextString = "New Value" oAtt.Update oBref.Update Else MsgBox "Object is not an attribute, exit..." Exit Sub End If adoc.SaveAs folderPath & dwgName, acNative adoc.Close Set adoc = Nothing Set acDocs = Nothing oACAD.ActiveDocument.Close oACAD.Quit Set oACAD = Nothing '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' DoEvents Application.WindowState = xlMaximized Application.Cursor = xlDefault MsgBox "Done, check a changes in the drawing" Error_Control: If Err.Number <> 0 Then Err.Clear MsgBox Err.Description End If End Sub



Reply With Quote


Bookmarks