Run Command or Code when Excel changes

Run Command or Code when Excel changes

Anonymous
Not applicable
1,034 Views
4 Replies
Message 1 of 5

Run Command or Code when Excel changes

Anonymous
Not applicable

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

 

0 Likes
1,035 Views
4 Replies
Replies (4)
Message 2 of 5

Keith.Brown
Advisor
Advisor

Any reason why you are using the editor instead of using the API to create a line?

 

Which version of AutoCAD are you developing for?

0 Likes
Message 3 of 5

Matti72
Advocate
Advocate

We had a similar problem... and we were only able to solve it by using a mixed mode dll, because we have to use functions, which we found only in the ObjectARX SDK but not in the .net api.

 

Our function looks like this:

 

[System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern System.IntPtr SetFocus(System.IntPtr hwnd);

        static public void ActivateEditor()
        {
            SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Window.Handle);
           MgHelper.SetFocusToAcadFrame();
           MgHelper.SendMouseMoveToView();
            System.Windows.Forms.Application.DoEvents();
        }

With these functions in the mixed Mode .dll:

void MgHelper::SendMouseMoveToView()
{
	acedGetAcadDwgView()->SendMessage(WM_MOUSEMOVE,NULL,NULL);
}

void MgHelper::SetFocusToAcadFrame()
{
	acedGetAcadFrame()->SetFocus();
}

Without using the ObjectARX staff, we weren't able to get the editor work....

0 Likes
Message 4 of 5

Anonymous
Not applicable

Hi Keith,

 

I found a work-a-round to get this working, but to answer your questions for other readers who may have the same issue, I am developing for 2015.  The purpose of the line command was just to test getting a command through the editor.

 

I have a palette, and when a button is pressed, it opens excel and populates a spreadsheet.  If certain cells in the speardsheet are changed, I wanted it to run code in autocad.  The code never wouold run as intended, it just did nothing.  I could get a message box to pop up, so i knew the event was being handled.  I created a command that does what the code did, and was trying to get the command to run as a work-a-around.  That is where this post stemed from.

 

I did read about executing in command context, and it looked like this would solve the problem, but then read it didn't work properly in 2015, but it was fixed in 2016.

 

Basically, what I did to get this working is create a new command that calls my custom command with ed.command which can pass through the parameters, and I call this dummy command with Doc.SendStringToExecute.  Its messy, and I was hoping for a cleaner way of making this work, but for now, its working.  Here is a snippet of the idea:


'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 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 Shared xlTargetValue As Integer = Nothing Dim xlTargetRow As Integer = Nothing Dim XlTargetCol As Double = Nothing Public Sub xlSheet_Change(ByVal Target As Excel.Range) Using acLckDoc As DocumentLock = Doc.LockDocument() If XlTargetCol = 20 Then Doc.SendStringToExecute("ContinueExcelTest ", True, False, False) End If 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 <CommandMethod("ContinueExcelTest")> Public Sub cmdJWtest3() Dim Doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = Doc.Database Dim ed As Editor = Doc.Editor Dim civDoc As CivilDocument = CivilApplication.ActiveDocument Using acLckDoc As DocumentLock = Doc.LockDocument() ed.Command("CustomCommandHere, "parameter", "parameter") End Using End Sub End Class

 

 

0 Likes
Message 5 of 5

Anonymous
Not applicable

Thanks Matti.  I will play around with your code and see what I can learn.

 

Its been a little frustrating, but I think this might have been fixed in the 2016 release.  I haven't verified yet.

0 Likes