Run Command or Code when Excel changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I got another one... I tried something today and I am having trouble getting it working and I was hoping one of you Guru's could help me again.
I am exporting data from autocad into excel. If something in excel changes, I want it to run code, or a command, in Autocad. It seems to be an issue with the focus being on excel when cells are changed. I have found many articles about people having similiar issues, but most were dealing with focusing from a Palette back to the editor, and none seem to be working
When the excel change event fires, I can get a message box to pop up, and even run a command using Doc.SendCommandToString(). I need to be able to pass parameters into the command, so that doesn't work for me. Nothing using the editor works. (e.g. ed.Command(), ed.WriteMessage()
What am I missing here?
Here is the code I am using: (The things commented at the top of "Sub xlSheet_Change" are things I have tried to set the focus to the editor)
'AutoCad Imports
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Windows
'Civil3d Imports
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices
'System Imports
Imports System.Runtime.InteropServices
'Excel Imports
Imports Microsoft.Office.Interop
Public Class Testing
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function SetFocus(hWnd As IntPtr) As IntPtr
End Function
Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = Doc.Database
Dim ed As Editor = Doc.Editor
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook = Nothing
Dim Event_CellsChange As Excel.DocEvents_ChangeEventHandler
Public WithEvents xlSheet As Excel.Worksheet
Public Sub xlSheet_Change(ByVal Target As Excel.Range)
'Microsoft.VisualBasic.AppActivate("Autodesk AutoCAD Civil 3D 2015")
'Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Focus()
'SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle)
Using acLckDoc As DocumentLock = Doc.LockDocument()
'Do Code or run command here
ed.Command("Line")
'MsgBox("hello world")
'Doc.SendStringToExecute("Line ", True, False, False)
End Using
End Sub
<CommandMethod("ExcelTest")> Public Sub cmdExcelTest()
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = xlApp.Workbooks.Open("C:\test.xlsx")
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
xlSheet.Range("A1").Value = 1
Event_CellsChange = New Excel.DocEvents_ChangeEventHandler(AddressOf xlSheet_Change)
AddHandler xlSheet.Change, Event_CellsChange
xlApp.Visible = True
xlApp.UserControl = True
End Sub
End Class